mirror of
https://github.com/Mestima/GodotHook.git
synced 2025-05-04 07:10:23 +00:00
hook.Call might be deferred or not now (was always deferred)
This commit is contained in:
parent
06587212b2
commit
bc55cacffc
@ -17,7 +17,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)
|
hook.Call(event: String, args: Array, defer: bool = false)
|
||||||
hook.Remove(event: String, uid: String)
|
hook.Remove(event: String, uid: String)
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -43,6 +43,7 @@ hook.Add("OnReady", "UniqueName2", Callable(self, "printHookOutput2"))
|
|||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
hook.Call("OnReady", ["Hey!", "It's OnReady hook!"])
|
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:
|
`hook.Remove` removes any listening event by its name and type:
|
||||||
|
9
hook.cpp
9
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) {
|
void Hook::Call(String event, Array args, 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());
|
||||||
@ -27,8 +27,13 @@ void Hook::Call(String event, Array args) {
|
|||||||
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) {
|
||||||
|
Callable::CallError call_error;
|
||||||
|
function.call(argptrs, args.size(), Variant(), call_error);
|
||||||
|
} else {
|
||||||
function.call_deferred(argptrs, args.size());
|
function.call_deferred(argptrs, args.size());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
|
ERR_PRINT("Hook event '" + event + "' cannot be found or empty.");
|
||||||
}
|
}
|
||||||
@ -51,7 +56,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"), &Hook::Call);
|
ClassDB::bind_method(D_METHOD("Call", "event", "args", "defer"), &Hook::Call, DEFVAL(false));
|
||||||
ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove);
|
ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
2
hook.h
2
hook.h
@ -21,7 +21,7 @@ protected:
|
|||||||
public:
|
public:
|
||||||
Dictionary GetTable();
|
Dictionary GetTable();
|
||||||
void Add(String event, String uid, Callable function);
|
void Add(String event, String uid, Callable function);
|
||||||
void Call(String event, Array args);
|
void Call(String event, Array args, bool defer);
|
||||||
void Remove(String event, String uid);
|
void Remove(String event, String uid);
|
||||||
Hook();
|
Hook();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user