This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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()
shape.set_extents(Vector2(5*PlatformSize,1))
new_platform.add_shape(shape)

add_child(new_platform) 

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.

Example:

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

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 (22,191 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 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.