Timers

From Unreal Documents: Timers allow actions to be performed – once or repeatedly – at designated intervals. They can be used to set actions to happen on a delay or over a period of time. For example, you may want to make the player invincible after obtaining a certain item and then revert after 10 seconds. Or, you may want to apply damage every second the player is in a room filled with toxic gas. Each of these can be achieved through the use of timers. To learn more about timers, check out the Unreal Engine Documents. 


Example

In this tutorial, we are going to set a timer that will change the look of a specific mesh within certain interval. Lets begin by creating a class based on Actor. Inside header we create a Scene Component and a Static mesh component. Then we create our timer, function that the timer will call and the value that keeps tracking current index.

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
USceneComponent* RootScene;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* MainMesh;
// Timer we are using
UPROPERTY()
FTimerHandle Timer_MeshChange;
// Timer will call this function
UFUNCTION()
void ChangeMesh();
// Count keeps track of the current index
//UPROPERTY()
UPROPERTY()
int32 Count;

In constructor we create our objects and set Count to 0 as default.

RootScene = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = RootScene;
MainMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MainMesh"));
MainMesh->SetupAttachment(RootScene);
Count = 0;

Next we are going to declare our function. In Function we are simply checking our current Count value and change mesh depending on it. Every time functin runs we add 1 to value. Our timer will call this function constantly.

void AC_EmptyActor::ChangeMesh()
{
if (Count == 0)
{	
// Load Static mesh from Content Browser	
UStaticMesh* NewMesh = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("/Game/StarterContent/Props/SM_Chair.SM_Chair")));
MainMesh->SetStaticMesh(NewMesh);
Count++;
// Exit the function
return;
}
if (Count == 1)
{
UStaticMesh* NewMesh = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("/Game/StarterContent/Props/SM_MatPreviewMesh_02.SM_MatPreviewMesh_02")));
MainMesh->SetStaticMesh(NewMesh);
Count++;
return;
}
if (Count == 2)
{
UStaticMesh* NewMesh = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("/Game/StarterContent/Props/SM_Rock.SM_Rock")));
MainMesh->SetStaticMesh(NewMesh);
Count = 0;
return;
}		
}

All that is left to do is to start our timer. We do that In BeginPlay().

// Set timer to be called every one second. Timer is looping and it takes one second before the timer activates first time.
GetWorldTimerManager().SetTimer(Timer_MeshChange, this, &AC_EmptyActor::ChangeMesh, 1.0f, true, -1.0f);

This is pretty simple if the function takes no parameters. If we are taking parameters however, we are going to use Dynamic timers.

FTimerDelegate TimerDynamic;
//Binding the function with specific values
TimerDynamic.BindUFunction(this, FName("ChangeMesh"), Count);
// Call the function every 1.5 seconds. 
GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDynamic, 1.5f, true, -1.0f);

Timers have a whole lot of different functions you can use. You can see al of them here

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *