20 Commits
3.x ... main

Author SHA1 Message Date
5a6df8512f Updated docs according godot v4 2024-03-31 15:16:49 +03:00
5300b67a9a Fixed bug that static var cannot be created in method arguments 2024-03-11 20:36:41 +03:00
805257da76 fixed bug when hook.Call calls a hook more than 1 time 2023-10-10 23:50:49 +03:00
bab2506cc7 Revert "Hook class methods are now following to lowercase naming convention"
This reverts commit ed1903119a.
2023-10-10 14:36:33 +03:00
adb93c170d Revert "README updated according lowercase naming convention"
This reverts commit dd24f3fd45.
2023-10-10 14:36:32 +03:00
ad57544e9a Revert "Added note about uppercase to lowercase method names changing"
This reverts commit 3da035f582.
2023-10-10 14:36:28 +03:00
3da035f582 Added note about uppercase to lowercase method names changing 2023-10-10 13:10:22 +03:00
dd24f3fd45 README updated according lowercase naming convention 2023-10-10 13:07:06 +03:00
ed1903119a Hook class methods are now following to lowercase naming convention 2023-10-10 13:05:16 +03:00
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 66 additions and 42 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)
```
@ -27,7 +28,7 @@ hook.Remove(event: String, uid: String)
```gdscript
# autorun.gd
var hook: Hook = Hook.new()
@onready var hook: Hook = Hook.new()
func printHookOutput1(a: String, b: String):
print(a, " ", b)
@ -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,30 +6,42 @@ 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()) {
for (int i = 0; i < table.size(); i++) {
Dictionary tmp = table.get(event, Dictionary());
if (!tmp.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);
}
} else {
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
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];
}
}
Dictionary tmp = table.get(event, Dictionary());
if (!tmp.is_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;
Variant r_return_variant = Variant();
function.callp(argptrs, args.size(), r_return_variant, call_error);
} else {
function.call_deferredp(argptrs, args.size());
}
}
} else {
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
}
}
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 +55,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);
};

31
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 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();
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_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 */