.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Engine/Classes/Kismet/KismetSystemLibrary.h"
#include "MyActor.generated.h"
UCLASS()
class MYPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
private:
UFUNCTION()
void OnLatentActionCompleted(int32 LinkID);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
}
.cpp:
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
void AMyActor::OnLatentActionCompleted(int32 LinkID)
{
UE_LOG(LogTemp, Log, TEXT("Delay Invoke!"));
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
FLatentActionInfo Ret(0, GetTypeHash(FGuid::NewGuid()), TEXT("OnLatentActionCompleted"), this);
UKismetSystemLibrary::Delay(this, 3.0, Ret);
//延迟3秒后,调用函数名为OnLatentActionCompleted的函数
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}