Why doesn't the Unreal Engine have Draw Debug functions in screen space?

Does anyone know, why the Unreal Engine doesn’t have Draw Debug functions for screen space?

There is the draw debug circle, capsule, box, line, and many other functions/events. But they’re all drawing these shapes in the 3D space. Why isn’t there something similar for 2D screen space? Where you enter pixel coordinates as input, and the shape gets drawn at that location.

You can use the AHUD and UCanvas functions (DrawLine, DrawRect) to draw shapes in screen space. It’s not the same as using the Draw Debug functions, but it works for most tasks.

Yes, but you only have the Draw Line and Draw Rect, there is no draw circle or ellipse. You don’t have much control over it, and you can only use it within the HUD blueprint, as far as I’m aware. It’s not possible to call such functions inside your actors.

There are also DrawDebugCanvas2DCircle, DrawDebugCanvas2DBox etc. Check DrawDebugHelpers.h
And there are many ways to use UCanvas: AHUD::OnHUDPostRender delegate, UDebugDrawService, AActor::PostRenderFor.

Hi, thanks for the answer. This seems like something I’m looking for.

When using functions like DrawDebugCanvas2DCircle, it requires a UCanvas argument. Do you know how can I get reference to it?

image

if you are overriding the huds DrawHUD method then you can access it’s canvas directly through it’s variable called Canvas.

example

.h in custom hud class derived from AHUD

virtual void DrawHUD() override;

.cpp

void ACustomHUD::DrawHUD() {
DrawDebugCanvas2DCircle(Canvas, FVector2D(400, 400), 200, 30, FLinearColor::Red, 6);
}
1 Like

Hey, sorry for the late response. But yes, this works, if I use a custom hud class derived from AHUD there is a Canvas variable that can be used.

But I still don’t understand why the Unreal Engine doesn’t have more flexible draw debug functions for screen space. DrawDebugCanvas2DCircle works great, but If you, let’s say want to draw an ellipse, you can’t, while In the regular DrawDebugCircle (3D), when defining axes, you can multiply one axis with some number and get an ellipse.

You could make a custom debug class and just add some transforms to the draw process

void DrawDebugCanvas2DCircle(UCanvas* Canvas, const FVector2D& Center, float Radius, int32 NumSides, const FLinearColor& LineColor, const float& LineThickness)
{
	const float	AngleDelta = 2.0f * UE_PI / NumSides;
	FVector2D AxisX(1.f, 0.f);
	FVector2D AxisY(0.f, -1.f);
	FVector2D LastVertex = Center + AxisX * Radius;

	for (int32 SideIndex = 0; SideIndex < NumSides; SideIndex++)
	{
		const FVector2D Vertex = Center + (AxisX * FMath::Cos(AngleDelta * (SideIndex + 1)) + AxisY * FMath::Sin(AngleDelta * (SideIndex + 1))) * Radius;
		DrawDebugCanvas2DLine(Canvas, LastVertex, Vertex, LineColor, LineThickness);
		LastVertex = Vertex;
	}
}

It would probably be enough either multiple the AxisX and AxisY by a set value (the 1 and -1) or in the for loop the specific axis.

Or better yet if there was a possibility to pass debug object’s through a transform node that could change them.

Here is a implementation of DrawDebugCrossHair in screen space. Hope it helps.