C++ plugins and events

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By supagu
:warning: Old Version Published before Godot 3 was released.

I am writing a c++ plugin and have a singleton setup. What I would like is some method in my plugin to be called to once the engine is setup and running such that all its features a ready to use. I want to connect to a website using https so cannot do this in the register_types callback.

Is there anyway to be notified in C++ of certain events that occur in the engine?

I don’t know of the proper way of doing singletons in Godot C++, but one way would be to make your class a Node (so you can make multiple of them, if possible?) that you can then put in AutoLoad. Then you’ll be able to use the NOTIFICATION_READY notification (same as _ready in script)

Zylann | 2017-03-09 13:42

:bust_in_silhouette: Reply From: hungrymonkey

If you want to bind a class to gdscript with a singleton set up, you have to bind a dummy class. The dummy class access the singeton. The process of setting up a signal should be similar to a regular function call.

Size2 _OS::get_screen_size(int p_screen) const {
return OS::get_singleton()->get_screen_size(p_screen);
}
void _OS::_bind_method() {
ClassDB::bind_method(D_METHOD("get_screen_size", "screen"), &_OS::get_screen_size, DEFVAL(-1));
}

i would recommend using OS class as a guide.

Basically, you have a regular class

    void register_types() {
      	foo = memnew(FOO);
        foo->init();
         _foo = memnew(_FOO);
        ClassDB::register_class<_FOO>();
        Engine::get_singleton()->add_singleton(Engine::Singleton("FOO", 
        _FOO::get_singleton()));
  }

I am thinking of writing a guide on how to do these things but it will take awhile since Godot servers is an integral part of the engine,