Skip to content

GraphScript

Inherits: Script < Resource < RefCounted < Object

A script made of steps joined by wires, stored as an .ograph file.

A graph script holds functions, variables and signals. A function is a set of steps joined by wires. A flow wire says which step runs next. A data wire carries a value from one step to another.

A graph script attaches to an entity like any other script. It runs when it is attached and a function is called on it. Its base_type says which class it extends.

Every edit method changes one thing and returns an Error. When the edit fails, the script stays as it was and nothing is emitted. When it works, the script compiles again, running instances update and Resource.changed is emitted. The methods take and return text for anything that is not a string, a number or a bool, so a caller that only passes those types can use all of them. A text value is written the way @GlobalScope.var_to_str() writes it.

Each function starts at an entry step. A function stops with an error when it runs more steps in one resume than the ProjectSettings.graph_script/runtime/step_budget allows. A step that waits, such as a delay, hands the caller a GraphCall instead of a result.

Type Name Default
StringName base_type &""
Array functions []
Array signals []
Array variables []
Returns Method
Error add_function(name: String)
Error add_signal(name: String)
Error add_step(function: String, id: int, kind: String)
Error add_variable(name: String, type: int)
Error connect_steps(function: String, from_step: int, from_port: int, to_step: int, to_port: int)
Error disconnect_steps(function: String, from_step: int, from_port: int, to_step: int, to_port: int)
String find_overrides() const
String find_steps(text: String, dragged_side: String, type: int, sort: String) const
String get_document() const
String get_error() const
String get_payload(function: String, ids_text: String) const
String get_snapshot() const
int get_valid_step_id(function: String) const
Array get_warnings() const
Error move_step(function: String, id: int, x: float, y: float)
String paste_payload(function: String, payload_text: String, x: float, y: float, conflict: String)
Error remove_frame(function: String, id: int)
Error remove_function(name: String)
Error remove_signal(name: String)
Error remove_step(function: String, id: int)
Error remove_variable(name: String)
Error rename_function(name: String, new_name: String)
Error rename_signal(name: String, new_name: String)
Error rename_variable(name: String, new_name: String)
Error restore_snapshot(snapshot_text: String)
Error set_frame(function: String, frame_text: String)
Error set_signal_arguments(name: String, arguments_text: String)
Error set_step_parameter(function: String, id: int, parameter: String, value_text: String)
Error set_step_pins(function: String, id: int, count: int)
Error set_variable_default(name: String, value_text: String)
Error set_variable_exported(name: String, exported: bool)
Error set_variable_hint(name: String, hint: int, hint_string: String)
Error set_variable_info(name: String, category: String, description: String)
Error set_variable_type(name: String, type: int)

base_type

StringName base_type = &""

The name of the class this script extends. A script with no base type extends Object. It decides which virtual functions find_overrides() offers.

functions

Array functions = []

The functions of the script, as an array of dictionaries. Each has name, steps, wires and frames. Setting it replaces every function. A value that is not a valid list of functions prints an error and changes nothing. Use the edit methods to change one thing.

signals

Array signals = []

The signals of the script, as an array of dictionaries. Each has a name and its arguments. Setting it replaces every signal. A value that is not a valid list of signals prints an error and changes nothing.

variables

Array variables = []

The variables of the script, as an array of dictionaries. Each has name, type, default and exported, and may have hint, hint_string, category and description. Setting it replaces every variable. A value that is not a valid list of variables prints an error and changes nothing.

add_function

Error add_function(name: String)

Adds an empty function called name. Returns OK, ERR_INVALID_PARAMETER when the name is not a valid identifier, or ERR_ALREADY_EXISTS when a function has that name.

add_signal

Error add_signal(name: String)

Adds a signal called name with no arguments. Returns OK, ERR_INVALID_PARAMETER when the name is not a valid identifier, or ERR_ALREADY_EXISTS when a signal has that name. Set its arguments with set_signal_arguments().

add_step

Error add_step(function: String, id: int, kind: String)

Adds a step of the given kind to function, with the number id. Use get_valid_step_id() to get a free number. Returns OK, ERR_INVALID_PARAMETER when id is below 1 or the kind is unknown, ERR_ALREADY_EXISTS when the number is taken, or ERR_DOES_NOT_EXIST when the function is missing. Use find_steps() to list the kinds.

add_variable

Error add_variable(name: String, type: int)

Adds a variable called name of the given Variant type. Its default is the default value of that type. Returns OK, ERR_INVALID_PARAMETER when the name is not valid or the type is not a Variant type, or ERR_ALREADY_EXISTS when a variable has that name.

connect_steps

Error connect_steps(function: String, from_step: int, from_port: int, to_step: int, to_port: int)

Joins output from_port of from_step to input to_port of to_step in function. Returns OK, ERR_DOES_NOT_EXIST when a step or the function is missing, ERR_INVALID_PARAMETER when the two ports do not fit or both ends are the same step, ERR_ALREADY_EXISTS when the wire is already there, ERR_ALREADY_IN_USE when the data input already has a wire, or ERR_UNAVAILABLE when a step has a kind the engine does not know.

disconnect_steps

Error disconnect_steps(function: String, from_step: int, from_port: int, to_step: int, to_port: int)

Removes the wire from output from_port of from_step to input to_port of to_step in function. Returns OK or ERR_DOES_NOT_EXIST when there is no such wire.

find_overrides

String find_overrides() const

Returns a JSON array of the virtual functions that this script can override, for its base_type. A script with no base type is offered what an Object can override. Each entry is an object with kind, group, label and parameters, like the entries of find_steps(). Choosing an entry means adding a function named by its label.

The list is empty in a build without debug features.

find_steps

String find_steps(text: String, dragged_side: String, type: int, sort: String) const

Returns a JSON array of the steps that a person can add, best match first. Each entry is an object with kind, group, label and parameters. With an empty text it lists the step kinds of the engine. With text it lists the kinds that match, then the methods, properties, signals and classes of the engine that match.

dragged_side narrows the list to steps that a dragged wire can reach. It is "" for no filter, "input" when the wire comes from an input, or "output" when it comes from an output. type is the Variant type that a dragged data wire carries, and TYPE_NIL fits any. sort is "flow" or "data" and says which sort of port the wire comes from. A value that is not one of these prints an error and returns an empty string.

get_document

String get_document() const

Returns the whole script as text, in the format of an .ograph file. The same text is what saving the script writes.

get_error

String get_error() const

Returns the first error found when the script was last compiled, or an empty string when it compiled with no error. The error names the function and the step. A function with an error does not run.

get_payload

String get_payload(function: String, ids_text: String) const

Returns a copy of some steps of function as JSON text, ready to hand to paste_payload() on this script or another one. ids_text is a text array of step numbers, such as "[2, 3]". The copy holds those steps, the wires between them, and the variables and signals they use. It names the functions they call.

Returns an empty string when the function is missing, ids_text is not an array, or none of the numbers is a step. The entry step cannot be copied.

get_snapshot

String get_snapshot() const

Returns the functions, variables, signals and base type as one text. Pass the text to restore_snapshot() to return the script to this state. An undo history is made from these texts.

get_valid_step_id

int get_valid_step_id(function: String) const

Returns a step number that no step of function uses, or -1 when the function is missing.

get_warnings

Array get_warnings() const

Returns an array of dictionaries, one per warning. A warning does not stop a function from running. Each dictionary has these keys: kind, function, step, name and text.

kind is "unused_variable", "unused_local", "unreachable_step" or "shadowed_name". function is empty and step is -1 when the warning does not belong to one function or step. A name that starts with an underscore is never reported as unused.

move_step

Error move_step(function: String, id: int, x: float, y: float)

Puts step id of function at the position (x, y) on the canvas. The position is only for the editor. It does not change how the function runs. Returns OK or ERR_DOES_NOT_EXIST.

paste_payload

String paste_payload(function: String, payload_text: String, x: float, y: float, conflict: String)

Pastes steps from get_payload() into function, with the top left of the steps at (x, y). The pasted steps get new numbers. Returns JSON text of an object with three keys. error is an Error value, conflicts is an array and steps is the array of new step numbers.

The payload brings the variables and signals its steps use. When this script already has one with the same name and another definition, nothing is pasted and error is ERR_ALREADY_EXISTS. conflicts then lists each clash. Call again with conflict set to "reuse" to use this script’s own, or "keep_both" to add the pasted one under a new name. conflict is empty for the first try. A function that the payload calls and this script lacks is added empty.

Other errors are ERR_INVALID_DATA when the text is not a payload, ERR_DOES_NOT_EXIST when the function is missing, and ERR_INVALID_PARAMETER when conflict is not one of the three values or the payload has a return step and the function has no entry step. The whole paste is one change. It either all happens or none of it does.

remove_frame

Error remove_frame(function: String, id: int)

Removes comment frame id from function. The steps inside it stay. Returns OK or ERR_DOES_NOT_EXIST.

remove_function

Error remove_function(name: String)

Removes the function called name, with its steps. Steps that call it stay, and the call fails when it runs. Returns OK or ERR_DOES_NOT_EXIST.

remove_signal

Error remove_signal(name: String)

Removes the signal called name. Steps that use it stay, and they fail when they run. Returns OK or ERR_DOES_NOT_EXIST.

remove_step

Error remove_step(function: String, id: int)

Removes step id and every wire that touches it from function. Returns OK or ERR_DOES_NOT_EXIST.

remove_variable

Error remove_variable(name: String)

Removes the variable called name. Steps that read or write it stay, and the script does not compile until they change. Returns OK or ERR_DOES_NOT_EXIST.

rename_function

Error rename_function(name: String, new_name: String)

Renames a function and every step that calls it. Returns OK, ERR_DOES_NOT_EXIST when no function has the name name, ERR_INVALID_PARAMETER when new_name is not a valid identifier, or ERR_ALREADY_EXISTS when a function already has new_name.

rename_signal

Error rename_signal(name: String, new_name: String)

Renames a signal and every step that uses it. Returns OK, ERR_DOES_NOT_EXIST when no signal has the name name, ERR_INVALID_PARAMETER when new_name is not a valid identifier, or ERR_ALREADY_EXISTS when a signal already has new_name.

rename_variable

Error rename_variable(name: String, new_name: String)

Renames a variable and every step that reads or writes it. A running instance loses the value it held under the old name. Returns OK, ERR_DOES_NOT_EXIST when no variable has the name name, ERR_INVALID_PARAMETER when new_name is not a valid identifier, or ERR_ALREADY_EXISTS when a variable already has new_name.

restore_snapshot

Error restore_snapshot(snapshot_text: String)

Replaces the functions, variables, signals and base type with those of a text from get_snapshot(). Returns OK or ERR_INVALID_DATA when the text is not a snapshot. Nothing changes after an error.

set_frame

Error set_frame(function: String, frame_text: String)

Adds or replaces a comment frame in function. frame_text is the text of a dictionary with the keys id, title, x, y, width, height and collapsed. A frame is a box drawn behind steps. It has no effect on how the function runs. Returns OK, ERR_PARSE_ERROR when the text cannot be read, ERR_INVALID_DATA when it is not a frame or has no area, or ERR_DOES_NOT_EXIST when the function is missing.

set_signal_arguments

Error set_signal_arguments(name: String, arguments_text: String)

Sets the arguments of signal name. arguments_text is a text array with one entry per argument, each a dictionary with name and type. Returns OK, ERR_DOES_NOT_EXIST when the signal is missing, ERR_PARSE_ERROR when the text cannot be read, or ERR_INVALID_DATA when an entry is not a name and a type.

set_step_parameter

Error set_step_parameter(function: String, id: int, parameter: String, value_text: String)

Sets the parameter parameter of step id in function to the value written in value_text. A parameter is a value typed on the step, such as the name of a variable or the text to print. Wires that no longer fit the changed step are removed.

Returns OK, ERR_PARSE_ERROR when value_text cannot be read, ERR_DOES_NOT_EXIST when the step or function is missing, or ERR_INVALID_PARAMETER when parameter is empty or is "pins". Use set_step_pins() to change the number of pins.

set_step_pins

Error set_step_pins(function: String, id: int, count: int)

Sets how many pins a step has, for a kind that can grow, such as a sequence with more than two outputs. Wires on pins past the new count are removed. Returns OK, ERR_DOES_NOT_EXIST when the step or function is missing, ERR_UNAVAILABLE when the kind is unknown, or ERR_INVALID_PARAMETER when the kind cannot grow or count is below the smallest count or above the largest.

set_variable_default

Error set_variable_default(name: String, value_text: String)

Sets the value a new instance starts with for variable name. value_text is written the way @GlobalScope.var_to_str() writes a value. Its type must match the type of the variable, unless the variable has no type and holds any value. Returns OK, ERR_DOES_NOT_EXIST, ERR_PARSE_ERROR when the text cannot be read, or ERR_INVALID_PARAMETER when the type differs.

set_variable_exported

Error set_variable_exported(name: String, exported: bool)

Sets whether variable name shows in the inspector of an entity that has this script. Returns OK or ERR_DOES_NOT_EXIST.

set_variable_hint

Error set_variable_hint(name: String, hint: int, hint_string: String)

Sets the property hint of variable name to hint, a PropertyHint value, with hint_string as its text. The inspector uses it to pick an editor, such as a range slider. Returns OK, ERR_DOES_NOT_EXIST, or ERR_INVALID_PARAMETER when hint is not a hint.

set_variable_info

Error set_variable_info(name: String, category: String, description: String)

Sets the category and the description of variable name. The inspector groups exported variables by category and shows the description as a tooltip. Returns OK or ERR_DOES_NOT_EXIST.

set_variable_type

Error set_variable_type(name: String, type: int)

Sets the Variant type of variable name. The default value resets to the default of the new type when the type changes. Returns OK, ERR_DOES_NOT_EXIST, or ERR_INVALID_PARAMETER when type is not a Variant type.