# Rpg Stats - Temporary Changes, Harder Than I Realised!?

This topic is 714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm writing a turn-based RPG, and have a small coding dilemma about stats. Specifically, it's about when you need to make temporary changes. Say the player has a speed which is currently 50, and the spell "slow" takes 10 off the speed. So it's reduced to 40, and in a few turns it has 10 added back so it's back at 50. Fine, cool with that.

Now, suppose the speed is 5. I take the 10 off, but aha, can't be < 0, so I set it to 0. A few turns later I restore it, but now it goes back to 10, which is clearly wrong and an exploitable side-effect, so I don't want that to happen.

I have 2 choices as I see it.

1. Allow the speed to be negative, but whenever I use it, I clamp it to the range [0,100] (say). Note that the same goes for if the speed is 95, I allow it to go to 105 if the "fast" spell is used. I need to be very careful when coding this, because if I fail to clamp, things will go quite wrong and it will be a bit unpleasant to track down.

2. Create an undo record that says "only increase by 5 when you put it back". This requires an architecture for storing the changes, but also needs quite a lot of care to ensure consistency and I am not 100% convinced it can be made to be consistent because other things can be messing with the stats inbetween the change and the undo. I rejected the "store the actual speed it goes back to" option for this very reason.

So: am I missing other possibilities, or if not, which of the two appeals and why?

##### Share on other sites

I'd just add a "is under slow spell influence" property to the player that can be set/reset independently

##### Share on other sites
I love Hodgman's stack idea. In my games it's been a bit simpler - just store a list of modifiers/effects/buffs/whatever on the player, and when querying a stat, the query is always implemented as "base stat + (total of relevant modifiers)". In that situation you have to be careful about ordering because +10 followed by *2 is not the same as *2 followed by +10.

##### Share on other sites

Yep, this can lead to troubling bugs (or exploits) when attributes can change as well -- e.g. if you happen to level-up while a buf is enabled.

So:
3. Represent the stats as a stack of operations.
e.g. a stack for your speed stat might contain:
*Slow: Subtract 10
*Base: Set 50

When you evaluate that from the bottom up, you get (50-10) = 40.

When you cast the slow spell, the new instance of that spell adds an operator to speed's stack and retains a handle to it. When the spell ends, it uses that handle to remove the operator that it added earlier.

If the player levels up, you can modify the 'base' item in the stack above, and it will automatically allow you to recompute the new resulting speed value, including buffs.

You can keep a cache of the speed attribute as well as this stack, and update the cache whenever the stack is modified... But never change that cached value -- always change the stack of operators, and let the stack update the cache with the new value.

Yeah I like that. Nice to know that it was genuinely more complex than I had initially thought. Thanks!

##### Share on other sites

I all solutions above are great solutions for an RPG. Which one is better may depend on the requirements of your game.

However, one thing they all have in common is that the base speed is stored with the character and not the modified speed.

The modified speed is calculated when the speed is requested

A simple example of a GetSpeed function could look like the following:

class character {
private int _baseSpeed;
private bool _isSlowed;

public int GetSpeed() {
int speed = _baseSpeed;

if(_isSlowed) {
speed -= 10;
}

//return 0 if calculated speed < 0, otherwise return calculated speed
return speed < 0 ? 0 : speed;
}
}


Edited by Senorit

##### Share on other sites

A quick and simple fix I use myself is just to let buffs and de-buffs effect the percentage of a stat, so: round(Speed/de-buff).

This is nice as it will also prevent the speed from reaching 0 in most cases.

##### Share on other sites

3. Represent the stats as a stack of operations.
e.g. a stack for your speed stat might contain:
*Slow: Subtract 10
*Base: Set 50

When you evaluate that from the bottom up, you get (50-10) = 40.

A similar approach, and the one we took, was to have two classes for characters (or enemies, but lets focus on characters). One is called GlobalCharacter, and this is the permanent class object that maintains all of the character's stats. When we enter a battle, we create a BattleCharacter class which contains a copy of all the GlobalCharacter stats. We don't touch the global character stats in battle, so we always have the original values. When the battle ends, we set the stats on the GlobalCharacter to those of the BattleCharacter to reflect changes like HP or mana.

This also has the convenience of allowing us to restore the conditions when a battle began, which is something we take advantage of as we allow the player to restart a battle a number of times if they are defeated.

##### Share on other sites

A good design pattern for this is the Decorator pattern. Applied to this problem, you can think of it as an object-based formalization of Hodgeman's stack suggestion, or some of the other suggestions here.

How I use it is this --

First, in my case, characters' stats are fixed per-level, and stored in a read-only array-of-structs -- there's one struct for each character and for each level. The character data structure points to this for its base stats. Weapon, armor, and item stats are similar, but separate -- You can think of all of this as being a big dictionary, or set of spreadsheets or database tables -- in fact, that's usually how I author this data, which gets fed into the build (or could be read from a file a load-time).

*side note* If you have non-static leveling (e.g. where the user allocates their own points when they level up) you would manage this base data differently, but the basic pattern and application stays the same. You have a couple obvious choices: One is that you do away with the dictionary and just have a base-stat data structure for each character/item that you modify directly; The second way is that you still have the dictionary of read-only base stats, and you implement the user-allocated points themselves using this pattern (just apply them before any other buffs/debuffs/effects).

Now you apply the decorator pattern to that base stat structure -- basically, you have a decorator that presents the same interface as the structure itself, and when a decorator is applied, the character points to the decorator, and the decorator points to the base stat. This means the character reads from the decorator, which is where it gets its opportunity to modify the base values. What about multiple effects/buffs/debuffs? Simple! Since the decorator has the same interface as the base state, a decorator can point to either the base state or another decorator as long as they derive from the same interface and you use a pointer to the base class -- you can stack them arbitrarily deep, and each decorator will modify the sum of the base stat and all the decorators that came before it.

This might sound like an awful lot of engineering for something so simple, but its very robust and the pattern isn't terribly complicated. One of the things that makes it nice is that it really decouples the implementation of effects from one another and from the base stats structure. They don't need to know anything about what the other states look like, how many there are, or what they can do. It communicates only the language of the character stats (though, in my implementation, I have a side-channel that allows an effect to know if a certain effect has already been applied -- for items, think of a set of armor that's stronger when all its pieces are equipped.)

Also worth noting, is that the reason I like to pass in the entire character stat-set, rather than having individual decorators for each stat is that it makes it much easier to have multi-dimensional effects that modify several stats, or which take into account multiple stats in determining its value (e.g. a character's defense against spells might be modified based on both their magic resistance and their intelligence) -- its also a whole lot less to manage and a whole lot less code to write.

To talk about implementation of the interface for a bit, it'll depend on whether you're programming in a language with properties (like C#) or without (like C++) -- if you have properties, then you can use them to implement getters for each base stat (and setters, if you don't go the dictionary of read-only structs route). Otherwise, you need to implement member functions to get the values out.

Another good shortcut to know about is that in C++ a virtual function can have a default implementation -- so what I do is when I define the base class for the decorators is give the virtual getter a default implementation that just reads the value form whatever it points to and passes it along unmodified. That way, when an implementation of a particular decorator only modified one stat, I only have to implement that one stat's getter without any other boilerplate. Not only does it saves me typing, it removes the opportunity to make mistakes.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002141
×