We are going to write posts teaching different aspects of designing with our plugins. We add all of them to the cookbooks section of our documentation for the different products as well.

Once in a while you need to implement a cooldown for an action so it doesn’t execute for x number of seconds after it executes once. To implement such a mechanic you need to remember when an action got executed and how much you should wait before you can execute it again. To do this the action should have a consideration which returns 0 before the cooldown time finishes and returns 1 after that so the rest of the score is not affected by it.

It can be easily hardcoded if you want to do it for a specific action. Let’s call the action Attack1 and let’s say we want to execute it at most once per 5 seconds so with 5 seconds of cooldown. You can store a last executed time in the action itself and set it to current time when action gets activated and then have a consideration which subtracts the current time from the stored time in the action and if less than 5 returns 0 and otherwise returns 1. The curve for the consideration should return 0 for 0 and 1 for one as well so a linear curve would be perfect.

The method to override in Unity is OnStart() and in Unreal it is OnActivated(). These get called when an action is selected to execute once.

This said you can implement it more flexibly by using a blackboard and in this way you can have an item which if the character eats or an action which he does and reduces the cooldown. For example he can eat a potion to reduce his tiredness and Attack1 requires tiredness of less than 10. Let’s say tiredness is between 0 and 100 and it decreases when not attack 15 per second. Attack1 increases tiredness by 90 as well. There is also a pray action which has a 50% chance of reducing tiredness to 0 or increasing it by 10.

There is a consideration for the Attack1 which checks the blackboard float key of tiredness and if less than 10 returns 1 and otherwise returns 0. A code in a components of the agent decreases tiredness by 15 per second if the attack actions are not being executed. Eat fruit action decreases tiredness by say 50 and its consideration give it a high score when you are tired and a fruit is close by. The pray action gets a random number and if less than 0.5 then it increases tiredness by 5 and otherwise sets it to 0. Its consideration will see if there is no fruits close by and we are tired and the situation requires a not tired character.

Now to make it more interesting, some characters which have a more religious/spiritual attribute can use this more effectively and we can have a cool down for praying itself so an agent can use it once per few battles to not make the battle scenes look like a church in chaos. Also if this was based on player choices then it could improve based on how many temples a player built in a city building game or how much did the player sent his army to rick havoc on cities in a strategic game. To be clear, sending armies to ruin cities should decrease the effectiveness of prayers and other spiritual spells and actions in most games unless you are making a game about an evil God.