godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.74k stars 574 forks source link

Support for Custom Servers/Auto-Load Singletons #1192

Open saturnian-tides opened 1 year ago

saturnian-tides commented 1 year ago

Godot version

4.1.1stable

godot-cpp version

4.1.1stable

System information

Arch, btw

Issue description

I am trying to add a new Server to act as a something like an AutoLoad Singleton (a global resource that maintains state across the lifetime of the game application, and is given work by Nodes) in GDExtension. I run into the following issue however: extension/src/register_types.cpp:37:34: error: 'class godot::Engine' has no member named 'add_singleton'; did you mean 'get_singleton'? 37 | Engine::get_singleton()->add_singleton(Engine::Singleton("SteamAudioServer", SteamAudioServer::get_singleton())); | ^~~~~~~~~~~~~ | get_singleton extension/src/register_types.cpp:37:56: error: 'Singleton' is not a member of 'godot::Engine' 37 | Engine::get_singleton()->add_singleton(Engine::Singleton("SteamAudioServer", SteamAudioServer::get_singleton())); It seems the add_singleton method is not exposed by GDExtension for the Engine singleton. A version of this code works fine with old-fashioned Modules, however. For this implementation the Server (or Singleton) needs to load before nodes in the load order. Any suggestions would be great.

Steps to reproduce

Try to adapt a Custom Server module to GDExtension: https://docs.godotengine.org/en/stable/contributing/development/core_and_modules/custom_godot_servers.html

Minimal reproduction project

N/A

saturnian-tides commented 1 year ago

Hm looking at the generated code for Engine, I see: void register_singleton(const StringName &name, Object *instance); void unregister_singleton(const StringName &name); Perhaps this is just a difference in API naming convention?

dsnopek commented 1 year ago

Hm looking at the generated code for Engine, I see: void register_singleton(const StringName &name, Object *instance); void unregister_singleton(const StringName &name); Perhaps this is just a difference in API naming convention?

Yep! In one of my GDExtensions (SG Physics 2D) I'm registering a singleton (which is also a server) using Engine::get_singleton()->register_singleton() and it works just fine :-)

saturnian-tides commented 1 year ago

Woah, neat project! Love applications of fixed-point math. I am going to have to check it out as reference for GDExtension integration. Thanks!