- 使用定时器控制UTextRenderComponent上的字体显示;使用标识符BlueprintNativeEvent实现C++函数和蓝图函数的调用
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TimerActor.generated.h"
UCLASS()
class TRUEFPSPROJECT_API ATimerActor : public AActor {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATimerActor();
class UTextRenderComponent* timerTextComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AAA")
int timeCount = 10;
FTimerHandle timerHandle;
/**
BlueprintCallable函数是用C++编写的,可以从蓝图图表中调用,但如果不编辑C++代码,就无法更改或覆盖。
以这种方式标记的函数通常是已编程供非程序员使用的功能,但不应更改或更改没有意义。一个简单的例子是任何类型的数学函数。
BlueprintImplementableEvent函数在C++头 (.h) 文件中设置,但函数的主体完全在蓝图图中编写,而不是C++中。
创建这些通常使非程序员能够对没有预期默认操作或标准行为的特殊情况创建自定义反应。这方面的一个例子可能是在宇宙飞船游戏中道具触摸玩家的飞船时发生的事件。
BlueprintNativeEvent函数就像 and 函数的组合。它们具有以C++编程的默认行为,但可以通过在蓝图图表中覆盖来补充或替换这些行为。
在对这些代码进行编程时,C++代码始终进入虚函数,并在名称末尾添加_Implementation,
*/
UFUNCTION(BlueprintNativeEvent)
void timerFinish();
virtual void timerFinish_Implementation();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
void updateTimer();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "TimerActor.h"
#include "Components/TextRenderComponent.h"
// Sets default values
ATimerActor::ATimerActor() {
// 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;
timerTextComponent = CreateDefaultSubobject<UTextRenderComponent>(TEXT("timerTextComponent"));
//设置对其方式
timerTextComponent->SetHorizontalAlignment(EHorizTextAligment::EHTA_Center);
//设置字体大小
timerTextComponent->SetWorldSize(80.0);
//设置根组件
RootComponent = timerTextComponent;
}
void ATimerActor::timerFinish_Implementation() {
timerTextComponent->SetText(TEXT("GO!"));
}
// Called when the game starts or when spawned
void ATimerActor::BeginPlay() {
Super::BeginPlay();
/**
*void SetTimer
(
FTimerHandleNEW! & InOutHandle, //定时器句柄
UserClass * InObj, //调用对象
typename FTimerDelegate::TUObjectMethodDelegate_Const< UserClass >::FMethodPtr InTimerMethod,//调用的函数指针
float InRate,//间隔时长
bool InbLoop, //是否循环调用 true-每隔InRate时长就会被调用一次 false-只会被调用一次
float InFirstDelay //第一次被调用的时间间隔
)
*/
GetWorldTimerManager().SetTimer(timerHandle, this, &ATimerActor::updateTimer, 1, true, 1.0);
}
// Called every frame
void ATimerActor::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
void ATimerActor::updateTimer() {
timeCount--;
timerTextComponent->SetText(FString::FromInt(FMath::Max(timeCount, 0)));
if (timeCount <= 0) {
timerFinish();
GetWorldTimerManager().ClearTimer(timerHandle);
}
}
当定时器运行结束的时候,字体显示GO,并且还有个爆炸效果,这些功能需要在蓝图上实现。
创建ATimerActor对应的蓝图
上图标红的部分就是调用C++中的接口
aaa