Print to Screen using C++

Hello,

I am trying to do something quite simple. In a blank Unreal 4.12 Preview 3 project I would like to print text to the screen on runtime using c++.

I have found many posts relating to posting text to log files, and posting text to the screen using
GEngine->AddOnScreenDebugMessage.

However I am unsure where this code needs to go in order to get the message to display when I run the project.

Any advice?

4 Likes

GEngine is available to you from wherever you please; being global (that’s why it’s prefixed with a capital G) you can access it anywhere you like, just make sure you check whether it is not null first.

Simple syntax:

if(GEngine)
     GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Some debug message!"));	

Something more complex:

if(GEngine)
     GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("World delta for current frame equals %f"), GetWorld()->TimeSeconds));

See EngineGlobals.h for how GEngine is defined to learn more.

27 Likes

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

Handy^ : Debug messages show only on debug builds :slight_smile:

4 Likes

you can also define your own macro to make that convenient to use in everyday life:

  • in some file (i’d suggest creating a new one like “macros.h”) paste the next:

#define D(x) if(GEngine){GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, TEXT(x));}

  • include this file where you wish to use this macro

now you can simply D(“i’m a message and will be displayed”)

good luck :slight_smile:

7 Likes

Just curious, what’s the compile time hit when using preprocessor directives to tag debugging code. Like what if I had thousands of these in my project? Would it have a noticeable effect on compile time?

your welcome. since then i’ve discovered a way to use bind screen messages to std::cout, so it might be even more convenient to you depending on your needs. lookup “std::cout screen messages” in answerhub. i don’t remember where have i posted that and sadly no time to search currently

Very handy, thanks!

Don’t forget to #include "Engine/GameEngine.h".

3 Likes

Here’s a macro that creates something more like printf (e.g. DEBUGMESSAGE(“This is formatted %i %f”, 100, 99.999)):

#define DEBUGMESSAGE(x, ...) if(GEngine){GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, FString::Printf(TEXT(x), __VA_ARGS__));}
4 Likes

This doesn’t seem to display anything in Shipping build, so I advise against using it for anything that needs to happen for the end user, only use for things that developers need.

That’s why it’s called a debug message :wink:

You don’t have to do that. This directive is already in the function

For shipping, there is no other way than using UMG, as long as I know.

DebugMessages.h (3.4 KB)
add this header file to your project and include it anywhere you want to use it, examples are provided in the comments, just call the macros anywhere you want to use.

3 Likes