UE4在屏幕上的打印位置

UE4 Printing Location on Screen

本文关键字:打印 位置 屏幕 UE4      更新时间:2023-10-16

我想在游戏开始时在屏幕上看到我的演员的位置。

我试过这个:

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:Users840-g5DocumentsUnreal ProjectsSHOULDWORKSHOULDWORK.uproject" "C:Users840-g5DocumentsUnreal ProjectsSHOULDWORKIntermediateBuildWin64SHOULDWORKEditorDevelopmentSHOULDWORKEditor.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 Studio2019EnterpriseVCToolsMSVC14.22.27905) and Windows 10.0.18362.0 SDK (C:Program Files (x86)Windows Kits10).
Building 4 actions with 8 processes...
[1/4] MyActor.cpp
C:Program FilesEpic GamesUE_4.22EngineSourceRuntimeCorePublicContainers/UnrealString.h(1393) : error C2338: Invalid argument(s) passed to FString::Printf
C:Users840-g5DocumentsUnreal ProjectsSHOULDWORKSourceSHOULDWORKMyActor.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 FilesEpic GamesUE_4.22EngineSourceRuntimeCorePublicWindows/WindowsPlatformNamedPipe.h(12): note: see declaration of 'FString'
C:Program FilesEpic GamesUE_4.22EngineSourceRuntimeCorePublicContainers/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 FilesEpic GamesUE_4.22EngineSourceRuntimeCorePublicContainers/UnrealString.h(73): note: see declaration of 'FString::FString'
C:Program FilesEpic GamesUE_4.22EngineSourceRuntimeCorePublicContainers/UnrealString.h(1395): note: 'FString::FString' is non-trivial
C:Program FilesEpic GamesUE_4.22EngineSourceRuntimeCorePublicContainers/UnrealString.h(1395) : error C4840: non-portable use of class 'FString' as an argument to a variadic function

虚幻记录器需要一个指向FString的指针,下面是一个关于如何打印矢量的电子示例:

//"MyCharacter's Location is %s"
UE_LOG(YourLog,Warning,TEXT("MyCharacter's Location is %s"), 
*MyCharacter->GetActorLocation().ToString());

有关所有日志示例和文档,您可以在此处查看: https://wiki.unrealengine.com/Logs,_Printing_Messages_To_Yourself_During_Runtime

我不知道你是否想通了这一点,但事实证明,将 FString 返回给 Printf,你必须先取消引用:

GEngine->AddOnScreenDebugMessage(-1,200,FColor::Green,FString::Printf(TEXT("Hello %s"),*GetActorLocation().ToString()));

这在虚幻文档网站底部有简要描述。

我测试了它,它似乎有效。