UE4/变量、定时器和事件

官方文档,变量、定时器和事件

  • 找不到UTextRenderComponent类型

    在.h头文件上,添加上

#include "Components/TextRenderComponent.h"

但必须添加到#include “Countdown.generated.h”前面,因为.generated.h必须在最后包含。


教程上通过使用文本CountdownText,使其内容从3减到1,最后变为GO!,来体现定时器的效果。
1.
文本是UTextRenderComponent类型,在创建时,直接将根组件赋值为UTextRenderComponent,

CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
CountdownText->SetHorizontalAlignment(EHorizTextAligment::EHTA_Center);
CountdownText->SetWorldSize(150.0f);
RootComponent = CountdownText;

从创建一个UTextRenderComponent类型的CountdownText,然后设置水平对齐方式和文字大小。

设置CountdownText的显示文本,

CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));

FString是可变长的字符串。FromInt()函数的作用是将Integer类型的值转换为字符串。使用TEXT("xxx")也可获得FString类型的对象
2.

GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);

在该类的BeginPlay()函数中调用上句,总体的作用是:每隔一秒执行AdvanceTimer函数。相当于是启动了定时器,定时执行该函数,另外会返回定时器的句柄CountdownTimerHandle。

GetWorldTimerManager().ClearTimer(CountdownTimerHandle);

使用启动定时器时获得的句柄,作为上述函数调用的参数来停止指定的定时器

综上:
GetWorldTimerManager()
是在AActor中的普通方法,作用是Get the timer instance from the actors world。返回类型为FTimerManager类型,类FTimerManager中有SetTimer()【Sets a timer to call the given native function at a set interval.】的多个重载方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值