0 votes

i want to draw custom shapes use them as platforms in my game.
i tried using a StaticBody2D node for this, and i had no problem getting it to draw the shapes, but i got stuck on setting the collision shape, all the threads i could find suggested using a Polygon2D with the shape i want and adding it using the "add_shape" function,
but i get this error:

Invalid call. Nonexistent function 'add_shape' in base 'StaticBody2D (StaticBody2D.gd)'.

what am i doing wrong? should i use a different base?
thanks a lot, im a newcomer just started today :)

in Engine by (12 points)

1 Answer

+5 votes

add_shape() is not a function of StaticBody2D.

CollisionObject2D nodes (static/rigid/kinematic) have an api for adding shapes:
http://docs.godotengine.org/en/latest/classes/class_collisionobject2d.html
but it can be complex to use, as you need to create a shape owner and then add the shape, etc.

It's much more straightforward if you use CollisionPolygon2D:
http://docs.godotengine.org/en/latest/classes/class_collisionpolygon2d.html

Add it as a child of your StaticBody2D and set the polygon property using the same point array you used for your Polygon2D:

$CollisionPolygon2D.polygon = $Polygon2D.polygon
by (22,067 points)

Bless you mate, may i ask what is the meaning of the dollar signs you typed in the code?

The $ in GDScript is a shorthand for get_node(). So it means the same to write

get_node("CollisionPolygon2D")

as

$CollisionPolygon2D
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.