Skip to reading
Action Lists & Events

22. Call the same actions from a script

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 PDF page

A text script is useful when a short conditional operation is clearer in code. The supported scripting languages share the action names, while each language keeps its own syntax. The following is a complete GDScript example for the editor's standard PCA script base.

Code example
func run(_context: Dictionary = {}) -> Variant:
    await ChangeVariable({
        "name": "powered", "operation": "set", "value": true
    })
    await PlaySound({"assetId": "chime", "channel": "puzzle"})
    await ShowMessage({"text": "The signal is on.",
                       "duration": 2.2})
    return true

1. Create a GDScript record and give it a stable ID, such as power_signal.

2. Enter the code, validate it and click Apply Changes. Save the project.

3. On the lever or GUI event, choose Run Visual Script and select that text script.

4. Test the call through its real event, not just the editor's Run Script button.

Await means the caller waits for the action routine to return. It cannot force an action to wait for something the routine only starts. Play Sound starts playback; Show Message waits for its presentation routine. Animation timing needs the same distinction.

GDScript records without an explicit extends line receive PCA's standard script base. A custom base must inherit the PCA base to use these helpers. Godot's self still means the script instance; this means the calling game record.

Page 26 of 189Permalink
Go to page