# C++ / Is there a way to draw debug text on the screen at a 3d coordinate?

**URL:** <https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385>\
**Category:** Debugging, Optimization, & Profiling\
**Tags:** unreal-engine, editor, UE4, CPP, question\
**Created:** [March 24, 2015, 2:47pm UTC](https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385 "2015-03-24T14:47:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neofuturelabs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neofuturelabs/32/1194562_2.png) [@neofuturelabs](https://forums.unrealengine.com/u/neofuturelabs)\
**Post date:** [March 24, 2015, 2:47pm UTC](https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385/1 "2015-03-24T14:47:46Z")

</div>

A lot of other engines have simple functionality to do this built in (as it is extremely useful for debugging), but I haven’t been able to find an easy way to do it in UE4.

All I really want to do is draw the index of my route data’s nodes at the node position in 3D when the route is selected in the editor. (in both edit and play mode)

I was using a trick posted in the following thread, and managed to create a component that can draw debug primitives while in edit mode.

> **[\[Request\] Draw editor event for blueprints and native](https://forums.unrealengine.com/questions/110338/request-draw-editor-event-for-blueprints-and-nativ.html)**
>
> Coming from Unity, one feature they have is an easy way to draw debug lines for custom classes in the editor. Please see OnDrawGizmos and OnDrawGizmosSelected . Currently there appears to be no easy way to quickly draw debug lines, or information...

However, the only arguments I have in this function are (const FSceneView\* View, FPrimitiveDrawInterface\* PDI). If I can get a Canvas, it looks like I can at least call DrawText, but trying the following was just making the editor crash somewhere deep in DrawText.

```
 			static const FName CanvasName(TEXT("CanvasObject"));
 			UCanvas* Canvas = (UCanvas*)StaticFindObjectFast(UCanvas::StaticClass(), GetTransientPackage(), CanvasName);

			if (Canvas)
			{
				Canvas->SetDrawColor(255, 255, 255);
				UFont* RenderFont = GEngine->GetSmallFont();
				const FVector ScreenLocation = Canvas->Project(pos);
				Canvas->DrawText(RenderFont, FString::Printf(TEXT("%d"), i), ScreenLocation.X, ScreenLocation.Y);
			}

```

I also tried (experimentally) creating a canvas if it doesn’t exist like certain parts of the engine are doing, but it crashed in the same place.

Is there a correct way to get the current canvas somehow, or a better way to draw debug text on the screen?

Also, if any Epic devs are reading this: Please implement easy debug primitive and 2d/3d text printing functions that can be called from anywhere. A huge number of people would benefit from this.

---

<div class="post-metadata">

**Author:** ![kishoreven](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kishoreven](https://forums.unrealengine.com/u/kishoreven)\
**Post date:** [May 20, 2015, 4:28pm UTC](https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385/2 "2015-05-20T16:28:18Z")

</div>

I took a look at the code, when I came across the same problem. The way to do it is to override the  
_virtual void DisplayDebug(UCanvas Canvas, const FDebugDisplayInfo& DebugDisplay, float& YL, float& YPos);_\* in you pawn or actor code.

Hope that helped!

---

<div class="post-metadata">

**Author:** ![kishoreven](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kishoreven](https://forums.unrealengine.com/u/kishoreven)\
**Post date:** [May 20, 2015, 5:40pm UTC](https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385/3 "2015-05-20T17:40:02Z")

</div>

Also I am not sure if that really draws a 3D text. So I am at a loss as well ☹

---

<div class="post-metadata">

**Author:** ![ddbrown30](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ddbrown30](https://forums.unrealengine.com/u/ddbrown30)\
**Post date:** [October 5, 2016, 2:54pm UTC](https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385/4 "2016-10-05T14:54:41Z")

</div>

You’re looking for DrawDebugString. You can either give it a world position or you can give it an actor reference and the position will instead be used as an offset from that actor. The offset is not affected by the rotation of the actor.

> **[Draw Debug String](https://docs.unrealengine.com/4.27/en-US/BlueprintAPI/Rendering/Debug/DrawDebugString/)**
>
> Draw Debug String

---

<div class="post-metadata">

**Author:** ![neofuturelabs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neofuturelabs/32/1194562_2.png) [@neofuturelabs](https://forums.unrealengine.com/u/neofuturelabs)\
**Post date:** [October 6, 2016, 7:15am UTC](https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385/5 "2016-10-06T07:15:56Z")

</div>

I was actually referring to a way to do this from C++, sorry my question was unclear.

However, when I tried copying the C++ code the blueprint version of DrawDebugString was using, it functioned perfectly! Problem solved. Thanks!

Sample code:

```
#if !UE_BUILD_SHIPPING
   	FVector drawPos(0.0f);
   	FColor drawColor = FLinearColor(1.0f, 1.0f, 1.0f, 1.0f).ToFColor(true);
   	float drawDuration = 0.0f;
   	bool drawShadow = true;
   	DrawDebugString(GEngine->GetWorldFromContextObject(this), drawPos, *FString::Printf(TEXT("%s[%d]"), TEXT("test"), 12345), NULL, drawColor, drawDuration, drawShadow);
#endif //!UE_BUILD_SHIPPING
```

---

<div class="post-metadata">

**Author:** ![735333df6d5b4784be24](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@735333df6d5b4784be24](https://forums.unrealengine.com/u/735333df6d5b4784be24)\
**Post date:** [February 20, 2023, 8:43am UTC](https://forums.unrealengine.com/t/c-is-there-a-way-to-draw-debug-text-on-the-screen-at-a-3d-coordinate/312385/6 "2023-02-20T08:43:55Z")

</div>

FAnimNode\_SkeletalControlBase

Inside AnimNode how to visualize it?
