Emit signal when metadata change

Godot Version



Is it possible to emit a signal whenever a specific metadata value change?

As far as I know, there isn’t any built-in solution for this, though you can always propose that Godot add a signal to the Object class to support this.

You can write a script that adds a meta_changed(name, value) signal to the class and override the set_meta(name, value) method to emit that signal, but it would only apply to whatever scripts shared that logic; because GDScript does not yet support traits, it would be difficult to integrate that logic into custom scripts that inherit from more derived classes. You’d have to start copy/pasting the signal & set_meta(...) override into each script.

Alternatively, create an autoload singleton that maintains a Dictionary of Dictionaries with the keys being object instances. Re-implement the basic meta API around that singleton, but accepting an additional object instance as a first argument, and then defer your would-be meta operations to that singleton, that way you have full control and can have a signal emitted for any object, regardless of script or lack thereof. The downside there though is that you’ll want to periodically clean up the Dictionary to remove stale/deleted objects. It’s possible to receive a notification of an object being deleted, but it’s delivered via the _notification(what) callback with the NOTIFICATION_PREDELETE constant for the Object class rather than via a signal, so periodic removal is the only cleanup option you’d have available if you go that route.