By default, some objects in Unreal are tickable and some are not. UUserWidget
has a NativeTick
function, but a lot of other UWidget
subclasses are not.
Similarly when implementing Subsystem singletons I often want to make them tickable.
Making something tickable is a lot easier than I first thought. If your class
implements the FTickableGameObject
interface it will automatically be ticked!
It does not have to be a subclass of UObject
, it can just be a regular struct
or class
.
This sample code should be all you need to get started:
MyTickableThing.h
#pragma once
#include "CoreMinimal.h"
#include "Tickable.h"
class FMyTickableThing : public FTickableGameObject
{
public:
// FTickableGameObject Begin
virtual void Tick( float DeltaTime ) override;
virtual ETickableTickType GetTickableTickType() const override
{
return ETickableTickType::Always;
}
virtual TStatId GetStatId() const override
{
RETURN_QUICK_DECLARE_CYCLE_STAT( FMyTickableThing, STATGROUP_Tickables );
}
virtual bool IsTickableWhenPaused() const
{
return true;
}
virtual bool IsTickableInEditor() const
{
return false;
}
// FTickableGameObject End
private:
// The last frame number we were ticked.
// We don't want to tick multiple times per frame
uint32 LastFrameNumberWeTicked = INDEX_NONE;
};
MyTickableThing.cpp
#include "MyTickableThing.h"
void FMyTickableThing::Tick( float DeltaTime )
{
if ( LastFrameNumberWeTicked == GFrameCounter )
return;
// Do our tick
// ...
LastFrameNumberWeTicked = GFrameCounter;
}