CircleShape2D debugger errors, but game doesn't crush

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By yarikonline
:warning: Old Version Published before Godot 3 was released.

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?

:bust_in_silhouette: Reply From: nanimonull

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 set_shape() or get_shape(), 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.