forsene
forsene

Reputation: 31

UE4 Printing Location on Screen

I want to see my actor's location on the screen when the game starts.

I've tried this:

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));

But it gives a compile error such as:

Parsing headers for 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

Upvotes: 3

Views: 27232

Answers (2)

kaifas
kaifas

Reputation: 111

I don't know if you ever figured this out, but it turns out to return an FString to Printf, you have to dereference first:

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

This is briefly described at the unreal docs website towards the bottom.

I tested it and it seems to work.

Upvotes: 4

mattia biggeri
mattia biggeri

Reputation: 41

The Unreal Logger need a pointer to a FString, here is an esample on how to print a vector:

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

for all the logs examples and documentation you can look here: https://wiki.unrealengine.com/Logs,_Printing_Messages_To_Yourself_During_Runtime

Upvotes: 4

Related Questions