mirror of
https://github.com/Mestima/GodotHook.git
synced 2025-06-27 20:59:30 +00:00
Compare commits
11 Commits
f336f48bbf
...
main
Author | SHA1 | Date | |
---|---|---|---|
5a6df8512f | |||
5300b67a9a | |||
805257da76 | |||
bab2506cc7 | |||
adb93c170d | |||
ad57544e9a | |||
3da035f582 | |||
dd24f3fd45 | |||
ed1903119a | |||
18a42c61d3 | |||
8f42c858ea |
@ -18,7 +18,7 @@ Example compilation `Windows` command: `scons p=windows tools=yes -j4`
|
|||||||
```gdscript
|
```gdscript
|
||||||
hook.GetTable()
|
hook.GetTable()
|
||||||
hook.Add(event: String, uid: String, function: Callable)
|
hook.Add(event: String, uid: String, function: Callable)
|
||||||
hook.Call(event: String, args: Array, defer: bool = false)
|
hook.Call(event: String, args: Array = [], defer: bool = false)
|
||||||
hook.Remove(event: String, uid: String)
|
hook.Remove(event: String, uid: String)
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ hook.Remove(event: String, uid: String)
|
|||||||
```gdscript
|
```gdscript
|
||||||
# autorun.gd
|
# autorun.gd
|
||||||
|
|
||||||
var hook: Hook = Hook.new()
|
@onready var hook: Hook = Hook.new()
|
||||||
|
|
||||||
func printHookOutput1(a: String, b: String):
|
func printHookOutput1(a: String, b: String):
|
||||||
print(a, " ", b)
|
print(a, " ", b)
|
||||||
|
31
hook.cpp
31
hook.cpp
@ -12,7 +12,7 @@ void Hook::Add(String event, String uid, Callable function) {
|
|||||||
table[event] = tmp;
|
table[event] = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hook::Call(String event, Array args, bool defer = false) {
|
void Hook::Call(String event, Array args = Array(), bool defer = false) {
|
||||||
const Variant **argptrs = nullptr;
|
const Variant **argptrs = nullptr;
|
||||||
if (args.size() > 0) {
|
if (args.size() > 0) {
|
||||||
argptrs = (const Variant **)alloca(sizeof(Variant *) * args.size());
|
argptrs = (const Variant **)alloca(sizeof(Variant *) * args.size());
|
||||||
@ -21,22 +21,21 @@ void Hook::Call(String event, Array args, bool defer = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < table.size(); i++) {
|
Dictionary tmp = table.get(event, Dictionary());
|
||||||
Dictionary tmp = table.get(event, Dictionary());
|
if (!tmp.is_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++) {
|
Callable function = tmp[keys[key_i]];
|
||||||
Callable function = tmp[keys[key_i]];
|
if (!defer) {
|
||||||
if (!defer) {
|
Callable::CallError call_error;
|
||||||
Callable::CallError call_error;
|
Variant r_return_variant = Variant();
|
||||||
function.callp(argptrs, args.size(), Variant(), call_error);
|
function.callp(argptrs, args.size(), r_return_variant, call_error);
|
||||||
} else {
|
} else {
|
||||||
function.call_deferredp(argptrs, args.size());
|
function.call_deferredp(argptrs, args.size());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +55,7 @@ void Hook::Remove(String event, String uid) {
|
|||||||
void Hook::_bind_methods() {
|
void Hook::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("GetTable"), &Hook::GetTable);
|
ClassDB::bind_method(D_METHOD("GetTable"), &Hook::GetTable);
|
||||||
ClassDB::bind_method(D_METHOD("Add", "event", "uid", "function"), &Hook::Add);
|
ClassDB::bind_method(D_METHOD("Add", "event", "uid", "function"), &Hook::Add);
|
||||||
ClassDB::bind_method(D_METHOD("Call", "event", "args", "defer"), &Hook::Call, DEFVAL(false));
|
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);
|
ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user