How to Delete a Specific MultiMeshInstance2D Instance

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

Hello, I am attempting to make a bullet hell engine using MultiMeshes, but I am having a hard time deleting a specific instance of the MultiMesh. I am able to calculate collision based on position, and I am writing this in C++, so optimization is not too much of an issue for now. Thank you so much in advance!

:bust_in_silhouette: Reply From: ibrahim.semab

To delete a specific instance of a MultiMesh, you can use the set_instance_transform method to set the transform of the instance you want to delete to an unused value (such as a position outside the viewport), effectively removing it from the visible scene. Here’s an example:

// Assume we have a MultiMeshInstance with the MultiMesh resource loaded.
MultiMeshInstance* multi_mesh_instance = get_node("MultiMeshInstance");

// Assume we have an index of the instance we want to delete.
int instance_index_to_delete = 2;

// Set the transform of the instance to an unused value.
Transform unused_transform;
unused_transform.origin = Vector3(9999, 9999, 9999);
multi_mesh_instance->get_multimesh()- 
>set_instance_transform(instance_index_to_delete, unused_transform);

After setting the instance’s transform to an unused value, it will no longer be rendered in the scene. If you need to completely remove the instance from the MultiMesh resource (i.e. freeing up memory), you can use the remove_instance method:

multi_mesh_instance->get_multimesh()->remove_instance(instance_index_to_delete);

Note that removing an instance from the MultiMesh resource will shift the indices of all subsequent instances, so you may need to adjust your instance index references accordingly.