Module updated to 4.x Godot version

This commit is contained in:
Mestima 2022-05-16 14:01:00 +03:00
parent 1eeb43f801
commit 06587212b2
5 changed files with 49 additions and 35 deletions

View File

@ -2,10 +2,10 @@
A lite custom event system for Godot Engine A lite custom event system for Godot Engine
## Supported versions ## Supported versions
| Godot version | Supported | | Godot version | Supported | Branch |
| - | - | | - | - | - |
| 3.x | yes | | 3.x | yes | 3.x |
| 4.x | not yet | | 4.x | yes | main |
## Compilation ## Compilation
- Download GodotHook and place it to the Godot `modules` folder. - 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 ## Methods
```gdscript ```gdscript
hook.GetTable() 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.Call(event: String, args: Array)
hook.Remove(event: String, uid: String) hook.Remove(event: String, uid: String)
``` ```
@ -35,8 +35,8 @@ func printHookOutput1(a: String, b: String):
func printHookOutput2(a: String, b: String): func printHookOutput2(a: String, b: String):
print(a, " ", b) print(a, " ", b)
hook.Add("OnReady", "UniqueName1", funcref(self, "printHookOutput1")) hook.Add("OnReady", "UniqueName1", Callable(self, "printHookOutput1"))
hook.Add("OnReady", "UniqueName2", funcref(self, "printHookOutput2")) hook.Add("OnReady", "UniqueName2", Callable(self, "printHookOutput2"))
``` ```
```gdscript ```gdscript
# any node script # any node script

View File

@ -6,20 +6,28 @@ Dictionary Hook::GetTable() {
return table; return table;
} }
void Hook::Add(String event, String uid, Ref<FuncRef> function) { void Hook::Add(String event, String uid, Callable function) {
Dictionary tmp = table.get(event, Dictionary()); Dictionary tmp = table.get(event, Dictionary());
tmp[uid] = function; tmp[uid] = function;
table[event] = tmp; 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++) { for (int i = 0; i < table.size(); i++) {
Dictionary tmp = table.get(event, Dictionary()); Dictionary tmp = table.get(event, Dictionary());
if (!tmp.empty()) { if (!tmp.is_empty()) {
Array keys = tmp.keys(); Array keys = tmp.keys();
for (int key_i = 0; key_i < keys.size(); key_i++) { for (int key_i = 0; key_i < keys.size(); key_i++) {
Ref<FuncRef> function = tmp[keys[key_i]]; Callable function = tmp[keys[key_i]];
function->call_funcv(args); function.call_deferred(argptrs, args.size());
} }
} else { } else {
ERR_PRINT("Hook event '" + event + "' cannot be found or empty."); 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) { void Hook::Remove(String event, String uid) {
Dictionary tmp = table.get(event, Dictionary()); Dictionary tmp = table.get(event, Dictionary());
if (!tmp.empty()) { if (!tmp.is_empty()) {
if (tmp.erase(uid)) { if (tmp.erase(uid)) {
table[event] = tmp; table[event] = tmp;
} else { } else {

17
hook.h
View File

@ -3,14 +3,15 @@
#ifndef HOOK_H #ifndef HOOK_H
#define HOOK_H #define HOOK_H
#include "core/reference.h" #include "core/object/ref_counted.h"
#include "core/func_ref.h" #include "core/string/ustring.h"
#include "core/ustring.h" #include "core/variant/variant.h"
#include "core/dictionary.h" #include "core/variant/callable.h"
#include "core/array.h" #include "core/variant/array.h"
#include "core/variant/dictionary.h"
class Hook : public Reference { class Hook : public RefCounted {
GDCLASS(Hook, Reference); GDCLASS(Hook, RefCounted);
Dictionary table; Dictionary table;
@ -19,7 +20,7 @@ protected:
public: public:
Dictionary GetTable(); Dictionary GetTable();
void Add(String event, String uid, Ref<FuncRef> function); void Add(String event, String uid, Callable function);
void Call(String event, Array args); void Call(String event, Array args);
void Remove(String event, String uid); void Remove(String event, String uid);
Hook(); Hook();

View File

@ -1,14 +1,17 @@
/* register_types.cpp */ /* register_types.cpp */
#include "register_types.h" #include "register_types.h"
#include "core/object/class_db.h"
#include "core/class_db.h"
#include "hook.h" #include "hook.h"
void register_hook_types() { void initialize_hook_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
ClassDB::register_class<Hook>(); ClassDB::register_class<Hook>();
}
} }
void unregister_hook_types() { void uninitialize_hook_module(ModuleInitializationLevel p_level) {
// Nothing to do here in this example. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
} }

View File

@ -1,5 +1,7 @@
/* register_types.h */ /* register_types.h */
void register_hook_types(); #include "modules/register_module_types.h"
void unregister_hook_types();
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 */ /* yes, the word in the middle must be the same as the module folder name */