Evaluating expressions

    An expression can be:

    • A mathematical expression such as .

    • A built-in method call like deg2rad(90).

    • A method call on an user-provided script like update_health(), if base_instance is set to a value other than null when calling Expression.execute().

    Note

    The Expression class is independent from GDScript. It’s available even if you compile Godot with the GDScript module disabled.

    To evaluate a mathematical expression, use:

    The following operators are available:

    Spaces around operators are optional. Also, keep in mind the usual order of operations applies. Use parentheses to override the order of operations if needed.

    All the Variant types supported in Godot can be used: integers, floating-point numbers, strings, arrays, dictionaries, colors, vectors, …

    Arrays and dictionaries can be indexed like in GDScript:

    1. # Returns 1.
    2. [1, 2][0]
    3. # Returns 3. Negative indices can be used to count from the end of the array.
    4. [1, 3][-1]
    5. # Returns "green".
    6. Vector3(5, 6, 7)[2]
    7. Vector3(5, 6, 7)["z"]
    8. Vector3(5, 6, 7).z

    You can pass variables to an expression. These variables will then become available in the expression’s “context” and will be substituted when used in the expression:

    Both the variable names and variable values must be specified as an array, even if you only define one variable. Also, variable names are case-sensitive.

    When calling , you can set the value of the base_instance parameter to a specific object instance such as self, another script instance or even a singleton:

    1. func double(number):
    2. return number * 2
    3. func _ready():
    4. var expression = Expression.new()
    5. expression.parse("double(10)")
    6. # This won't work since we're not passing the current script as the base instance.
    7. var result = expression.execute([], null)
    8. print(result) # null
    9. # as the base instance.
    10. print(result) # 20

    Associating a base instance allows doing the following:

    • Reference the instance’s constants (const) in the expression.

    • Reference the instance’s member variables (var) in the expression.

    • Call methods defined in the instance and use their return values in the expression.

    Warning

    Setting a base instance to a value other than null allows referencing constants, member variables, and calling all methods defined in the script attached to the instance. Allowing users to enter expressions may allow cheating in your game, or may even introduce security vulnerabilities if you allow arbitrary clients to run expressions on other players’ devices.

    The script below demonstrates what the Expression class is capable of:

    The output from the script will be:

    1. 4
    2. 160
    3. 1.570796
    4. You called 'call_me()' in the expression text.
    5. 1365
    6. You called 'call_me()' in the expression text.
    7. Argument passed: 42
    8. 0
    9. You called 'call_me()' in the expression text.
    10. Argument passed: some string

    Most methods available in the @GDScript scope are available in the Expression class, even if no base instance is bound to the expression. The same parameters and return types are available.

    However, unlike GDScript, parameters are always required even if they’re specified as being optional in the class reference. In contrast, this restriction on arguments doesn’t apply to user-made functions when you bind a base instance to the expression.