Skip to reading
GDScript Programming & Godot

22. Your run function and its caller

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
func run(call_context: Dictionary = {}) -> String:
    var verb_id: String = str(call_context.get("verb", ""))
    var source_id: String = str(call_context.get("entityId", ""))
    Log("Verb: " + verb_id + "; source: " + source_id)
    return source_id

The caller context explains why the script ran. An object response, a GUI button, a parser command and a manual Run Script call can provide different keys. Read an optional key with get and a fallback.

The inherited context member is refreshed for the call, and PCA’s helpers pass it on to nested actions/scripts. The parameter shown above is a local copy/reference to the supplied dictionary; naming it call_context avoids hiding the base member.

For initial inspection, Log(ToJSON(call_context)) shows ordinary data. Do not assume every context contains entityId, itemId, guiId, elementId or a parser command. Discover the relevant payload in a small test, then depend only on the keys that route supplies.

Page 31 of 378Permalink
Go to page