Skip to content Skip to sidebar Skip to footer

Class Attributes Not Passing To Objects

I'm trying to create a very basic RPG-like game where you select a character, give that character a weapon, and then tell it to attack another character with damage based on the st

Solution 1:

Remember to use self, as in your other __init__ methods, to make attributes of the instance.

def __init__(self, name):
    self.name = name
    self.hp = 300
    ...
    self.weapon = None

Also, I think the other attributes should also be instance attributes, i.e., set them in the __init__ methods and use self, e.g. self.wgt, self.impact, self.broken_bones, etc.


Solution 2:

Change the body of the constructor, because now you are assigning values to local variables rather than to the object variables:

def __init__(self, name):
    self.name = name
    self.hp = 300
    self.mp = 10
    self.strn = 1
    self.dmg = 1
    self.dex = 1
    self.armor = 0
    self.weapon = None

Post a Comment for "Class Attributes Not Passing To Objects"