Help! I Cannot get different Shape instance on different CollisionShape2D

Godot Version

Godot Engine v4.2.2.stable.mono.official.15073afe3
C# version

Question

I want to set different radiu

Scenes:

  • Node2D
    • Area2D
      • CollisionShape2D : {Shape:CircleShape2D}

Codes:

public override void _Ready()
{
viewArea = Main.GetChildNode(this, “Area2D”);
GD.Print(“view area:”+viewArea);

viewArea.AreaEntered += (area) => GD.Print(“view area entered:”+area);
CollisionShape2D viewShape = viewArea.GetNode(“CollisionShape2D”);
GD.Print(“view area shape:”+viewShape);
GD.Print(“view area shape inner shape:”+viewShape.Shape);

// return a same CircleShape2D in different Area2D _Ready method
var shape = (CircleShape2D) viewShape.Shape;
shape.Radius = model.viewRange;
}

question: var shape = (CircleShape2D) viewShape.Shape;
I Cannot get different Shape instance on different CollisionShape2D

logs:
view area:<Area2D#28303164645>
view area shape:<CollisionShape2D#28319941853>
view area shape inner shape:<CircleShape2D#-9223372008719383329>
view area:<Area2D#28487714034>
view area shape:<CollisionShape2D#28504491251>
view area shape inner shape:<CircleShape2D#-9223372008719383329>

I got the same Shape:9223372008719383329

please help

If you are trying to achieve a duplication, use the Resource.Duplicate() method.

1 Like

Thank you for your help!

Your reply solved my trouble.
I get a new different CircleShape instance use the Resource.Duplicate() method.
Thank you!

But I don’t know when to use this method.
Because sometimes the instances I get are not public, I can’t tell, which is very troublesome if they all need to be tested.
Whether this is a bug?

Use the same shape when you’re not going to modify it, duplicate before use when you’re going to change it to prevent affecting other code that are using this shape.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.