How to properly remove a shape ?

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

I made a scene with a single StaticBody2D who create several walls dynamically like this

func newWall(begin, end):
 var wall = SegmentShape2D.new()
 wall.set_a(begin)
 wall.set_b(end)
 add_shape(wall)
 WallArray.push_back(wall)

I want to destroy the first segment shape but this code seems to be illegal.

  # remove_shape function needs an int. I try to put the Object ID 
  # as argument probably because I'm stupid.
   remove_shape(WallArray[0].get_instance_ID())

The error : Index p_shape_idx out of size (shapes.size())

So I want to know where is the shape index ? Or there is a better way to delete a single collision shape ?

The method remove_shape asks for a shape index (shapes are added like into an array), I don’t know what you keep on WallArray.

Maybe if you want to add and remove the same set of shapes all the time you can add all and just set some as trigger (not sure if will work as I think, never tried that).

Check the options for CollisionObjects
http://docs.godotengine.org/en/stable/classes/class_collisionobject2d.html

eons | 2017-01-06 16:55

I’m blind ! The shape index is an index array, like the error message said. So to delete the first shape of this static body (or in other words, the older) remove_shape(0) works well.

Unfortunately I don’t know how to access to the shapes array so I need my own WallArray to work on the shapes. I use it to draw walls upon them.
Thanks !

DriNeo | 2017-01-06 17:22

Get the number of shapes with get_shape_count, there you have the equivalent to array size, then get_shape to retrieve the shape.


And just tested the method to set shapes as trigger and works fine on static bodies, but you will have to wake up all the rigids (if you have any) so they notice the change if are sleeping in contact.

eons | 2017-01-06 19:05

remove_shape is fine for my scene because I don’t need to respawn the segments.

The functions get_shape and get_shape_count can replace my array. But I wonder which of those solutions is the most efficient. I want to wait a little bit before removing all the references to my array.

DriNeo | 2017-01-06 22:11