UE5 How to make Tickable Objects

In Unreal Engine 5 , you can tick any c++ class object. It does not have to be UObject. Here is how you do it:

  1. Have your class inherit from the FTickableGameObject class.
  2. Override the virtual function “Tick” in your class.
  3. In the “Tick” function, implement the logic that should be executed each tick.


That’s basically it. But let’s show a code example on how to make C++ Classes tick in unreal engine.

				
					class MyTickableClass : public FTickableGameObject
{

private:
	// Track The last frame number we were ticked.
	uint32 LastTickFrame = INDEX_NONE;

public:
    

virtual void Tick(float DeltaTime) override
{
    // Implement the logic that should be executed each tick
    
    if ( LastTickFrame == GFrameCounter )
	return;

    // Do our tick
	// ...

    LastTickFrame = GFrameCounter;
}



virtual ETickableTickType GetTickableTickType() const override
{
	return ETickableTickType::Always;
}

virtual TStatId GetStatId() const override
{
    RETURN_QUICK_DECLARE_CYCLE_STAT(MyTickableClass, STATGROUP_Tickables);
}
};

				
			

Should we keep track of the last frame we ticked? is it necessary?

It is possible that a tickable object’s Tick function could be called multiple times in the same frame so to be on the safe side and ensure we only tick once, it’s wise to ensure we only tick once per frame by checking our counter. We do this by enlisting the help of the GFrameCounter variable.

What is GFrameCounter?

GFrameCounter is a global variable in Unreal Engine that holds the current frame number. It is incremented every frame and can be used to keep track of the current frame number in your game.

 

When you use GFrameCounter in your Tick function, you’re storing the current frame number so that you can compare it with the last frame number ticked in your IsTickable function. This way you can ensure that the tick function is only executed once per frame.

What does GetTickableTickType function do?

The FTickableGameObject class has a virtual function GetTickableTickType which you can override to specify how often your class should be ticked. The default value is ETickableTickType::Always, which means that the class will be ticked every frame.

You have several options for the tick type (Always,Conditional,Never) that you can pick based on the needs of your class.

What about ETickableTickType::Conditional

When a tickable object is set to ETickableTickType::Conditional, it means that the object’s IsTickable function will be called to determine if the object should be ticked or not. If the IsTickable function returns true, the Tick function will be called. If it returns false, the Tick function will not be called.

 

The IsTickable function is a virtual function in FTickableGameObject that you can override in your class to check the state of your object and determine if it should be ticked or not. This function is called before the Tick function, so you can use it to control when your object should be ticked.

 

ETickableTickType::Conditional is useful in cases where you only want your object to be ticked when certain conditions are met, or if you want to save CPU time by not ticking an object that doesn’t need to be ticked.

 

Please keep in mind that when using ETickableTickType::Conditional, you should make sure that IsTickable function is quick and it’s not doing any heavy computation, otherwise it will have an impact on performance. Also, it’s worth noting that you can use the ETickableTickType::Conditional in conjunction with keeping track of the last frame ticked, to make sure that the object is ticked only once per frame.

The GetStatId function and its purpose

The GetStatId function is used to assign a unique identification for a tickable object. It is part of the FTickableGameObject interface and is used by Unreal Engine’s performance tracking system to collect and display statistics about the performance of tickable objects.

 

The GetStatId function returns a TStatId struct, which is a unique identifier for the tickable object. This struct is used internally by Unreal Engine to identify the tickable object and track its performance.

 

The RETURN_QUICK_DECLARE_CYCLE_STAT macro is a shorthand way of creating a unique TStatId struct for the tickable object. The first parameter is the name of the class, and the second parameter is the group to which the stat belongs.

 

It’s important to keep in mind that the GetStatId function is only used for performance tracking purposes and it’s not required for the correct functioning of your tickable object.

What is StatGroup_Tickables and what are its alternatives

STATGROUP_Tickables is a predefined constant in Unreal Engine that represents a group of statistics related to tickable objects.

 

It is used as the second parameter of the RETURN_QUICK_DECLARE_CYCLE_STAT macro in the GetStatId function to assign a unique identification for a tickable object and to group it with other tickable objects’ statistics.

 

In the Unreal Engine’s statistics view, you can filter the statistics by group, and by using STATGROUP_Tickables, you will be able to see statistics specifically for your tickable object.

Conlcusion

We hope this guide helped you get started with making C++ classes tickable in unreal engine without making them AActors or AActorComponents.