#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Actor.h"#include"Runtime/Engine/Classes/Kismet/KismetSystemLibrary.h"#include"MyActor.generated.h"UCLASS()classMYPROJECT_API AMyActor :public AActor
{GENERATED_BODY()public:// Sets default values for this actor's propertiesAMyActor();private:UFUNCTION()voidOnLatentActionCompleted(int32 LinkID);protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay()override;public:// Called every framevirtualvoidTick(float DeltaTime)override;};
.cpp:
#include"MyActor.h"// Sets default valuesAMyActor::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;}voidAMyActor::OnLatentActionCompleted(int32 LinkID){UE_LOG(LogTemp, Log,TEXT("Delay Invoke!"));}// Called when the game starts or when spawnedvoidAMyActor::BeginPlay(){Super::BeginPlay();
FLatentActionInfo Ret(0,GetTypeHash(FGuid::NewGuid()),TEXT("OnLatentActionCompleted"),this);UKismetSystemLibrary::Delay(this,3.0, Ret);//延迟3秒后,调用函数名为OnLatentActionCompleted的函数}// Called every framevoidAMyActor::Tick(float DeltaTime){Super::Tick(DeltaTime);}