Description
Changelog
Reviews (0)
Godot Event Bus Addon
A lightweight, decoupled pub/sub event bus system for Godot 4.
The Event Bus addon allows nodes across different scenes and UI hierarchies to communicate seamlessly via string-based routes without creating direct scene dependencies or cluttering global singleton scripts with hardcoded signals.
๐ Features
- ๐ Decoupled Architecture: Send and receive messages across scenes without needing node references (
get_node,%UniqueNames, etc.). - ๐ ๏ธ No-Code Node Workflow: Use custom
EventProducerandEventListenernodes directly in the Godot inspector. - ๐ฏ Flexible Payload Extraction:
EventProducercan automatically extract and send a property value, call a method return value, or pass an entire node object reference visually from the inspector without code. - โก Pure Script / API Support: Direct usage via the global
EventBrokersingleton. - ๐งน Automatic Lifecycle Management:
EventListenerauto-subscribes on_enter_tree()and auto-unsubscribes on_exit_tree(). Stale callbacks are cleaned up automatically during publishing.
๐ฆ Installation
- Copy or clone the
addons/event_busdirectory into your Godot project'saddons/folder. - Open your Godot project and navigate to Project Settings > Plugins.
- Enable the Event Bus plugin.
> Note: Enabling the plugin automatically registers the
EventBrokersingleton in your project's Autoload settings.
๐ ๏ธ Usage Modes
Method 1: Node-Based (Visual / Inspector Workflow)
This approach requires minimal to no code and integrates directly with Godot's Signal system.
1. Listening for Events (EventListener)
- Add an
EventListenernode to any scene that needs to receive messages. - In the Inspector, set the
Routefield to your desired event key (e.g.,"player/health_changed"). - Select the
EventListenernode, go to the Node > Signals dock, and connect themessage_received(payload)signal to any function on your receiving node (e.g., updating a Health Bar UI).
EventProducer)
- Add an
EventProducernode to any scene that triggers events. - In the Inspector, set the
Routefield (e.g.,"player/health_changed"). - (Optional) Select a
Target Nodeunder Payload selector to enable visual payload extraction:Selector Type:PROPERTY: Select a specific property fromTarget Nodeto send as the payload.METHOD: Select a parameterless method fromTarget Node; its return value will be sent as the payload.OBJECT: Sends theTarget Nodeobject itself as the payload.
Selected Payload: Pick the target property or method from the inspector dropdown.
- Trigger the event:
- Via Editor Signals: Connect a node's signal (e.g., a
Button'spressedsignal or anArea2D'sbody_enteredsignal) directly toEventProducer.publish(). - Via Code: Call
$EventProducer.publish(payload)from a script.
- Via Editor Signals: Connect a node's signal (e.g., a
Method 2: Code / Script API Workflow
If you prefer writing pure GDScript without placing extra helper nodes in the scene tree, you can use the global EventBroker singleton directly.
# Publish an event with a payload
EventBroker.publish(&"player/health_changed", 80)
# Publish an event without payload
EventBroker.publish(&"game/paused")
func _ready() -> void:
# Subscribe a method to a topic route
EventBroker.subscribe(&"player/health_changed", _on_health_changed)
func _exit_tree() -> void:
# Always unsubscribe when the node is freed
EventBroker.unsubscribe(&"player/health_changed", _on_health_changed)
func _on_health_changed(payload: Variant) -> void:
print("Health updated:", payload)
๐ API Reference
EventBroker (Autoload Singleton)
| Method | Parameters | Description |
|---|---|---|
publish |
route: StringName, payload: Variant = null |
Sends an event to all subscribers listening on route. |
subscribe |
route: StringName, callable: Callable |
Registers a callback function for the specified route. |
unsubscribe |
route: StringName, callable: Callable |
Removes a callback registration for route. |
EventProducer (Node)
| Property / Method | Type | Description |
|---|---|---|
route |
StringName |
Default topic route for messages emitted by this producer. |
_target_node |
Node |
Node used to extract payload data from. |
_selector_type |
SelectorType (PROPERTY, METHOD, OBJECT) |
Specifies how payload data is retrieved from _target_node. |
_selected_payload |
StringName |
Property or method name on _target_node used as the payload source. |
publish() |
(payload = null, route_override = "") |
Publishes a message to route (or route_override). Evaluates target node payload if payload is null. |
EventListener (Node)
| Property / Signal | Type | Description |
|---|---|---|
_route |
String |
Topic route to listen to. Auto-subscribes on enter tree. |
message_received |
signal(payload) |
Emitted when a message is published to _route. |
๐ก Best Practices
- Consistent Route Naming: Use organized, namespaced route strings like
"game/state_changed","ui/score_updated", or"player/died". - Use StringNames: When calling
EventBrokerin script, prefix routes with&(e.g.,&"player/health_changed") for optimal StringName hashing performance in Godot 4. - Clean Up Manual Subscriptions: If using
EventBroker.subscribedirectly in GDScript, remember to callEventBroker.unsubscribein_exit_tree()to avoid memory leaks or unnecessary calls. (TheEventListenernode handles this automatically!).
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog for version v1.1.0
No changelog provided for this version.