UE4在屏幕上打印位置

3

当游戏开始时,我希望能够在屏幕上看到我的演员位置。

我尝试了以下方法:

GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("LOCATION: %f"), Location));

.cpp

    FVector Location = GetActorLocation();

    FRotator Rotation = GetActorRotation();
    
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("LOCATION: %f"), Location));

但它会出现编译错误,例如:

正在解析 SHOULDWORKEditor 的头文件

Running UnrealHeaderTool "C:\Users\840-g5\Documents\Unreal Projects\SHOULDWORK\SHOULDWORK.uproject" "C:\Users\840-g5\Documents\Unreal Projects\SHOULDWORK\Intermediate\Build\Win64\SHOULDWORKEditor\Development\SHOULDWORKEditor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
Reflection code generated for SHOULDWORKEditor in 7,2799668 seconds
Using Visual Studio 2019 14.22.27905 toolchain (C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.22.27905) and Windows 10.0.18362.0 SDK (C:\Program Files (x86)\Windows Kits\10).
Building 4 actions with 8 processes...
 [1/4] MyActor.cpp
  C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Containers/UnrealString.h(1393) : error C2338: Invalid argument(s) passed to FString::Printf
 C:\Users\840-g5\Documents\Unreal Projects\SHOULDWORK\Source\SHOULDWORK\MyActor.cpp(26): note: see reference to function template instantiation 'FString FString::Printf<wchar_t[13],FVector>(const FmtType (&),FVector)' being compiled
         [
         with
             FmtType=wchar_t [13]
         ]

EDIT Tried this:
Location.ToString()

and got the same error

and tried this:
   
FString LocString = Location.ToString();

GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("LOCATION: %s"), LocString));

and i got the error:
 C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Windows/WindowsPlatformNamedPipe.h(12): note: see declaration of 'FString'
 C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Containers/UnrealString.h(1395): note: the constructor and destructor will not be called; a bitwise copy of the class will be passed as the argument
 C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Containers/UnrealString.h(73): note: see declaration of 'FString::FString'
 C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Containers/UnrealString.h(1395): note: 'FString::FString' is non-trivial
  C:\Program Files\Epic Games\UE_4.22\Engine\Source\Runtime\Core\Public\Containers/UnrealString.h(1395) : error C4840: non-portable use of class 'FString' as an argument to a variadic function

Location视为浮点数似乎非常可疑。请包含完整的错误消息。 - molbdnilo
C:\Users\840-g5\Documents\Unreal Projects\SHOULDWORK\Source\SHOULDWORK\MyActor.cpp(26): 注意:查看正在编译的函数模板实例化 'FString FString::Printf<wchar_t[13],FVector>(const FmtType (&),FVector)' 的参考资料 其中 [ FmtType=wchar_t [13] ] - forsene
这仅仅是信息的一部分,而且信息应该放在问题中,而不是作为评论。(按下问题下方的“编辑”来编辑它。) - molbdnilo
我再次编辑了整个输出日志。 - forsene
你试过使用 location.ToString() 了吗? - VectorX
现在试了一下,谢谢你的建议。 - forsene
2个回答

4
我不知道你是否已经明白了这一点,但是为了将FString返回给Printf,您需要先解除引用:
GEngine->AddOnScreenDebugMessage(-1,200,FColor::Green,FString::Printf(TEXT("Hello %s"),*GetActorLocation().ToString()));

这在虚幻引擎文档网站的底部简要介绍了。我已经测试过它,似乎可以工作。

糟糕!看起来@mattia-biggeri已经回答了指针部分... - kaifas

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接