UE4 定时器和字体组件、标识符BlueprintNativeEvent

  1. 使用定时器控制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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值