使用C++创建一个蓝图函数

41 篇文章 3 订阅

在实际的开发需求可能会遇到一些无法用蓝图实现的功能,或者实现起来比较麻烦,更或者是一些长期不动的逻辑而不想创建在蓝图中,那么就需要将一些逻辑写在C++里,这些逻辑可能是比如玩家的Input,基本上不会变的,可以写在C++里,今天我来创建一个获取本地时间的一个蓝图函数,首先创建一个C++ Class为Blueprint Library,创建好后,在头文件的GENERATED_BODY()下创建一个函数,代码如下:

UFUNCTION(BlueprintCallable, BlueprintPure, Category = "MDSBPLibrary")
		static FString GetCurrentOSTime(
			int32& MilliSeconds,
			int32& Seconds,
			int32& Minutes,
			int32& Hours12,
			int32& Hours24,
			int32& Day,
			int32& Month,
			int32& Year
		);

然后在CPP中写入以下代码:

FString UMDSBPLibrary::GetCurrentOSTime(
	int32& MilliSeconds,
	int32& Seconds,
	int32& Minutes,
	int32& Hours12,
	int32& Hours24,
	int32& Day,
	int32& Month,
	int32& Year
) {
	const FDateTime Now = FDateTime::Now();

	MilliSeconds = Now.GetMillisecond();
	Seconds = Now.GetSecond();
	Minutes = Now.GetMinute();
	Hours12 = Now.GetHour12();
	Hours24 = Now.GetHour(); //24
	Day = Now.GetDay();
	Month = Now.GetMonth();
	Year = Now.GetYear();

	//返回当前系统的所有时间信息
	FString NowWithMS = Now.ToString();
	NowWithMS += "." + FString::FromInt(MilliSeconds);
	return NowWithMS;
}

然后构建,在UE蓝图中搜索GetCurrentOSTime极客使用该函数,如下图所示:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值