1 Commits

Author SHA1 Message Date
435c16942c hook -> GodotHook 2022-08-19 05:16:20 +03:00
5 changed files with 34 additions and 59 deletions

View File

@ -9,7 +9,6 @@
## Compilation
- Download GodotHook and place it to the Godot `modules` folder.
- Rename `GodotHook-master` to `GodotHook`
- Compile Godot like usual using one of [these guides](https://docs.godotengine.org/en/stable/development/compiling/index.html).
Example compilation `Windows` command: `scons p=windows tools=yes -j4`
@ -17,8 +16,8 @@ Example compilation `Windows` command: `scons p=windows tools=yes -j4`
## Methods
```gdscript
hook.GetTable()
hook.Add(event: String, uid: String, function: Callable)
hook.Call(event: String, args: Array = [], defer: bool = false)
hook.Add(event: String, uid: String, function: FuncRef)
hook.Call(event: String, args: Array)
hook.Remove(event: String, uid: String)
```
@ -36,15 +35,14 @@ func printHookOutput1(a: String, b: String):
func printHookOutput2(a: String, b: String):
print(a, " ", b)
hook.Add("OnReady", "UniqueName1", Callable(self, "printHookOutput1"))
hook.Add("OnReady", "UniqueName2", Callable(self, "printHookOutput2"))
hook.Add("OnReady", "UniqueName1", funcref(self, "printHookOutput1"))
hook.Add("OnReady", "UniqueName2", funcref(self, "printHookOutput2"))
```
```gdscript
# any node script
func _ready():
hook.Call("OnReady", ["Hey!", "It's OnReady hook!"])
hook.Call("OnReady", ["Hey!", "It's OnReady hook, but deferred!"], true)
```
`hook.Remove` removes any listening event by its name and type:
@ -62,7 +60,3 @@ hook.Remove("OnReady", "UniqueName")
var hookTable: Dictionary = hook.GetTable()
```
## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=Mestima/GodotHook&type=Date)](https://star-history.com/#Mestima/GodotHook&Date)

View File

@ -6,33 +6,20 @@ Dictionary Hook::GetTable() {
return table;
}
void Hook::Add(String event, String uid, Callable function) {
void Hook::Add(String event, String uid, Ref<FuncRef> function) {
Dictionary tmp = table.get(event, Dictionary());
tmp[uid] = function;
table[event] = tmp;
}
void Hook::Call(String event, Array args = Array(), bool defer = false) {
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];
}
}
void Hook::Call(String event, Array args = Array()) {
for (int i = 0; i < table.size(); i++) {
Dictionary tmp = table.get(event, Dictionary());
if (!tmp.is_empty()) {
if (!tmp.empty()) {
Array keys = tmp.keys();
for (int key_i = 0; key_i < keys.size(); key_i++) {
Callable function = tmp[keys[key_i]];
if (!defer) {
Callable::CallError call_error;
function.callp(argptrs, args.size(), Variant(), call_error);
} else {
function.call_deferredp(argptrs, args.size());
}
Ref<FuncRef> function = tmp[keys[key_i]];
function->call_funcv(args);
}
} else {
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
@ -42,7 +29,7 @@ void Hook::Call(String event, Array args = Array(), bool defer = false) {
void Hook::Remove(String event, String uid) {
Dictionary tmp = table.get(event, Dictionary());
if (!tmp.is_empty()) {
if (!tmp.empty()) {
if (tmp.erase(uid)) {
table[event] = tmp;
} else {
@ -56,7 +43,7 @@ void Hook::Remove(String event, String uid) {
void Hook::_bind_methods() {
ClassDB::bind_method(D_METHOD("GetTable"), &Hook::GetTable);
ClassDB::bind_method(D_METHOD("Add", "event", "uid", "function"), &Hook::Add);
ClassDB::bind_method(D_METHOD("Call", "event", "args", "defer"), &Hook::Call, DEFVAL(Array()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("Call", "event", "args"), &Hook::Call);
ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove);
};

25
hook.h
View File

@ -3,27 +3,26 @@
#ifndef HOOK_H
#define HOOK_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"
#include "core/reference.h"
#include "core/func_ref.h"
#include "core/ustring.h"
#include "core/dictionary.h"
#include "core/array.h"
class Hook : public RefCounted {
GDCLASS(Hook, RefCounted);
class Hook : public Reference {
GDCLASS(Hook, Reference);
Dictionary table;
Dictionary table;
protected:
static void _bind_methods();
static void _bind_methods();
public:
Dictionary GetTable();
void Add(String event, String uid, Callable function);
void Call(String event, Array args, bool defer);
void Add(String event, String uid, Ref<FuncRef> function);
void Call(String event, Array args);
void Remove(String event, String uid);
Hook();
Hook();
};
#endif // HOOK_H

View File

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

View File

@ -1,7 +1,5 @@
/* register_types.h */
#include "modules/register_module_types.h"
void initialize_GodotHook_module(ModuleInitializationLevel p_level);
void uninitialize_GodotHook_module(ModuleInitializationLevel p_level);
void register_GodotHook_types();
void unregister_GodotHook_types();
/* yes, the word in the middle must be the same as the module folder name */