# How to print debug output in C++ from OnConstruction()?

**URL:** <https://forums.unrealengine.com/t/how-to-print-debug-output-in-c-from-onconstruction/302471>\
**Category:** Debugging, Optimization, & Profiling\
**Tags:** debug, unreal-engine, construction-script, CPP, question\
**Created:** [December 26, 2014, 6:34am UTC](https://forums.unrealengine.com/t/how-to-print-debug-output-in-c-from-onconstruction/302471 "2014-12-26T06:34:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_2f5f0c3f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2f5f0c3f](https://forums.unrealengine.com/u/anonymous_user_2f5f0c3f)\
**Post date:** [December 26, 2014, 6:34am UTC](https://forums.unrealengine.com/t/how-to-print-debug-output-in-c-from-onconstruction/302471/1 "2014-12-26T06:34:29Z")

</div>

I want to print a string from the OnConstruction() function, which is the C++ equivalent of the construction script.

The standard method works from the Tick() function but doesn’t work from the OnConstruction() function, presumably because GEngine is null when the game is not running:

```
if (GEngine)
{
    GEngine->AddOnScreenDebugMessage(-1, TimeToDisplay, Color, Text);
}

```

Any other way this can be done?

---

<div class="post-metadata">

**Author:** ![eXi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/exi/32/906583_2.png) [@eXi](https://forums.unrealengine.com/u/eXi)\
**Post date:** [December 26, 2014, 1:10pm UTC](https://forums.unrealengine.com/t/how-to-print-debug-output-in-c-from-onconstruction/302471/2 "2014-12-26T13:10:58Z")

</div>

Hm, i just searched the API for GEngine. It is a pointer to the UEngine which is the base class of all Engine classes. UEngine got the AddOnScreenDebugMessage, so if GEngine is null, the pointer has no reference to the UEngine. The bad thing is, i couldn’t find any information about the chronological order of the initialization of this UEngine etc. so i can’t tell you if the Pointer is just null because it wasn’t set by now (so you could use something else than this pointer) or if the whole UEngine isn’t initialized at this point.

Have you tried checking if the OnConstruction() is called at all? You could try using a normal Log Debug etc.

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

EDIT: Would be nice to know where GEngine is set :X I can’t find that for now.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2f5f0c3f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2f5f0c3f](https://forums.unrealengine.com/u/anonymous_user_2f5f0c3f)\
**Post date:** [December 26, 2014, 4:59pm UTC](https://forums.unrealengine.com/t/how-to-print-debug-output-in-c-from-onconstruction/302471/3 "2014-12-26T16:59:02Z")

</div>

Thanks eXi for the solution. I used UE\_LOG() for the construction-time logging.

Also GEngine is indeed not null. The GEngine-\>AddOnScreenDebugMessage() executes but does nothing.

Modified solution:

```
static FORCEINLINE void Print(FString Text, bool PrintToLog = false, FColor Color = FColor::Green, float TimeToDisplay = 1.0f)
{
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, TimeToDisplay, Color, Text);
	}

	if (PrintToLog)
	{
		UE_LOG(LogTemp, Warning, TEXT("%s"), *Text);
	}
}

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2f5f0c3f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2f5f0c3f](https://forums.unrealengine.com/u/anonymous_user_2f5f0c3f)\
**Post date:** [December 26, 2014, 5:02pm UTC](https://forums.unrealengine.com/t/how-to-print-debug-output-in-c-from-onconstruction/302471/4 "2014-12-26T17:02:55Z")

</div>

@eXi: Solved. Turns out that OnConstruction was being called, and GEngine wasn’t null. GEngine-\>AddOnScreenDebugMessage() simply did nothing. UE\_LOG worked. Thanks.
