Melkoshi wrote:
I figure i'd ask incase there was a function or something that did it anyways.
As it goes, the way this kind of thing is usually done is to define a class to store your stats and access particular modifications of them in. Classes are passed by reference rather than value (meaning that when you pass your class into a function and modify bits of it, the changes show up everywhere that class is used, as opposed to regular variables where the '5' you pass into a function is a completely different '5' to the variable you got the value from) and thus it's easier to keep consistent.
So if you're planning to do this kind of thing a lot, and you're feeling a little more confident with your programming, it's probably worth reading up on classes. They're not incredibly difficult - he's a quick example (from the Python interactive prompt, so please excuse the '>>>' and '...'s ;-) - and don't get daunted by the length of the code blocks, most of it's example usage. ;-):
Code: Select all
>>> class Fighter(object):
... # This function gets called when you create a new Fighter
... def __init__(self, strength, health):
... # you can use 'self' to refer to the particular Fighter you're using
... # from within any function on the class, but it has to be the first
... # parameter to each function in the class.
... self.Strength = strength
... self.Health = health
... self.MaxHealth = health
... self.Alive = True
... # This function can be called from the Fighter you've created in your code,
... # and will affect that Fighter's Health using 'self':
... def Damage(self, damage):
... self.Health = self.Health - damage
... if self.Health <= 0:
... self.Alive = False
...
>>> # This function takes a Fighter instance as a parameter and modifies a property of it,
... # but any other references to that Fighter elsewhere in your code will see the change.
... def Heal(fighter, health):
... fighter.Health = fighter.Health + health
... if (fighter.Health > fighter.MaxHealth):
... fighter.Health = fighter.MaxHealth
...
>>> def GiveStrengthBoost(fighter, boost):
... fighter.Strength = fighter.Strength + boost
We create an instance of the class by calling it like a Function:
We can look up properties by putting a dot then the property name after the name of our variable:
Code: Select all
>>> boris.Strength
10
>>> boris.Health
100
When we call functions that are defined on the class themselves, like 'Damage', we do it with a dot, similar to properties:
Code: Select all
>>> boris.Damage(15)
>>> boris.Health
85
>>> boris.Damage(34)
>>> boris.Health
51
When we call functions that take a class as a parameter, the same instance of the class is modified even if we never run any code to synch up the changes anywhere:
Code: Select all
>>> Heal(boris, 25)
>>> boris.Health
76
>>> Heal(boris, 25)
>>> boris.Health
100
>>> GiveStrengthBoost(boris, 5)
>>> boris.Strength
15
Most importantly, we can have two separate Fighters and use them completely separately, so it's much easier to keep track of all your data:
Code: Select all
>>> steve = Fighter(15, 80)
>>> steve.Health
80
>>> boris.Health
100
>>> steve.Damage(10)
>>> steve.Health
70
>>> boris.Health
100
>>> GiveStrengthBoost(steve, 10)
>>> steve.Strength
25
>>> boris.Strength
15