instance.queue_free() like the comment
# Turn this into a Deferred functionsaid. It's the same as
Most of the time, it's better to use
queue_free() rather than
free(). It queues all the nodes that you want to delete, and destroys them all together at the end of current frame. It ensure all node are safe to delete.
free() destroys the node immediately, but if the node is still in the scene tree, or if the node is emitting a signal or calling a function, it crashes the game.
if not is_instance_valid(instance):
works too. (This code is just for demonstration of the reason behind. Practically, just use
But I'm really NOT sure why it worked in 3.1. Someone needs to tell me why.