在实际的开发需求可能会遇到一些无法用蓝图实现的功能,或者实现起来比较麻烦,更或者是一些长期不动的逻辑而不想创建在蓝图中,那么就需要将一些逻辑写在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极客使用该函数,如下图所示: