add proper singleton methods

main
Nathan Franke 4 years ago
parent 1815783acd
commit 3e9ae67a4d
No known key found for this signature in database
GPG Key ID: 082B90CF10A5B648

@ -8,17 +8,28 @@
using namespace godot; using namespace godot;
MySingleton *MySingleton::singleton = nullptr;
void MySingleton::_bind_methods() void MySingleton::_bind_methods()
{ {
ClassDB::bind_method(D_METHOD("hello_singleton"), &MySingleton::hello_singleton); ClassDB::bind_method(D_METHOD("hello_singleton"), &MySingleton::hello_singleton);
} }
MySingleton *MySingleton::get_singleton()
{
return singleton;
}
MySingleton::MySingleton() MySingleton::MySingleton()
{ {
ERR_FAIL_COND(singleton != nullptr);
singleton = this;
} }
MySingleton::~MySingleton() MySingleton::~MySingleton()
{ {
ERR_FAIL_COND(singleton != this);
singleton = nullptr;
} }
void MySingleton::hello_singleton() void MySingleton::hello_singleton()

@ -12,10 +12,14 @@ class MySingleton : public Object
{ {
GDCLASS(MySingleton, Object); GDCLASS(MySingleton, Object);
static MySingleton *singleton;
protected: protected:
static void _bind_methods(); static void _bind_methods();
public: public:
static MySingleton *get_singleton();
MySingleton(); MySingleton();
~MySingleton(); ~MySingleton();

@ -22,7 +22,7 @@ void gdextension_initialize(ModuleInitializationLevel p_level)
ClassDB::register_class<MySingleton>(); ClassDB::register_class<MySingleton>();
_my_singleton = memnew(MySingleton); _my_singleton = memnew(MySingleton);
Engine::get_singleton()->register_singleton("MySingleton", _my_singleton); Engine::get_singleton()->register_singleton("MySingleton", MySingleton::get_singleton());
} }
} }

Loading…
Cancel
Save