mirror of
https://github.com/Mestima/GodotHook.git
synced 2025-06-27 20:59:30 +00:00
Compare commits
11 Commits
3.x
...
18a42c61d3
Author | SHA1 | Date | |
---|---|---|---|
18a42c61d3 | |||
8f42c858ea | |||
f336f48bbf | |||
6ba7ff7e1f | |||
eeeee63f7b | |||
564ddd4aaa | |||
ef1ab130c0 | |||
7f98fd6817 | |||
bc55cacffc | |||
f87fe302d0 | |||
06587212b2 |
14
README.md
14
README.md
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
## Compilation
|
## Compilation
|
||||||
- Download GodotHook and place it to the Godot `modules` folder.
|
- 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).
|
- 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`
|
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
|
## Methods
|
||||||
```gdscript
|
```gdscript
|
||||||
hook.GetTable()
|
hook.GetTable()
|
||||||
hook.Add(event: String, uid: String, function: FuncRef)
|
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)
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -35,14 +36,15 @@ func printHookOutput1(a: String, b: String):
|
|||||||
func printHookOutput2(a: String, b: String):
|
func printHookOutput2(a: String, b: String):
|
||||||
print(a, " ", b)
|
print(a, " ", b)
|
||||||
|
|
||||||
hook.Add("OnReady", "UniqueName1", funcref(self, "printHookOutput1"))
|
hook.Add("OnReady", "UniqueName1", Callable(self, "printHookOutput1"))
|
||||||
hook.Add("OnReady", "UniqueName2", funcref(self, "printHookOutput2"))
|
hook.Add("OnReady", "UniqueName2", Callable(self, "printHookOutput2"))
|
||||||
```
|
```
|
||||||
```gdscript
|
```gdscript
|
||||||
# any node script
|
# any node script
|
||||||
|
|
||||||
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:
|
||||||
@ -60,3 +62,7 @@ hook.Remove("OnReady", "UniqueName")
|
|||||||
|
|
||||||
var hookTable: Dictionary = hook.GetTable()
|
var hookTable: Dictionary = hook.GetTable()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Star History
|
||||||
|
|
||||||
|
[](https://star-history.com/#Mestima/GodotHook&Date)
|
||||||
|
27
hook.cpp
27
hook.cpp
@ -6,20 +6,33 @@ Dictionary Hook::GetTable() {
|
|||||||
return table;
|
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());
|
Dictionary tmp = table.get(event, Dictionary());
|
||||||
tmp[uid] = function;
|
tmp[uid] = function;
|
||||||
table[event] = tmp;
|
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++) {
|
for (int i = 0; i < table.size(); i++) {
|
||||||
Dictionary tmp = table.get(event, Dictionary());
|
Dictionary tmp = table.get(event, Dictionary());
|
||||||
if (!tmp.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++) {
|
||||||
Ref<FuncRef> function = tmp[keys[key_i]];
|
Callable function = tmp[keys[key_i]];
|
||||||
function->call_funcv(args);
|
if (!defer) {
|
||||||
|
Callable::CallError call_error;
|
||||||
|
function.callp(argptrs, args.size(), Variant(), call_error);
|
||||||
|
} else {
|
||||||
|
function.call_deferredp(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.");
|
||||||
@ -29,7 +42,7 @@ void Hook::Call(String event, Array args = Array()) {
|
|||||||
|
|
||||||
void Hook::Remove(String event, String uid) {
|
void Hook::Remove(String event, String uid) {
|
||||||
Dictionary tmp = table.get(event, Dictionary());
|
Dictionary tmp = table.get(event, Dictionary());
|
||||||
if (!tmp.empty()) {
|
if (!tmp.is_empty()) {
|
||||||
if (tmp.erase(uid)) {
|
if (tmp.erase(uid)) {
|
||||||
table[event] = tmp;
|
table[event] = tmp;
|
||||||
} else {
|
} else {
|
||||||
@ -43,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(Array()), DEFVAL(false));
|
||||||
ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove);
|
ClassDB::bind_method(D_METHOD("Remove", "event", "uid"), &Hook::Remove);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
31
hook.h
31
hook.h
@ -3,26 +3,27 @@
|
|||||||
#ifndef HOOK_H
|
#ifndef HOOK_H
|
||||||
#define HOOK_H
|
#define HOOK_H
|
||||||
|
|
||||||
#include "core/reference.h"
|
#include "core/object/ref_counted.h"
|
||||||
#include "core/func_ref.h"
|
#include "core/string/ustring.h"
|
||||||
#include "core/ustring.h"
|
#include "core/variant/variant.h"
|
||||||
#include "core/dictionary.h"
|
#include "core/variant/callable.h"
|
||||||
#include "core/array.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:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dictionary GetTable();
|
Dictionary GetTable();
|
||||||
void Add(String event, String uid, Ref<FuncRef> 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();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HOOK_H
|
#endif // HOOK_H
|
@ -1,14 +1,17 @@
|
|||||||
/* register_types.cpp */
|
/* register_types.cpp */
|
||||||
|
|
||||||
#include "register_types.h"
|
#include "register_types.h"
|
||||||
|
#include "core/object/class_db.h"
|
||||||
#include "core/class_db.h"
|
|
||||||
#include "hook.h"
|
#include "hook.h"
|
||||||
|
|
||||||
void register_hook_types() {
|
void initialize_GodotHook_module(ModuleInitializationLevel p_level) {
|
||||||
ClassDB::register_class<Hook>();
|
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||||
|
ClassDB::register_class<Hook>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void unregister_hook_types() {
|
void uninitialize_GodotHook_module(ModuleInitializationLevel p_level) {
|
||||||
// Nothing to do here in this example.
|
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
/* register_types.h */
|
/* register_types.h */
|
||||||
|
|
||||||
void register_hook_types();
|
#include "modules/register_module_types.h"
|
||||||
void unregister_hook_types();
|
|
||||||
|
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 */
|
/* yes, the word in the middle must be the same as the module folder name */
|
Reference in New Issue
Block a user