Home life · Updated
How to Print String in C++ Unreal Engine
Quick answer Use UE_LOG with the LogTemp category and the Warning verbosity to print a string to the Output Log, or AddOnScreenDebugMessage to show it on the game viewport.
What You’ll Need
- A C++ class in your Unreal Engine project (e.g., an Actor or GameMode).
- Visual Studio or another IDE configured for Unreal Engine development.
- Basic familiarity with the Unreal Editor and C++ syntax.
Step-by-Step
- Open your C++ class – In the Unreal Editor, go to Tools → New C++ Class to create a new class if you don’t have one, or open an existing class in your IDE. Choose a base class like
ActororGameModeBase. - Include the necessary headers – At the top of your .cpp file, ensure you have
#include "Engine/Engine.h"for screen debug messages and#include "CoreMinimal.h"(usually already present). For UE_LOG, you may also need#include "Logging/LogMacros.h". - Add code to print to the Output Log – Inside a function like
BeginPlay()orTick(), add:UE_LOG(LogTemp, Warning, TEXT("Your message here"));. ReplaceLogTempwith a custom log category if you have one, andWarningwithLog,Error, orFataldepending on severity. To print a variable, use format specifiers like%sfor strings,%dfor integers, and%ffor floats. Example:UE_LOG(LogTemp, Warning, TEXT("Health: %d"), Health);. - Add code to print to the viewport – For on-screen text, use:
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Your message"));. The first parameter is a key (use -1 for a unique message), the second is display duration in seconds, and the third is color. To include variables, useFString::Printf:GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Score: %d"), Score));. - Compile and test – Save your code, compile in Visual Studio (Ctrl+Shift+B), then return to the Unreal Editor. Click Compile in the editor toolbar. Place your class in the level (if it’s an Actor) or set it as the GameMode, then press Play. Check the Output Log (Window → Developer Tools → Output Log) or the viewport for your message.
Common Mistakes
- Forgetting to include
Engine/Engine.hwhen usingGEngine– results in a compile error. - Using
%swith aFStringdirectly – you need*FStringor use%swithTCHAR*. - Not adding a semicolon at the end of UE_LOG or AddOnScreenDebugMessage statements.
- Calling
GEnginein a context where it might be null (e.g., in a constructor) – use it after initialization or check ifGEngineis not null. - Using
UE_LOGwith a category that doesn’t exist – stick toLogTempfor quick tests.
Tips
- Use
FString::Printffor complex formatting – it works like printf in C. - Use
LogTempfor temporary debug messages; create a custom category for production logging. - To print a boolean, use
%dwithtrue/falseconverts to 1/0, or use%swith(bFlag ? TEXT("true") : TEXT("false")). - For vectors, use
%swith*MyVector.ToString(). - Use
FColor::Redfor errors,Yellowfor warnings,Greenfor success.
Troubleshooting
- No output appears – Check that you are in Play mode; the Output Log may not show in editor without playing. Also verify the verbosity level (e.g., Warning) is enabled in the Output Log filter.
- Compilation errors – Ensure you have included the correct headers and that your code is inside a member function of a UCLASS. Check for missing semicolons or parentheses.
- Screen message not showing – Make sure
GEngineis not null (it can be null in commandlet or server contexts). Also check that the duration is not zero and that the message key is unique if you want it to persist. - String formatting issues – If you see garbled text, you likely used
%swith aFStringwithout dereferencing. Use*MyStringor%swithTEXT("..."). - Log spam – If you print every frame in Tick, use a timer or condition to avoid flooding the log.
Frequently Asked Questions
How do I print a float with two decimal places?
Use %.2f in the format string: UE_LOG(LogTemp, Warning, TEXT("Value: %.2f"), MyFloat);.
Can I print to the screen from a Blueprint?
Yes, use the Print String node in Blueprints. It outputs to both the viewport and the Output Log.
What is the difference between UE_LOG and AddOnScreenDebugMessage?
UE_LOG writes to the Output Log only, while AddOnScreenDebugMessage displays on the game viewport. Use UE_LOG for permanent logs and screen messages for temporary debugging.
How do I print a FVector?
Use UE_LOG(LogTemp, Warning, TEXT("Location: %s"), *MyVector.ToString()); or AddOnScreenDebugMessage(-1, 5.f, FColor::Green, MyVector.ToString());.
Why is my message not showing in the Output Log?
Check the log verbosity filter – ensure that the level you used (e.g., Warning) is enabled. Also, verify that you are looking at the correct log (Output Log, not the editor log).
Can I print to a file?
Yes, use FString::Printf and write to a file with FFileHelper::SaveStringToFile, but for simple debugging UE_LOG is easier.
Related
- How to Clean Xbox One: Safe Step-by-Step Guide
- How to Print Royal Caribbean Luggage Tags
- How to Clean Xbox Elite Controller Series 2
Sources: Epic Games Unreal Engine Documentation. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.