add singleton example

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

@ -2,5 +2,5 @@ extends Node
func _ready() -> void: func _ready() -> void:
print("Hello GDScript!") print("Hello GDScript!")
# TODO: Static https://github.com/godotengine/godot/issues/61963 $MyNode.hello_node()
Example.new().hello_extension() MySingleton.hello_singleton()

@ -4,3 +4,5 @@
[node name="Demo" type="Node"] [node name="Demo" type="Node"]
script = ExtResource( "1_6gvlp" ) script = ExtResource( "1_6gvlp" )
[node name="MyNode" type="MyNode" parent="."]

@ -1,28 +0,0 @@
#include "example.hpp"
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/classes/label.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
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!");
}

@ -0,0 +1,27 @@
#include "my_node.hpp"
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/classes/label.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
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!");
}

@ -8,16 +8,16 @@
using namespace godot; using namespace godot;
class Example : public Node class MyNode : public Node
{ {
GDCLASS(Example, Node); GDCLASS(MyNode, Node);
protected: protected:
static void _bind_methods(); static void _bind_methods();
public: public:
Example(); MyNode();
~Example(); ~MyNode();
void hello_extension(); void hello_node();
}; };

@ -0,0 +1,27 @@
#include "my_singleton.hpp"
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/classes/label.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
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!");
}

@ -0,0 +1,23 @@
#pragma once
#include <godot_cpp/classes/object.hpp>
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/classes/viewport.hpp>
#include <godot_cpp/core/binder_common.hpp>
using namespace godot;
class MySingleton : public Object
{
GDCLASS(MySingleton, Object);
protected:
static void _bind_methods();
public:
MySingleton();
~MySingleton();
void hello_singleton();
};

@ -4,17 +4,25 @@
#include <godot_cpp/core/class_db.hpp> #include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp> #include <godot_cpp/core/defs.hpp>
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/godot.hpp> #include <godot_cpp/godot.hpp>
#include "example.hpp" #include "my_node.hpp"
#include "my_singleton.hpp"
using namespace godot; using namespace godot;
static MySingleton *_my_singleton;
void gdextension_initialize(ModuleInitializationLevel p_level) void gdextension_initialize(ModuleInitializationLevel p_level)
{ {
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE)
{ {
ClassDB::register_class<Example>(); ClassDB::register_class<MyNode>();
ClassDB::register_class<MySingleton>();
_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) if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE)
{ {
Engine::get_singleton()->unregister_singleton("MySingleton");
memdelete(_my_singleton);
} }
} }

Loading…
Cancel
Save