The other day I was looking at the “stats overlay” in my game and noticed that some behavior tree C++ tasks were at the top of the list. I noticed they were ticking on every game update but didn’t need to. I knew that BT_Service had an “interval” min and max you could set to vary the tick rate and was wondering if I could do the same for behavior tree tasks. Turns out you can, and it is very easy but not well documented IMO. I figured this out by looking at the source for base class for tasks: UBTTaskNode and also looking at an example in the Unreal source: BTTask_Wait that is used for the “Wait node” in the behavior tree built in tasks.
Steps
- Set bTickIntervals = true in the task constructor
This activates the variable tick rate behavior, though by default it will still tick every frame. You can actually see this is set to true in the BTService base class as it is inherited from both the task and service common parent node class.
You also need to be sure that bNotifyTick = true in the constructor, but if you are already ticking then this will already be the case.
- Call SetNextTickTime(NodeMemory, NextTickDeltaTime) in the ExecuteTask function override for an “In progress task”
This will set the first tick time for your TickNode override function override. Here NextTickDeltaTime will be the delta time for your first desired tick.
-
Call SetNextTickTime(NodeMemory, NextTickDeltaTime) in TickTask before returning if the task is still in progress.
-
(Optional but recommended) Add a UPROPERTY for tweaking the interval in the behavior tree property editor for your placed task node. If it makes sense for your task you can provide a min and max for the time and use FMath::RandRange to randomize within that time interval to avoid stacking your ticks in a single frame.
UPROPERTY(Category = "Tick", EditAnywhere)
float TickInterval{ 0.1f };
Here we set it to be 0.1 seconds by default. It’s important that the UPROPERTY be EditAnywhere and not EditDefaultsOnly as the latter do not show up as properties in the behavior tree editor.