# How to Debug via C++?

**URL:** <https://forums.unrealengine.com/t/how-to-debug-via-c/293747>\
**Category:** Debugging, Optimization, & Profiling\
**Tags:** Debugging, debug, how-do-i, unreal-engine, print-string, question\
**Created:** [September 15, 2014, 9:42am UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747 "2014-09-15T09:42:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezzzus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dezzzus](https://forums.unrealengine.com/u/dezzzus)\
**Post date:** [September 15, 2014, 9:42am UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/1 "2014-09-15T09:42:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jaragoondoo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jaragoondoo](https://forums.unrealengine.com/u/jaragoondoo)\
**Post date:** [September 15, 2014, 9:46am UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/2 "2014-09-15T09:46:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ecb5cc49](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ecb5cc49](https://forums.unrealengine.com/u/anonymous_user_ecb5cc49)\
**Post date:** [September 15, 2014, 10:07am UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/3 "2014-09-15T10:07:48Z")

</div>

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](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UEngine/AddOnScreenDebugMessage/1/index.html)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_68f8320f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_68f8320f](https://forums.unrealengine.com/u/anonymous_user_68f8320f)\
**Post date:** [March 2, 2015, 5:09pm UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/4 "2015-03-02T17:09:37Z")

</div>

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](https://wiki.unrealengine.com/Logs,_Printing_Messages_To_Yourself_During_Runtime):

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [March 2, 2015, 5:11pm UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/5 "2015-03-02T17:11:53Z")

</div>

I deleted the duplicated anwser

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [March 2, 2015, 5:20pm UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/6 "2015-03-02T17:20:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [July 23, 2015, 8:07pm UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/7 "2015-07-23T20:07:48Z")

</div>

#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!**

> **[New Wiki, How to Get a UE4 FString of Calling Class, Function, and Line Number...](https://forums.unrealengine.com/showthread.php?57154-New-Wiki-How-to-Get-a-UE4-FString-of-Calling-Class-Function-and-Line-Number-&p=210060&viewfull=1#post210060)**
>
> Dear Community, I’ve just posted a new wiki! I am giving you easy-to-use pre-processor commands to get a UE4 String that tells you the Class Name, Function Name, and Line Number of the calling code! Logs, Printing the Class Name, Function Name,...

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

 ![51251-joystringcurrentclassfunclinenumber2.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/5/1/a51b4eb12a773cd47ef504c3015401ed5a1222f7.jpeg)

---

<div class="post-metadata">

**Author:** ![Commander\_Shepard](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Commander\_Shepard](https://forums.unrealengine.com/u/Commander_Shepard)\
**Post date:** [August 19, 2015, 5:55pm UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/8 "2015-08-19T17:55:10Z")

</div>

Ayy! thats my pic you have 🙂

---

<div class="post-metadata">

**Author:** ![volkmedia](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@volkmedia](https://forums.unrealengine.com/u/volkmedia)\
**Post date:** [April 16, 2016, 1:16am UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/9 "2016-04-16T01:16:00Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![dellis23](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dellis23](https://forums.unrealengine.com/u/dellis23)\
**Post date:** [August 7, 2020, 3:55pm UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/10 "2020-08-07T15:55:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MeddleGearSalad](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/meddlegearsalad/32/88706_2.png) [@MeddleGearSalad](https://forums.unrealengine.com/u/MeddleGearSalad)\
**Post date:** [September 21, 2021, 2:47am UTC](https://forums.unrealengine.com/t/how-to-debug-via-c/293747/11 "2021-09-21T02:47:55Z")

</div>

Yeah it’s really painful right now
