+3 votes

I have an Area2D node with a child CollisionShape2D node. The CollisionShape2D does not have a shape. I would like to give it one in the code during the _ready() function. I used the following code:

var shape = RectangleShape2D.new()
shape.set_extents(Vector2(50,50))
get_node("CollisionShape2D").set_shape(shape)

Unfortunately, this did not work. It did not call the _inputEvent(...) function when it should have. Out of curiosity, I gave the CollisionShape2D a shape and tried the following code:

var shape = get_node("CollisionShape2D").get_shape()
shape.set_extents(Vector2(50,50))
get_node("CollisionShape2D").set_shape(shape)

This code worked, so I can use this if necessary. However, I would like to know what is wrong with the first code snippet?

in Engine by (156 points)
edited by

1 Answer

0 votes
Best answer

Try this:

var shape = RectangleShape2D.new()
get_node("CollisionShape2D").set_shape(shape)
shape.set_extents(Vector2(50,50))

The same sequence is required in a lot of cases as it seems that objects have to be added to the tree before any of their physical properties can be set in order for them to work correctly.

by (44 points)
selected by
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.