A lite custom event system for Godot Engine
Godot version | Supported | Branch |
---|---|---|
3.x | yes | 3.x |
4.x | yes | main |
- Download GodotHook and place it to the Godot
modules
folder. - Rename
GodotHook-master
toGodotHook
- Compile Godot like usual using one of these guides.
Example compilation Windows
command: scons p=windows tools=yes -j4
hook.GetTable()
hook.Add(event: String, uid: String, function: Callable)
hook.Call(event: String, args: Array = [], defer: bool = false)
hook.Remove(event: String, uid: String)
hook.Add
creates a new listening event and hook.Call
executes all listening events of the same type, for example:
# autorun.gd
@onready var hook: Hook = Hook.new()
func printHookOutput1(a: String, b: String):
print(a, " ", b)
func printHookOutput2(a: String, b: String):
print(a, " ", b)
hook.Add("OnReady", "UniqueName1", Callable(self, "printHookOutput1"))
hook.Add("OnReady", "UniqueName2", Callable(self, "printHookOutput2"))
# 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:
# can be executed at any place
hook.Remove("OnReady", "UniqueName")
hook.GetTable
returns a Dictionary
with all added listening events
# can be executed at any place
var hookTable: Dictionary = hook.GetTable()