How do I create an outline that exists within a polygon?

Godot v4.2.1.stable

I am creating polygons next to eachother using Polygon2d. I want each polygon to have an outline. I am currently copying the points of the Polygon2d and then generating a line2d that serves as the outline. However, this outline (using the width variable in line2d) goes beyond the polygon and clips into the other nearby polygons. I want to keep the outline INSIDE the polygon. How would I do this?

Code of Polygon2d

extends Polygon2D
var claim: int
var troop_number: int
var continent: int
var claim_color
var temp_array = PackedVector2Array()
#var outline

Called when the node enters the scene tree for the first time.

func _ready():
claim_color = Color(0, 1, 0, 1)
color = claim_color
self_modulate.a = 0.25
var outline = Line2D.new()
add_child(outline)
outline.name = “Outline of {name}”.format({“name”: name})
outline.points = polygon
outline.default_color = claim_color
outline.self_modulate.a = 1
outline.closed = true

pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
pass

Set the Polygon2D CanvasItem.clip_children property to Clip + draw

1 Like

Thank you this has done what I want. However now everything has become more transparent which is not what I wanted. I wanted the outline to be 100% visible and for the inside to be 25% visible. I was doing this by changing the alpha channel variables.

That’s one drawback of this method. Don’t change the modulate of the Polygon2D and try setting its Polygon2D.color instead

1 Like

I ended up just using 3 different nodes. A masking polygon 2d to clip the outline, a polygon 2d for the base color, and a line 2d for the outline. I’m sure there’s a more efficient way but this works great for what I need. You have saved me hours of time thank you.