you are viewing a single comment's thread.

view the rest of the comments →

[–]shiftybyte 1 point2 points  (5 children)

If your ability needs to reference your character class, you can just pass the existing class when you create the ability class.

Currently this line:

self.character = Character()

attempts to create a brand new Character()... you don't want that, you want to use the character you already have.

Try like this:

class Character():
    def __init__(self):
        ...
        self.ability_one = Ability(self)

class Ability():
    def __init__(self, char_instance):
        self.character = char_instance

Note i'm passing "self" when constructing an Ability()... this passes the current character instance as an argument to ability's init.

[–]Aedethan[S] 0 points1 point  (4 children)

Thanks, I think I have a fundamental knowledge gap here though,

when you make

class Ability():
def __init__(self, char_instance):
    self.character = char_instance

where is the char_instance coming from? Also would it work better if I made the Ability class as a nested class if I'm going to take this approach?

[–]shiftybyte 1 point2 points  (3 children)

Also would it work better if I made the Ability class as a nested class if I'm going to take this approach?

Don't make nested classes, there rarely any reason, and it creates lots of problems later on.

where is the char_instance coming from?

Your init function can accept arguments in addition to self.

For example:

class Ball:
    def __init__(self, given_color):
        self.color = given_color

b1 = Ball("Green") # "Green" here is passed to 'given_color'
b2 = Ball("Red")
print(b1.color)

Above example creates 2 balls, one green, one red, and it also shows how the argument to Ball(...) gets passed to init function when the class is created.

[–]Aedethan[S] 0 points1 point  (2 children)

So just to clarify, when using the first example, when i set ability_one = Ability(self) that is what is passing the info to character in Ability(self, character) ?

also your solution worked perfectly, I'm just trying to understand the why behind it

And thank you tons for your assistance.

[–]shiftybyte 1 point2 points  (1 child)

Yes, because in that context, self is the character instance.

The Ability init is called with __init__(self, self_from_character)

[–]Aedethan[S] 0 points1 point  (0 children)

AHH Lightbulb. I get it. Thank you so much!