Skip to reading
Action Lists & Events

23. Subscribe a script to a shared event

Edition 0.86.4.3

0.86.4.3 edition with revised lessons, updated cross-references and new cinematic/camera-safety supplements. Earlier examples, screenshots and registry figures retain their stated edition context.

Download PDFView illustrations

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.

Code example
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 true

Figure 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.

Page 27 of 189Permalink
Go to page