[UE4]Tick Function Notes
Keywords: UE4, Tick Function Notes
Enable & Disable Tick
Trigger Tick at game beginning.
Constructor:
// Activate ticking in order to update the cursor every frame.
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = true;
Trigger Tick at runtime.
Constructor:
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = false;
Non-constructor:
SetActorTickEnabled(true);
RegisterComponent() must be executed before AttachToComponent()
If a Component created by NewObject<>()
at run-time, RegisterComponent()
must be executed before AttachToComponent()
, otherwise TickComponent()
would not be triggered on attaching finished.
ResisterComponent()
only need to execute when using NewObject
to create Component, Component created by SpawnActor()
or ConstructorHelpers
doesn’t need to execute it.
Trigger of BeginPlay() and Tick()
BeginPlay()
and Tick()
would be triggered if an Actor created by NewObject<>()
before it’s added to scene(GameWorld), even PrimaryActorTick.bCanEverTick = true;
was executed.
How to set callback function of next tick
In GameThread.
GetWorld()->GetTimerManager().SetTimerForNextTick(FTimerDelegate::CreateUObject(this, &AMyCharacter::TestCallback));
or:
GetWorld()->GetTimerManager().SetTimerForNextTick(FTimerDelegate::CreateWeakLambda(this, []()
{
//some codes
}));
or:
FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, []
{
//some codes
}, 0.0001f, false, 0.f);
In non-GameThread:
FTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateLambda([arg1, arg2](float dts) {
//...
return true;
}));
You can set parameter Delay
(default value is 0 which means “next frame”) if want to delay a certain time.
How to enable Tick function in Editor
Tick()
will only be executed at game run-time by default, if you want to execute Tick()
in Editor, do that in the following way:
For Actor:
Override ShouldTickIfViewportsOnly()
, and return true
, return value is false
by default.
bool Actor::ShouldTickIfViewportsOnly() const
{
return true;
}
For ActorComponent:
Has bTickInEditor
set be true
to trigger TickComponent()
, it’s false by default.
Even you have done it in above way, Tick()
will only trigger when open Actor Blueprint in editor, it would not be triggered at Editor startup.
Reduce tick frequency
CPP:
//Component
PrimaryComponentTick.TickInterval = 1.0f;
//Actor
PrimaryActorTick.TickInterval = 1.0f;
BP Node:
Set Actor Tick Interval
Tick Issues
GameThread time cost increased heavily
Found on 2019-02-16.
UE4 Editor bug: GameThread time cost increased heavily when there’re some uninitialized properties in Blueprint.
Problem:
there’re some Object Reference type properties in Blueprint that are uninitialized, e.g. ParticleComponent, and these properties are referenced in Tick Function of Blueprint, when game running, GameThread time cost increased heavily.
Solution:
Remove those uninitialized properties in Blueprint.
Tick doesn’t work while SetActorTickInterval and SetGlobalTimeDilation
Issue:
if SetActorTickInterval(1.f / 30);
and UGameplayStatics::SetGlobalTimeDilation(GetWorld(), 0.f);
, AActor::Tick()
would not work at all.
The advantage of a bad memory is that one enjoys several times the same good things for the first time. ― Friedrich Nietzsche