How to Debug via C++?

jara is right but I prefer to call it like this:

#if UE_BUILD_DEBUG
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "debug msg");
#endif

the -1 is the key, the 15.0f is how long to display it in seconds, the color is obviously the color to print, and the final param is obviously the message that you want printed which is an FString

using -1 as the key tells it that duplicate messages are ok. meaning that rather than update the same message it will create a new one and print that. If you want to reuse the same message just use a key != -1.

and normally I just use FString::FromInt or FString::SanatizeFloat for numbers. Personally I never check to make sure GEngine is valid before calling because I wrap this in a debug check so it will never make it to production and I also never need to worry about removing it. I also use the same #if to set things like bDrawDebugViewTrace and bShowHitBoxDebugInfo.

Here are the docs on this if you want to see how it works: UEngine::AddOnScreenDebugMessage | Unreal Engine Documentation

I want to print a string on the screen during runtime for debug via c++. But i can’t find true method for it.

AddOnScreenDebugMessage (usage below):

GEngine->AddOnScreenDebugMessage(-1,
5.f, FColor::Red, FString::Printf(TEXT(“StartTCPReceiver>>
Listen socket could not be created! ~>
%s %d”), *TheIP, ThePort));

It’s good practice to make sure GEngine is valid before you call the method.

You requested a method for on screen message for debugging, but I prefer using logs on the “output” window.

On screen messages will disappear, but you can browse the log messages indefinitely.

UE_LOG(LogTemp, Log, TEXT("Your message"));

You can learn more from THIS LINK:

I deleted the duplicated anwser

You can also run your project in VS debugger. Put breakpoints on points you interested in to check the states (you can do that by clicking on left side of code line you want to break) and then click on start debugging, or attach to project if you already running UE4. During a break you can explore variable states, as well as execute code step by step letting you see how your code is executed, debugger also let you notice on which point your code is crashing.

Debugger works the best if you got engine build from source in debug build configuration, you might have problems with development and release configurations. This also let you debug engine code.

#Full C++ Support Header File For You

I recently wrote a full header file for the community, that I use myself all the time, which lets you automatically print out the class and line number where you send a message to the screen from C++!

UE4 Forum Link ~ Print Class and Line Number along with your Screen Messages!

In this pic, AEVCoreDefense is my C++ Class, and the line number of the screen message is currently line 93 (automatically updates as your class grows in size!)

Ayy! thats my pic you have :slight_smile:

You can use:

#The right way according to UE docs

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("YOUR DEBUG MESSAGE"));

Note: You need to include Engine.h to your projects .cpp header. The EngineMinimal.h wont cut it.

See [Print To Viewport][1]
87077-

It’s really baffling to me that epic would destroy all of their old doc links in whatever migration they did. So many of these answers are now incomplete.

Yeah it’s really painful right now