使用 C++ 内联 UnrealEngine UE_LOG 宏

Inlining the UnrealEngine UE_LOG macro with C++

我是 Unreal Engine 的新手,我正在尝试声明一个 inline 函数,如下所示:

void inline Print(const char* s) {
    UE_LOG(LogTemp, Warning, TEXT("%s"), s);
}

为了避免每次用LogTempWarning调用UE_LOG

例如调用Print("Hello")时,输出为LogTemp:Warning: 效汬o

我的猜测与 ASCII 编码有关,但我真的不确定。

我也尝试使用 reinterpret_cast 如下:

void inline Print(const char* s) {
UE_LOG(LogTemp, Warning, TEXT("%s"), reinterpret_cast<const TCHAR *>(s));
}

但结果相同。

我想知道正确的做法(我不想使用 MACRO 而不是内联函数),如果对乱码输出的原因有一个简单的解释,它也会是很有用。

你不能把const char*UE_LOG,它需要const TCHAR*,也不能这样reinterpret_cast,它需要转换,但让 FString 为您处理肮脏的工作。我想你可以选择以下之一:

1.

inline void Print(const FString& s)
{
    UE_LOG(LogTemp, Warning, TEXT("%s"), *s);
}

2.

inline void Print(const char* s)
{
    FString str(s);
    UE_LOG(LogTemp, Warning, TEXT("%s"), *str);
}