I tried searching around, but I don't understand exactly what I'm looking for so it could very well exist already and I just don't know how to ask the search engines about it. I apologize in advance. Thus the question mark on the thread title.

My current background of programming: some. Took Java classes in college (disliked it), and a friend tried to teach me SheerPower 4GL (disliked it more)... that's about it. I've only started learning Python roughly a week ago, starting by making a simple role-playing engine. Why? Because I'm a nerd, I like RPGs, and it's something I (and friends) can relate to.

Anyway, as an example, let's say I have this code:

class Character:
    def __init__(self, characterName):
        self.Name = characterName
        
        self.BaseStatistics = {
            "Health": 30,
            "Magic": 10
            }
        self.CurrentStatistics = {
            "Health": 20,
            "Magic": 0,
            "Alive": 1
            }
        
        ... more variables...

Then I create two characters (or three, or four, etc.):

theFirstCharacter = Character("Charles")
theSecondCharacter = Character("Bailey")

Then I create an Item class with values for modifying the Character's dictionary:

item_Potion = Item("Potion", {"Health": 50})

Then a useItem definition for the Character to place those values where they're supposed to go. It works wonderfully, no problem: the one character uses it, it restores they're health by up to fifty points. As such:

theFirstCharacter.useItem(item_Potion)

However, I've run into a snag... Let's say I have an item called "Revive" and I want it to restore the character from a dead state and heal 50% of their BaseStatistics health. I can do it by specifically referencing a character, yes, as such:

item_Revive = Item("Revive", {"Alive": 1, "Health": (theFirstCharacter.BaseStatistics["Health"] / 2)}

But that only works for theFirstCharacter: theSecondCharacter would never get that effect... So how would I, in the Item's attributes, tell it to heal only 50% of the max health of the Character using it? I have no idea how to even start trying to figure this out... There were a couple brainstorms, though:

1) At first I thought maybe generically reference anything created by the Character class, but then how would I tell the item, inside of itself, which specific character used it? *facepalm* Nervermind...

2) Then I thought about storing a variable at the very beginning of the useItem definition to grab the internal name of the character using it (ie. theFirstCharacter and not "Charles") so that it would work on any character using it by placing that variable into the item... but I don't know how to do that and it wouldn't work anyway, because the character would use the item before that variable could be correctly adjusted for the character using it. *facepalm*

3) About three other stupid ideas... and such...

4) Okay, to DaniWeb.

Broken example (just so you can understand, hopefully):

item_Revive = Item("Revive", {"Alive": 1, "Health": (THE_CHARACTER_USING_THE_ITEM.BaseStatistics["Health"] / 2)}

Is this possible? Please, any help would be appreciated. I hope you guys understand this, because I'm at a loss... Thank you!

When you call character.useItem(item) you want the item to update the character's statistics using an item-specific dynamic rule. One way to do this is to define an action function for the item instead of a dictionary of statistics

class Character(object):
    def __init__(self, name):
        self.name = name
    def useItem(self, item):
        item.action(item, self)
        
class Item(object):
    def __init__(self, name, action):
        self.name = name
        self.action = action

def action_hello(item, character):
    # implement whatever action the item should do here
    print("hello %s!" % character.name)

item_hello = Item("hello", action_hello)
charles = Character("Charles")

charles.useItem(item_hello)

"""my output -->
hello Charles!
"""

Other ways would be to define specific item classes with an action() method.

commented: Unfortunately now no points, likely. Bug is eating them ;) +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.