From 06587212b2da073c5759a522003aa162eb2813b8 Mon Sep 17 00:00:00 2001 From: Mestima Date: Mon, 16 May 2022 14:01:00 +0300 Subject: [PATCH] Module updated to 4.x Godot version --- README.md | 14 +++++++------- hook.cpp | 20 ++++++++++++++------ hook.h | 29 +++++++++++++++-------------- register_types.cpp | 15 +++++++++------ register_types.h | 6 ++++-- 5 files changed, 49 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index ea58821..fbdddd1 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ A lite custom event system for Godot Engine ## Supported versions -| Godot version | Supported | -| - | - | -| 3.x | yes | -| 4.x | not yet | +| Godot version | Supported | Branch | +| - | - | - | +| 3.x | yes | 3.x | +| 4.x | yes | main | ## Compilation - Download GodotHook and place it to the Godot `modules` folder. @@ -16,7 +16,7 @@ Example compilation `Windows` command: `scons p=windows tools=yes -j4` ## Methods ```gdscript hook.GetTable() -hook.Add(event: String, uid: String, function: FuncRef) +hook.Add(event: String, uid: String, function: Callable) hook.Call(event: String, args: Array) hook.Remove(event: String, uid: String) ``` @@ -35,8 +35,8 @@ func printHookOutput1(a: String, b: String): func printHookOutput2(a: String, b: String): print(a, " ", b) -hook.Add("OnReady", "UniqueName1", funcref(self, "printHookOutput1")) -hook.Add("OnReady", "UniqueName2", funcref(self, "printHookOutput2")) +hook.Add("OnReady", "UniqueName1", Callable(self, "printHookOutput1")) +hook.Add("OnReady", "UniqueName2", Callable(self, "printHookOutput2")) ``` ```gdscript # any node script diff --git a/hook.cpp b/hook.cpp index dbb9cad..3f98eb4 100644 --- a/hook.cpp +++ b/hook.cpp @@ -6,20 +6,28 @@ Dictionary Hook::GetTable() { return table; } -void Hook::Add(String event, String uid, Ref function) { +void Hook::Add(String event, String uid, Callable function) { Dictionary tmp = table.get(event, Dictionary()); tmp[uid] = function; table[event] = tmp; } -void Hook::Call(String event, Array args = Array()) { +void Hook::Call(String event, Array args) { + const Variant **argptrs = nullptr; + if (args.size() > 0) { + argptrs = (const Variant **)alloca(sizeof(Variant *) * args.size()); + for (int i = 0; i < args.size(); i++) { + argptrs[i] = &args[i]; + } + } + for (int i = 0; i < table.size(); i++) { Dictionary tmp = table.get(event, Dictionary()); - if (!tmp.empty()) { + if (!tmp.is_empty()) { Array keys = tmp.keys(); for (int key_i = 0; key_i < keys.size(); key_i++) { - Ref function = tmp[keys[key_i]]; - function->call_funcv(args); + Callable function = tmp[keys[key_i]]; + function.call_deferred(argptrs, args.size()); } } else { ERR_PRINT("Hook event '" + event + "' cannot be found or empty."); @@ -29,7 +37,7 @@ void Hook::Call(String event, Array args = Array()) { void Hook::Remove(String event, String uid) { Dictionary tmp = table.get(event, Dictionary()); - if (!tmp.empty()) { + if (!tmp.is_empty()) { if (tmp.erase(uid)) { table[event] = tmp; } else { diff --git a/hook.h b/hook.h index fe8ca30..400d6b8 100644 --- a/hook.h +++ b/hook.h @@ -3,26 +3,27 @@ #ifndef HOOK_H #define HOOK_H -#include "core/reference.h" -#include "core/func_ref.h" -#include "core/ustring.h" -#include "core/dictionary.h" -#include "core/array.h" +#include "core/object/ref_counted.h" +#include "core/string/ustring.h" +#include "core/variant/variant.h" +#include "core/variant/callable.h" +#include "core/variant/array.h" +#include "core/variant/dictionary.h" + +class Hook : public RefCounted { + GDCLASS(Hook, RefCounted); + + Dictionary table; -class Hook : public Reference { - GDCLASS(Hook, Reference); - - Dictionary table; - protected: - static void _bind_methods(); - + static void _bind_methods(); + public: Dictionary GetTable(); - void Add(String event, String uid, Ref function); + void Add(String event, String uid, Callable function); void Call(String event, Array args); void Remove(String event, String uid); - Hook(); + Hook(); }; #endif // HOOK_H \ No newline at end of file diff --git a/register_types.cpp b/register_types.cpp index 9ef1c1e..c29a85f 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -1,14 +1,17 @@ /* register_types.cpp */ #include "register_types.h" - -#include "core/class_db.h" +#include "core/object/class_db.h" #include "hook.h" -void register_hook_types() { - ClassDB::register_class(); +void initialize_hook_module(ModuleInitializationLevel p_level) { + if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { + ClassDB::register_class(); + } } -void unregister_hook_types() { - // Nothing to do here in this example. +void uninitialize_hook_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } } \ No newline at end of file diff --git a/register_types.h b/register_types.h index d6b73f6..47f63cd 100644 --- a/register_types.h +++ b/register_types.h @@ -1,5 +1,7 @@ /* register_types.h */ -void register_hook_types(); -void unregister_hook_types(); +#include "modules/register_module_types.h" + +void initialize_hook_module(ModuleInitializationLevel p_level); +void uninitialize_hook_module(ModuleInitializationLevel p_level); /* yes, the word in the middle must be the same as the module folder name */ \ No newline at end of file