23. Subscribe a script to a shared event
A shared event lets a script listen without being attached to one particular lever or button. For a simple test, count explicitly emitted custom events.
1. Create a numeric game variable custom_count starting at 0.
2. Create a GDScript named Event Counter, ID event_log.
3. In Event Functions, check OnCustomEvent. Keep global instance alive is optional for a counter stored in a game variable; it is useful when the script has its own persistent instance state.
4. Add the matching handler below, validate, Apply Changes and save.
func on_custom_event(event: Dictionary = {}) -> Variant:
if event.get("reason", "") != "signal_powered":
return false
await ChangeVariable({"name": "custom_count",
"operation": "add", "value": 1})
return trueFigure 19. The subscription checkbox and matching on_custom_event function are both part of the setup.
A Global Script checkbox controls instance lifetime. It is not a shortcut for subscribing to every event. Likewise, writing on_custom_event without the subscription does not make this script a universal listener.
