UE5 C++ 定时器 官方案例练习

一.这里参照 官方案例

Quick Start Guide to Variables Timers and Events in Unreal Engine CPP | 虚幻引擎 5.6 文档 | Epic Developer Community

二.

UPrimitiveComponent 是涉及渲染的组件,三角面的渲染几何等等。渲染线程它不能用U开头的东西,它会在这里把数据转换为F开头的结构体,把数据传递给渲染现场。

* PrimitiveComponents are SceneComponents that contain or generate some sort of geometry, generally to be rendered or used as collision data.。

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CountdownActor.generated.h"

UCLASS()
class THIRDLEARN_API ACountdownActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ACountdownActor();
	class UTextRenderComponent* CountdownText;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	void UpdateTimeDisplay();
	void AdvanceTimer();
	void CountdownHasFinished();

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
protected:
	int32 CountdonwTime = 3;
	FTimerHandle CountTimer;
	
};

UpdateTimeDisplay()更新,在BeginPlay一开始的时候就更新一次,并设置好定时器。

定时器,AdvanceTimer 减少计时,更新显示的位置。并判断是否要消除计时器,再最后清楚的时候,更新文本。

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "CountdownActor.h"
#include "Components/TextRenderComponent.h"

// Sets default values
ACountdownActor::ACountdownActor()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
	CountdownText = CreateDefaultSubobject<UTextRenderComponent>("CountdownTime");
	CountdownText->SetHorizontalAlignment(EHTA_Center);
	CountdownText->SetWorldSize(150);
	RootComponent = CountdownText;
	CountdonwTime = 3;
}

// Called when the game starts or when spawned
void ACountdownActor::BeginPlay()
{
	Super::BeginPlay();
	UpdateTimeDisplay();
	GetWorldTimerManager().SetTimer(CountTimer, this, &ACountdownActor::AdvanceTimer, 1.0f, true);
}

// Called every frame
void ACountdownActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ACountdownActor::UpdateTimeDisplay()
{
	CountdownText->SetText(FText::FromString(FString::FromInt(FMath::Max(CountdonwTime, 0))));
}

void ACountdownActor::AdvanceTimer()
{
	--CountdonwTime;
	UpdateTimeDisplay();
	if (CountdonwTime < 0)
	{
		GetWorldTimerManager().ClearTimer(CountTimer);
		CountdownHasFinished();
	}
}

void ACountdownActor::CountdownHasFinished()
{
	CountdownText->SetText(FText::FromString(TEXT("GO")));
}


// Called every frame
void ACountdownActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ACountdownActor::UpdateTimeDisplay()
{
	CountdownText->SetText(FText::FromString(FString::FromInt(FMath::Max(CountdonwTime,0))));
}

void ACountdownActor::AdvanceTimer()
{ 
	--CountdonwTime;
}

void ACountdownActor::CountdownHasFinished()
{
	CountdownText->SetText(FText::FromString(TEXT("GO")));
}
相关推荐
zhangzhangkeji5 小时前
UE5 C++(UObject 的实例化 19-3):类 UWorld,模板函数 NewObject<>(...),
ue5
zhangzhangkeji16 小时前
UE5 C++(14-1):UPROPERTY 宏、属性说明符和元数据说明符, visible,edit,Blueprint读写,Category 存储目录
ue5
Zhichao_9721 小时前
【UE5.3 C++】ARPG游戏 05-准备角色攻击的武器和动画
游戏·ue5
zhangzhangkeji1 天前
UE5 C++(15-3):UFUNCTION 的 meta 元数据, DisplayName ,同 UPROPERTY 的 meta
ue5
夜色。1 天前
UE5 Error LNK2019 编译异常修复备忘
ue5
陈友松2 天前
UE5 表格文件动态导入导出插件
ue5
zhangzhangkeji2 天前
UE5 C++(17):结构体,USTRUCT(BlueprintType) struct FmyStruct ,必须用 F 开头
ue5
碎梦人2 天前
基于UE5开发的pico大空间项目开发之虚拟坐标系与真实坐标系的对应关系
ue5·lbe·大空间坐标系对应关系
zhangzhangkeji2 天前
UE5 C++(16):枚举 UENUM,UENUM(BlueprintType) 使其成为蓝图里的类型。模板类 TEnumAsByte<enum 类型>
ue5
zhangzhangkeji2 天前
UE5 C++(UObject 的实例化 19-2):UObject 类的继承关系
ue5