How can I change the position of the points in a Polygon2D node with a line of code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Reuben_Levine

There doesn’t seem to be any clear info elsewhere online on this topic. What I want to do is write a line of code to specify the location of a specific point in the polygon. How is this done?

I tried this:

newPolygon.polygon = PackedVector2Array([[0,100],[100,400],[500,300]])

and this:

newPolygon.set_polygon(PackedVector2Array([[0,100],[100,400],[500,300]]))

but these methods both seem to have no effect on the polygon node itself.

:bust_in_silhouette: Reply From: jgodfrey

Here’s one way. First, initialize the polygon with some points (here, just a 100 pixel square):

func _ready():
	var arr = PackedVector2Array()
	arr.append(Vector2(0,0))
	arr.append(Vector2(0, 100))
	arr.append(Vector2(100,100))
	arr.append(Vector2(100, 0))
	$Polygon2D.polygon = arr

Then, to later change say, the position of the 3rd point…

$Polygon2D.polygon[2] = Vector2(500,500)