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
## 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

View File

@ -6,20 +6,28 @@ Dictionary Hook::GetTable() {
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());
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<FuncRef> 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 {

29
hook.h
View File

@ -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<FuncRef> 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

View File

@ -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<Hook>();
void initialize_hook_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
ClassDB::register_class<Hook>();
}
}
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;
}
}

View File

@ -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 */