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

Hi all.
In my project "enemies" are all the same scene, but on setup() they are randomized and some parameters are changed. In case of size, I also need to resize CircleShape2D, for their's "hitbox" be same as "image" player see.

Scene tree:
KinematicBody2D
->CollisionShape2D
->Sprite

setup() called after adding "enemy" to main scene, to set it position and movement direction, it's not _ready() (dunno why I do it like this)

At first I set default CircleShape2D to CollisionShape2D and on setup() set new radius, but in this case actual radius sometimes not match the sprite size.
Then I decided to remove default CircleShape2D from CollisionShape2D and add it in the code on setup(). This time all work as intended, BUT debugger create new error every time new "enemy" is spawned. Stack trace lead me to

get_node("collision").set_shape(CircleShape2D.new())

Also, in cmd I can see error, that appears on every "enemy" spawn:

CollisionObject2D::set_shape: Index p_shape_idx out of size (shapes.size()).

The Question: why debugger throw this error and no process disruption was seen during game?

Maybe I should just create separate scenes for each enemy and forget about this?

in Engine by (14 points)

1 Answer

0 votes

This is a bit old, but I'd like to answer for anyone else who saw this page as the only result in google.

I just started physics and ran into the exact same problem and debugger error. All you need to do is use add_shape(CircleShape2D.new()) to add a new shape to the physics object's shape array. So obviously this code must be run in the context of a Static/Rigid/KinematicBody2D. This array starts out empty and will throw errors if you try to use setshape() or getshape(), just like an int array would not allow you to use array[0] if the array was empty.

You can also do something like this to manipulate the radius.
var circle_shape = CircleShape2D.new() shape.set_radius(10) add_shape(circle_shape)

Check the docs for information on how to change the size of any shape you might want to use.

by (14 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 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.