UE4 延时调用(定时器)

  • //.h
    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/GameModeBase.h"
    #include "PersonMoveGameModeBase.generated.h"
    
    /**
     * 
     */
    UCLASS()
    class PERSONMOVE_API APersonMoveGameModeBase : public AGameModeBase
    {
    	GENERATED_BODY()
    	
    public:
    	//GameMode 的开始是StartPlay
    	virtual void StartPlay() override;
    	
    protected:
    
    	APersonMoveGameModeBase();
    
    	FTimerHandle TimerHandle_TimeCount;
    	void CutTime();
    
    	UPROPERTY(BlueprintReadOnly,Category="UI")
    	int CountTime;
    };
    
  • //.cpp
    // Fill out your copyright notice in the Description page of Project Settings.
    
    #include "PersonMoveGameModeBase.h"
    #include "TimerManager.h"  //定时器
    
    void APersonMoveGameModeBase :: StartPlay()
    {
    	Super::StartPlay();
    
    	//SetTimer(时间句柄,作用在哪个物体身上,调用的函数,多长时间调用一次,是否要循环,几秒后调用)
      GetWorldTimerManager().SetTimer(TimerHandle_TimeCount, this, &APersonMoveGameModeBase::CutTime, 1.0f, true, 1.0f);
    
    }
    
    APersonMoveGameModeBase::APersonMoveGameModeBase()
    {
    	CountTime = 3;
    	
    }
    
    void APersonMoveGameModeBase::CutTime()
    {
    	CountTime--;
    	UE_LOG(LogTemp, Warning, TEXT("%s"), *FString::SanitizeFloat(CountTime));
    	if (CountTime == 0)
    	{
    		//清除时间
    		GetWorldTimerManager().ClearTimer(TimerHandle_TimeCount);
    	}
    	
    }
    

猜你喜欢

转载自blog.csdn.net/m0_37981386/article/details/83214525