官方文档,变量、定时器和事件
找不到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.】的多个重载方法。