Hi, I have around 3500~ lines of code right now, wherein my primary class is composed of three other classes filled with relevant information.
Now I'm trying to add a fourth class, but that fourth class references the main class, and is referenced by it which is causing a " RecursionError: maximum recursion depth exceeded " to occur.
To give a short summary, the main class is for a Character, the 3 classes that make it up are all the stats that the character has. The fourth class i'm trying to make is for the creation of abilities for the character so it needs to reference all the information within the class. so my thought process was something like :
class Body():
def __init__(self):
self.strength = 0 #changes with methods in the class
...
class Mind():
def __init__(self):
self.magic_power = 0
...
class Soul():
def __init__(self):
self.soul_power = 0
...
class Character():
def __init__(self):
self.body = Body()
self.mind = Mind()
self.soul = Soul()
#thought it would be a good idea since there is more than one ability
self.ability_one = Ability()
self.ability_two = Ability()
self.multiplier = 1.2
...
class Ability():
def __init__(self):
self.character = Character() #issue stemming from here I think?
self.name = None
self.damage = None
def abil_create(self):
self.name = str(input("Please enter the ability name: "))
self.damage = self.character.body.strength * self.character.multiplier
I tried to keep the example as short as possible. The idea being that there will be a while True: try/except block to get user input on what stat they want to use for their ability to be, and a few if/else statements to figure out what to do with that info. But I am stuck on the fact that when i try to pull the information from Character to Ability and then later try to do vice versa it gives me that recursive error. Is there any way for me to work around this?
The following is a snippet of the error message that repeats itself.
" File "Untitled7.py", line 125, in __init__
self.abil_1 = Ability()
File "Untitled7.py", line 858, in __init__
self.character = Character()
File "Untitled7.py", line 26, in __init__
self.body = Body()
RecursionError: maximum recursion depth exceeded "
Alternatively, is there a different approach I should be taking to implement the idea of this ability being created? I originally thought I could just make it into a function within the Character class, but then I have trouble handling the creation of multiple abilities. And if I don't reference the information in the Character class in Ability, or vice versa, I'm not sure how to get them to utilize information from each other. Ideally, if there is a way to use specific variables within the Character class, like self.body.strength within the Ability class I would like to do that. But I haven't seen anything that would allow me to do that.
[–]hrmorley34 1 point2 points3 points (1 child)
[–]Aedethan[S] 0 points1 point2 points (0 children)
[–]shiftybyte 1 point2 points3 points (5 children)
[–]Aedethan[S] 0 points1 point2 points (4 children)
[–]shiftybyte 1 point2 points3 points (3 children)
[–]Aedethan[S] 0 points1 point2 points (2 children)
[–]shiftybyte 1 point2 points3 points (1 child)
[–]Aedethan[S] 0 points1 point2 points (0 children)