0

I am trying to create a magic system in my text-based RPG that refers to an attribute of a monster class using the attribute from the magic class. The monster class looks like

class monster(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

with the list of monsters being store in the format

bestiary = { 
99999: monster(name="Slime", currentHP= 3, maxHP= 10, initiativeMod= 1, AC= 0, baseAttack= 0, equippedWeapon= itemsList[13], speed = 10) ##Syntax items 
}

The spells are created in the form

class BuffSpell(object):
def __init__(self, **kwargs):
    self.__dict__.update(kwargs)

with instances of each spell in the form

bardSpells = {
    2: BuffSpell(name= "Flare", level= 0, stat= "baseAttack", value = -1, MP = 3, spellType = "buff"),
}

I am trying to refer to an attribute in monster that is given by an attribute in the spell like this

def useMagic(target, spell):
if spell.spellType == "buff":
    x = spell.stat
    target.x += spell.value

which of course doesn't work. How can I get the spell.stat attribute and apply spell.value to the corresponding attribute in monster?

0

1 Answer 1

1

You could try something like the following:

def use_magic(target, spell):
    if spell.spell_type == "buff":
        stat = spell.stat
        setattr(target, stat, getattr(target,stat) + spell.value)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.