Append does not work on Polygon2D.polygon

Godot Version

4.2.1

Question

I have a simple program that spawns a single Polygon2D.
I can substitute Polygon2D.polygon for a PackedVector2Array, but I cannot append a vertex. Since substituting it for a different PackedVector2Array works, it does not seem to be immutable after the first assignment.
Why is this and where can I get information about things like this? (I am new to this)
(edited for better readability)

extends Node2D


# Called when the node enters the scene tree for the first time.
func _ready():
	var polygon = Polygon2D.new()
	var points = PackedVector2Array()
	points.append(Vector2(0,0))
	points.append(Vector2(100,0))
	points.append(Vector2(100,100))

	var points2 = PackedVector2Array([Vector2(420,200),Vector2(0,300),Vector2(0,0)])

	polygon.polygon = points
	print(polygon.polygon) # [(0, 0), (100, 0), (100, 100)]

	polygon.polygon.append(Vector2(200,280)) # this does not seem to do anything
	print(polygon.polygon) # [(0, 0), (100, 0), (100, 100)]

	polygon.polygon=points2
	print(polygon.polygon) # [(420, 200), (0, 300), (0, 0)]

	polygon.offset = Vector2(100,100)
	polygon.color = Color(1,1,1)
	add_child(polygon)
	
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

1 Like

You need to run the setter for all the node update logic and (re)assigning an array runs the setter. Doing just internal changes to an array does not notify anything.

1 Like

how do you update without performing an assignment? there are no such methods in the documentation for Polygon2D. do I call something like get_bone_count?
Also, when using print after appending on Polygon2D.polygon, I got the PackedVector2Array before the append, so I am not sure it is just the Polygon2D not acknowledging the update.

PackedXYZArrays are copy-on-write. If you do any changes to them you create a copy.

1 Like

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