AjapaReporter

Home life · Updated

How to Print String in C++ Unreal Engine

Time it takes5 min
Steps5in the list below
LevelEasyno special skills
Official sourceEpic Games Unreal Engine Documentationcheck it for changes

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

  1. 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 Actor or GameModeBase.
  2. 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".
  3. Add code to print to the Output Log – Inside a function like BeginPlay() or Tick(), add: UE_LOG(LogTemp, Warning, TEXT("Your message here"));. Replace LogTemp with a custom log category if you have one, and Warning with Log, Error, or Fatal depending on severity. To print a variable, use format specifiers like %s for strings, %d for integers, and %f for floats. Example: UE_LOG(LogTemp, Warning, TEXT("Health: %d"), Health);.
  4. 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, use FString::Printf: GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Score: %d"), Score));.
  5. 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.h when using GEngine – results in a compile error.
  • Using %s with a FString directly – you need *FString or use %s with TCHAR*.
  • Not adding a semicolon at the end of UE_LOG or AddOnScreenDebugMessage statements.
  • Calling GEngine in a context where it might be null (e.g., in a constructor) – use it after initialization or check if GEngine is not null.
  • Using UE_LOG with a category that doesn’t exist – stick to LogTemp for quick tests.

Tips

  • Use FString::Printf for complex formatting – it works like printf in C.
  • Use LogTemp for temporary debug messages; create a custom category for production logging.
  • To print a boolean, use %d with true/false converts to 1/0, or use %s with (bFlag ? TEXT("true") : TEXT("false")).
  • For vectors, use %s with *MyVector.ToString().
  • Use FColor::Red for errors, Yellow for warnings, Green for 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 GEngine is 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 %s with a FString without dereferencing. Use *MyString or %s with TEXT("...").
  • 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.

Sources: Epic Games Unreal Engine Documentation. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.