0 votes

I am trying to dynamically create platforms, along with their collision shapes.
I actually managed to do it in Godot 2.1, but in Godot 3 I get an error because "add_shape" does not exist anymore.

Any ideas how to achieve the same? This is the snippet of code that worked in Godot 2.1:

    # add collision shape of size PLATFORMSIZE  
var shape = RectangleShape2D.new()


Now "addchild" gives an error. I am just trying to add a collision shape to newplatform and then add new_platform to my world.

in Engine by (21 points)

1 Answer

+2 votes

You have two options. If you add a CollisionShape2D child, you can add your new shape to it. Otherwise, you have to use the collision/shape_owner API, which I think is more cumbersome to use.


var collision = CollisionShape2D.new()
var shape = RectangleShape2D.new()
shape.extents = Vector2(5 * PlatformSize, 1))
collision.shape = shape

See CollisionObject2D docs for details of the shape_owner API. You might also find it easier if you create a default platform scene that includes a collision shape and instance that, so that you only have to set the shape extents.

You didn't mention what error add_child() was giving you, so I'm not sure what to advise about that.

by (20,897 points)

Thanks a lot.
That helped immensely!

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.
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 webmaster@godotengine.org with your username.