# How to implement expiring and self-refreshing buffs in an RPG?

I have a turn based RPG with buff system where buffs are always on, and the player has an option to cancel them at any time.

An exception to this system is buffs like "Effect lasts until the end of this turn" or "Absorbs first x points of damage". And I'm thinking of a good way to implement them.

Currently I have these types of effects in game:

enum {
kBuffOnTurnStart,
kBuffOnAttack,
kBuffOnEffect,
kBuffAlwaysOn
};


I think that I can implement the recharging or expiring buffs as two part buffs - one is the "always on" portion and another a worker portion that will do it's action on turn start. For example, a shield buff has 8 points of damage it can absorb. After it does so, it can no longer absorb damage. At the start of next round the "worker" buff/action would find the linked shield buff and recharge it.

Alternatively, an "always on" buff has some action or behavior associated with it, which is executed on turn start.

What is a simple and clean way to implement auto refreshing or expiring buffs?

## 1 Answer

Treat the buffs as components. So you have a "always on" buff object, and you add it to your player. Each turn you fire an OnBeginTurn event to each buff, and they each manage their own behavior.

Examples: A buff that needs to expire will have an internal ticker that it will increment and remove itself when necessary. A buff that is always on will apply some effect to its parent player. A buff that triggers on an attack could receive an OnHandleAttack event, and apply some effect to the player or any sub-components of that player.

Of course I'm just basing this off my current component architecture, but the concept translates fairly easily.