//AMyActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class GAMECPP_API AMyActor : public AActor
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
private:
struct FTimerHandle ScreenTimerHandle;//句柄
int32 GameTime;
void TimerCallback();//回调函数
};
//AMyActor.cpp
#include "MyActor.h"
#include "TimerManager.h"
void AMyActor::BeginPlay()
{
Super::BeginPlay();
GameTime = 0;
UWorld* world = this->GetWorld();//world可能为空,所以要判断一下
if(world)
{
world->GetTimerManager().ClearTimer(ScreenTimerHandle);
world->GetTimerManager().SetTimer(ScreenTimerHandle,this,&AMyActor::TimerCallback,2.0f,true);
}
}
void AMyActor::TimerCallback()
{
GEngine->AddOnScreenDebugMessage(1,1.0,FColor::Green,FString::FromInt(GameTime++));
}