Game Dev for Noobs

You've just called queue_free() on a node in your scene in Godot. However, right after that line, you need to execute a function, _update_ui_elements(), which might have previously referenced parts of the now-deleted node. To prevent your game from crashing with an "invalid instance" error, how do you safely tell Godot to run _update_ui_elements() only after the node is truly gone and dusted?

3 months ago | [YT] | 19



@GameDevForNoobs

When you call queue_free() on a node in Godot, the node isn't immediately deleted. Instead, it's marked for deletion and actually removed at the end of the current frame. If you try to access or reference it right after queue_free(), it might still technically exist- but it's in an unsafe state, leading to potential "invalid instance" errors. Using call_deferred() delays the call to the next safe moment (after the current frame ends), ensuring that all nodes queued for deletion have actually been removed. This makes it perfect for safely calling a method like _update_ui_elements().

3 months ago (edited) | 1

@CopperFrance

You can also write _update_ui_elements.call_deferred()

3 months ago | 2  

@DeeJayLSP

On _update_ui_elements() you may also check if the referenced node is valid or not queued for deletion before doing anything on it.

3 months ago (edited) | 0