How to remove a node from an array?

Godot Version

4.4

Question

So I am instantiating a scene into another scene using a for loop and saving all nodes in an Array and I have a script on the other node to queue free them when on clicked but when the nodes get deleted,the array remains the same. I want to remove the deleted node from the array.
HasDust is an array

remove_at if you know the index, erase if you do not. (Don’t use erase while inside of a for loop.)

you can use erase inside a for loop if you iterate backwards! :+1:

Anyway you can get the index of a child with get_index() but that will only work if the nodes are in the same order in the array as they are children. It looks like the order is reversed because of push_front() so you’d need to subtract the value from get_index() from the total number of children minus 1.

1 Like

Usually it’s a working strategy to have whoever creates the thing also be responsible for destroying the thing. Send a signal to the original creator and ask for deletion, instead of just deleting on the spot.

Alternatively, when you use the array, you can check if the references are still valid, and if not, remove them from the array at that point.