Compare commits

11 Commits

Author SHA1 Message Date
18a42c61d3 Update README.md 2023-10-09 23:48:38 +03:00
8f42c858ea args parameter is optional now 2023-10-09 23:46:52 +03:00
f336f48bbf Update README.md 2023-04-28 20:12:13 +03:00
6ba7ff7e1f Update to the latest version of godot
Callable class changed in a new version: call is callp and call_deferred is a call_deferredp in a new godot version
2022-08-19 03:56:18 +03:00
eeeee63f7b Update README.md 2022-05-17 23:48:40 +03:00
564ddd4aaa Module path renamed: hook -> GodotHook 2022-05-17 23:46:38 +03:00
ef1ab130c0 readme style fix 2022-05-16 16:24:59 +03:00
7f98fd6817 Merge branch 'main' of https://github.com/Mestima/GodotHook 2022-05-16 16:23:02 +03:00
bc55cacffc hook.Call might be deferred or not now (was always deferred) 2022-05-16 16:22:52 +03:00
f87fe302d0 Merge pull request #1 from Mestima/3.x
4.x Godot support
2022-05-16 14:05:11 +03:00
06587212b2 Module updated to 4.x Godot version 2022-05-16 14:01:00 +03:00
5 changed files with 59 additions and 34 deletions

View File

@ -9,6 +9,7 @@
## 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`
@ -16,8 +17,8 @@ Example compilation `Windows` command: `scons p=windows tools=yes -j4`
## Methods
```gdscript
hook.GetTable()
hook.Add(event: String, uid: String, function: FuncRef)
hook.Call(event: String, args: Array)
hook.Add(event: String, uid: String, function: Callable)
hook.Call(event: String, args: Array = [], defer: bool = false)
hook.Remove(event: String, uid: String)
```
@ -35,14 +36,15 @@ 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
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:
@ -60,3 +62,7 @@ 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,20 +6,33 @@ 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 = 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];
}
}
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]];
if (!defer) {
Callable::CallError call_error;
function.callp(argptrs, args.size(), Variant(), call_error);
} else {
function.call_deferredp(argptrs, args.size());
}
}
} else {
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
@ -29,7 +42,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 {
@ -43,7 +56,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"), &Hook::Call);
ClassDB::bind_method(D_METHOD("Call", "event", "args", "defer"), &Hook::Call, DEFVAL(Array()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove);
};

19
hook.h
View File

@ -3,14 +3,15 @@
#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 Reference {
GDCLASS(Hook, Reference);
class Hook : public RefCounted {
GDCLASS(Hook, RefCounted);
Dictionary table;
@ -19,8 +20,8 @@ protected:
public:
Dictionary GetTable();
void Add(String event, String uid, Ref<FuncRef> function);
void Call(String event, Array args);
void Add(String event, String uid, Callable function);
void Call(String event, Array args, bool defer);
void Remove(String event, String uid);
Hook();
};

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() {
void initialize_GodotHook_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_GodotHook_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_GodotHook_module(ModuleInitializationLevel p_level);
void uninitialize_GodotHook_module(ModuleInitializationLevel p_level);
/* yes, the word in the middle must be the same as the module folder name */