From 1815783acd7bad50c0799b11f4bda2a1ed222dcf Mon Sep 17 00:00:00 2001 From: Nathan Franke Date: Fri, 17 Jun 2022 11:19:38 -0500 Subject: [PATCH] add singleton example --- demo/demo.gd | 4 ++-- demo/demo.tscn | 2 ++ src/example.cpp | 28 ---------------------------- src/my_node.cpp | 27 +++++++++++++++++++++++++++ src/{example.hpp => my_node.hpp} | 10 +++++----- src/my_singleton.cpp | 27 +++++++++++++++++++++++++++ src/my_singleton.hpp | 23 +++++++++++++++++++++++ src/register_types.cpp | 14 ++++++++++++-- 8 files changed, 98 insertions(+), 37 deletions(-) delete mode 100644 src/example.cpp create mode 100644 src/my_node.cpp rename src/{example.hpp => my_node.hpp} (72%) create mode 100644 src/my_singleton.cpp create mode 100644 src/my_singleton.hpp diff --git a/demo/demo.gd b/demo/demo.gd index 555d963..6c8b82c 100644 --- a/demo/demo.gd +++ b/demo/demo.gd @@ -2,5 +2,5 @@ extends Node func _ready() -> void: print("Hello GDScript!") - # TODO: Static https://github.com/godotengine/godot/issues/61963 - Example.new().hello_extension() + $MyNode.hello_node() + MySingleton.hello_singleton() diff --git a/demo/demo.tscn b/demo/demo.tscn index 780c646..f9052f2 100644 --- a/demo/demo.tscn +++ b/demo/demo.tscn @@ -4,3 +4,5 @@ [node name="Demo" type="Node"] script = ExtResource( "1_6gvlp" ) + +[node name="MyNode" type="MyNode" parent="."] diff --git a/src/example.cpp b/src/example.cpp deleted file mode 100644 index 174a690..0000000 --- a/src/example.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "example.hpp" - -#include - -#include -#include -#include - -using namespace godot; - -void Example::_bind_methods() -{ - // TODO: Static https://github.com/godotengine/godot/issues/61963 - ClassDB::bind_method(D_METHOD("hello_extension"), &Example::hello_extension); -} - -Example::Example() -{ -} - -Example::~Example() -{ -} - -void Example::hello_extension() -{ - UtilityFunctions::print("Hello GDExtension!"); -} diff --git a/src/my_node.cpp b/src/my_node.cpp new file mode 100644 index 0000000..8889196 --- /dev/null +++ b/src/my_node.cpp @@ -0,0 +1,27 @@ +#include "my_node.hpp" + +#include + +#include +#include +#include + +using namespace godot; + +void MyNode::_bind_methods() +{ + ClassDB::bind_method(D_METHOD("hello_node"), &MyNode::hello_node); +} + +MyNode::MyNode() +{ +} + +MyNode::~MyNode() +{ +} + +void MyNode::hello_node() +{ + UtilityFunctions::print("Hello Node!"); +} diff --git a/src/example.hpp b/src/my_node.hpp similarity index 72% rename from src/example.hpp rename to src/my_node.hpp index 2fef158..369226b 100644 --- a/src/example.hpp +++ b/src/my_node.hpp @@ -8,16 +8,16 @@ using namespace godot; -class Example : public Node +class MyNode : public Node { - GDCLASS(Example, Node); + GDCLASS(MyNode, Node); protected: static void _bind_methods(); public: - Example(); - ~Example(); + MyNode(); + ~MyNode(); - void hello_extension(); + void hello_node(); }; diff --git a/src/my_singleton.cpp b/src/my_singleton.cpp new file mode 100644 index 0000000..a9cc740 --- /dev/null +++ b/src/my_singleton.cpp @@ -0,0 +1,27 @@ +#include "my_singleton.hpp" + +#include + +#include +#include +#include + +using namespace godot; + +void MySingleton::_bind_methods() +{ + ClassDB::bind_method(D_METHOD("hello_singleton"), &MySingleton::hello_singleton); +} + +MySingleton::MySingleton() +{ +} + +MySingleton::~MySingleton() +{ +} + +void MySingleton::hello_singleton() +{ + UtilityFunctions::print("Hello Singleton!"); +} diff --git a/src/my_singleton.hpp b/src/my_singleton.hpp new file mode 100644 index 0000000..a085c56 --- /dev/null +++ b/src/my_singleton.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include + +#include + +using namespace godot; + +class MySingleton : public Object +{ + GDCLASS(MySingleton, Object); + +protected: + static void _bind_methods(); + +public: + MySingleton(); + ~MySingleton(); + + void hello_singleton(); +}; diff --git a/src/register_types.cpp b/src/register_types.cpp index c319280..e369dee 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -4,17 +4,25 @@ #include #include +#include #include -#include "example.hpp" +#include "my_node.hpp" +#include "my_singleton.hpp" using namespace godot; +static MySingleton *_my_singleton; + void gdextension_initialize(ModuleInitializationLevel p_level) { if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { - ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + + _my_singleton = memnew(MySingleton); + Engine::get_singleton()->register_singleton("MySingleton", _my_singleton); } } @@ -22,6 +30,8 @@ void gdextension_terminate(ModuleInitializationLevel p_level) { if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { + Engine::get_singleton()->unregister_singleton("MySingleton"); + memdelete(_my_singleton); } }