Strange behavior changing the radius of a CollisionShape in instantiated scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By grettyr

Hi there,

I have the following problem and can’t get behind it…

I have a Bubble-Node which I instanciate via script two times on a playing field (Note the ids P1 and E1 and the starting health 250 and 20):

var bubble1 = bubble_bp.instance()
bubble1.init("P1", Globals.factions["Player"], 250, 1.00, Vector2(450, 600))
bubble1.connect("move", self, "getTarget")
bubbles[bubble1.id] = bubble1

var bubble2 = bubble_bp.instance()
bubble2.init("E1", Globals.factions["AI"], 20, 1.00, Vector2(750, 300))
bubble2.connect("move", self, "getTarget")
bubbles[bubble2.id] = bubble2

In the Bubble-Node, I have an Area2D, with an underlying CollisionShape (CirceShape2D).
I now want to change the radius of the shape, depending on the current health (health is contantly growing):

rad = health
$area/zone.shape.radius = rad

If I only instanciate the first bubble and print the radius of the shape + the Id of the Scene (area being the Area2D, a child of the root-Node containing the script, zone being the Collisionshape)

print("Shape: ", $area/zone.get_parent().get_parent().id)
	print("Radius: ", $area/zone.shape.radius)

the Log looks as follows, and everything looks as expected:

Shape: P1, Radius: 250
Shape: P1, Radius: 251
Shape: P1, Radius: 252

If I now instanciate both bubbles, like shown in the code above, the following happens:

Shape: P1, Radius: 20
Shape: E1, Radius: 251
Shape: P1, Radius: 21
Shape: E1, Radius: 252
Shape: P1,Radius: 22
Shape: E1,Radius: 253

As you can see, the radius-values for P1 and E1 are reversed, though this is not the whole truth. Visually, both Shapes have the same, smaller radius .

Can someone explain why this is happening? Its seems that the instanciated scenes are sharing the same Collionshape. All other values are changed independetly as you would expect…

edit:
I just did some more digging. It seems my guess both instances share the same Shape was correct.

Object: bubble:[Node2D:1289]
Shape: [CircleShape2D:1267]
Object: @bubble@2:[Node2D:1295]
Shape: [CircleShape2D:1267]

Why is this? Is this a bug or a feature? (feels buggy to me)

Is there a way to prevent this?

Thanks in advance,
greetings.grettyr

:bust_in_silhouette: Reply From: grettyr

The last realization was obviously the kicker. I call

$area/zone.shape = $area/zone.shape.duplicate()

in the _ready() func now and it’s working.

If you have another idea or thought, please let me know!

greetings.grettyr