Provide easy way to register user singletons/autoloads #1060
Labels
c: register
Register classes, functions and other symbols to GDScript
feature
Adds functionality to the library
Continuation of: godot-rust/book#79 (comment)
Long story short, godot-rust could provide mechanism to simplify registering and using user-declared GDExtension singletons. Given mechanism must work both with editor/godot autloads and "pure" engine singletons; they must be retrievable via custom GameLoop as well
The user-facing API could look like that:
or, for autoloads:
Afterwards one could use their singleton just like:
UserSingleton
trait could look like that:UserSingleton trait
Possible issues:
Namespace clashes – Godot 4.3 handles them nicely and even prevents creating new instances of given engine singleton. No further action is needed.
User Editor/Godot Autoload which is also registered as Engine Singleton will not be available via gdscript as an Engine Singleton (because it is not registered with the library itself, but later on during runtime) but only as an Autoload – thus avoiding the namespace clashes as well.
Additional notes:
Autoloads are global constants exposed to given script server as Variants and must exists in scene tree - in short, they must inherit nodes. They are persistent and not pruned in-between scene changes, unless user explicitly declare to do so. Rust GDExtension autoloads must be declared as scenes (scene with our node, duh) and then added as a godot autoload: https://docs.godotengine.org/en/latest/tutorials/scripting/singletons_autoload.html.
The Engine Singletons are almost the same thing – Godot keeps pointers to given singletons in
hashmap<StringName, Object *>
; engine singleton can be anything that inherits object (in godot context – just anything.).In the past I did some digging and noted down implementation details & differences between Editor/Godot Autoload and Engine singleton: https://discord.com/channels/723850269347283004/1259547468379455601/1259664201022967828 (warning: might be a little incoherent)
The text was updated successfully, but these errors were encountered: