executing code "on Free"

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

Hi there,
I wonder is there is a simple way to call a function just before a node gets freed.
Either some “OnFree()” to override (in C# if possible), or a signal to connect?

I am asking this because I have some container nodes “owning” other nodes which are not added to the main tree. (Typically patches of the world far from the player, which are only partially loaded and are not in the tree for performances reasons)
Having on “OnFree” method would thus allow to QueueFree also those unregisted nodes at the right time.
I think that if I do not it is a memory leak ?

:bust_in_silhouette: Reply From: a_world_of_madness

You could use the _notification callback to signal the NOTIFICATION_PREDELETE message.

From the docs:

Object::NOTIFICATION_PREDELETE: a callback that triggers before the engine deletes an Object, i.e. a ‘destructor’.

Thanks, it seems to be exactly what I was looking for!

AlexSand | 2022-11-03 23:36

For the stubborns as me:

func _notification(what):
		# Do your stuff here

d2clon | 2023-01-31 18:13