+1 vote

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.

asked Nov 23, 2019 in Engine by 3top1a (13 points)

1 Answer

+1 vote

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.

answered Nov 23, 2019 by kidscancode (17,714 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.