# How to draw lines under certain widgets/UI elements?

**URL:** <https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979>\
**Category:** C++\
**Tags:** unreal-engine, CPP, question\
**Created:** [November 19, 2024, 10:00am UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979 "2024-11-19T10:00:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justrenis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Justrenis](https://forums.unrealengine.com/u/Justrenis)\
**Post date:** [November 19, 2024, 10:00am UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979/1 "2024-11-19T10:00:26Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/0/3/c037625dcb2db4cc76c87faf884e68e23f3029c2.png)  
The drawn lines should be under the nodes(those robots images) and under the UI from outercanvas(outercanvas and innercanvas are used for panning and zooming)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/b/3/bb3f697222f5a09674d5a7ae95d7c4e3e419e2fb.png)

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 19, 2024, 3:41pm UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979/2 "2024-11-19T15:41:31Z")

</div>

> [@Draw Line but under the widget (line z order?)](https://forums.unrealengine.com/t/draw-line-but-under-the-widget-line-z-order/696717/8):
>
> Yeah, the solution is simple but kind of silly. Create a different Widget Blueprint with one job, which is drawing lines. Then add this blueprint to your main blueprint, and you can change its order and make it covered by other widgets. However, if you want different lines with different Z-orders, you’ll need to create multiple widgets with lines. If I were to do it now, I would create a generic widget blueprint called something like ‘WBP\_LineDrawer’, where I could draw a single line or arrays…

You can also experiment with passing in a custom LayerId in the NativePaint **strong text** for the MakeLines and super call

.h

```auto
#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "UpgradeWidgetTree.generated.h"

class UCanvasPanel;
/**
 * 
 */
UCLASS()
class YOUR_API UUpgradeWidgetTree : public UUserWidget
{
	GENERATED_BODY()
public:	

	virtual int32 NativePaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;
	
	UPROPERTY(meta = (BindWidget))
	UCanvasPanel* InnerCanvas;

	UPROPERTY(meta = (BindWidget))
	UWidget* Slot1;

	UPROPERTY(meta = (BindWidget))
	UWidget* Slot2;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 LineLayerId;
	

};

```

.cpp

```auto

#include "UpgradeWidgetTree.h"
#include "Components/CanvasPanelSlot.h"
#include "Blueprint/WidgetLayoutLibrary.h"
#include "Components/CanvasPanel.h"
#include "Rendering/DrawElementTypes.h"

int32 UUpgradeWidgetTree::NativePaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{	
	int32 LayerID = Super::NativePaint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
	
	if (InnerCanvas != nullptr && Slot1 != nullptr && Slot2 != nullptr)
	{		
			UCanvasPanelSlot* CanvasSlot0 = UWidgetLayoutLibrary::SlotAsCanvasSlot(Slot1);
			UCanvasPanelSlot* CanvasSlot1 = UWidgetLayoutLibrary::SlotAsCanvasSlot(Slot2);

			FVector2D AntchorPosition = InnerCanvas->GetCachedGeometry().GetLocalSize() * CanvasSlot0->GetAnchors().Minimum;
			FVector2D Position0 = AntchorPosition + CanvasSlot0->GetPosition();
			FVector2D Position1 = AntchorPosition + CanvasSlot1->GetPosition();
												
			FSlateDrawElement::MakeLines(
				OutDrawElements,				
				LineLayerId,				
				InnerCanvas->GetPaintSpaceGeometry().ToPaintGeometry(),
				{ Position0,Position1 },
				ESlateDrawEffect::None,
				FLinearColor::Blue,
				true,
				5.0f
			);					
	}	
	return LayerID;	
}

```

 ![obraz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/7/5/07588341938101a95b64f739456baaf25c2f8719.png)

Alignment for both boxes is set to 0.5 , 0.5  
Line layer ID = 50

---

<div class="post-metadata">

**Author:** ![enga](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@enga](https://forums.unrealengine.com/u/enga)\
**Post date:** [November 19, 2024, 6:49pm UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979/3 "2024-11-19T18:49:50Z")

</div>

also i dont really understand what he wanted, use widget need to be highest number, like 99999,(means on top) or you go thought the current drawer , opengl and so on and u base on that. using game engine if your developing is easy, but using this to interloop functions u need to hook

---

<div class="post-metadata">

**Author:** ![Justrenis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Justrenis](https://forums.unrealengine.com/u/Justrenis)\
**Post date:** [November 20, 2024, 1:51pm UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979/4 "2024-11-20T13:51:37Z")

</div>

Solution was to make a single widget that entirely handles all the lines and then you can just batch draw as many as you want, and make them draw beneath/on top of other things by Z-order or parenting.

About your second solution i already tried that before, and it won’t work, it worked on editor with id as “50” but in cooked build its completely unreliable, it even draws on one of the nodes and then beneath other node.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [December 20, 2024, 1:52pm UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979/5 "2024-12-20T13:52:17Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
