diff --git a/src/my_singleton.cpp b/src/my_singleton.cpp index a9cc740..7447857 100644 --- a/src/my_singleton.cpp +++ b/src/my_singleton.cpp @@ -8,17 +8,28 @@ using namespace godot; +MySingleton *MySingleton::singleton = nullptr; + void MySingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("hello_singleton"), &MySingleton::hello_singleton); } +MySingleton *MySingleton::get_singleton() +{ + return singleton; +} + MySingleton::MySingleton() { + ERR_FAIL_COND(singleton != nullptr); + singleton = this; } MySingleton::~MySingleton() { + ERR_FAIL_COND(singleton != this); + singleton = nullptr; } void MySingleton::hello_singleton() diff --git a/src/my_singleton.hpp b/src/my_singleton.hpp index a085c56..3a38b59 100644 --- a/src/my_singleton.hpp +++ b/src/my_singleton.hpp @@ -12,10 +12,14 @@ class MySingleton : public Object { GDCLASS(MySingleton, Object); + static MySingleton *singleton; + protected: static void _bind_methods(); public: + static MySingleton *get_singleton(); + MySingleton(); ~MySingleton(); diff --git a/src/register_types.cpp b/src/register_types.cpp index e369dee..3eaa8d3 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -22,7 +22,7 @@ void gdextension_initialize(ModuleInitializationLevel p_level) ClassDB::register_class(); _my_singleton = memnew(MySingleton); - Engine::get_singleton()->register_singleton("MySingleton", _my_singleton); + Engine::get_singleton()->register_singleton("MySingleton", MySingleton::get_singleton()); } }