set PolygonCollider2D's polygon variable in GDscript

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

I am trying to set a polygon’s collider through a script.

my code:
borderCol.get("polygon").set(0, Vector2(1000, 1000))

but it doesn’t set the size.

:bust_in_silhouette: Reply From: kidscancode

polygon is a property of CollisionPolygon2D. As shown in the linked doc, its type is PoolVector2Array. This is an array of Vector2 objects representing the vertices of the polygon. For example to set a triangular shape, you need three points:

var points = PoolVector2Array([Vector2(0, 0), Vector2(0, 100),
			    Vector2(100, 100)])
borderCol.polygon = points

Note that in GDScript, there’s no need for get()/set() - you can just access the property directly to get or set the value.