Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Expression

Inherits: RefCounted < Object

A class that stores an expression you can execute.

Description

An expression can be made of any arithmetic operation, built-in math function call, method call of a passed instance, or built-in type construction call.

An example expression text using the built-in math functions could be sqrt(pow(3, 2) + pow(4, 2)).

In the following example we use a LineEdit node to write our expression and show the result.

var expression = Expression.new()

func _ready():
    $LineEdit.text_submitted.connect(self._on_text_submitted)

func _on_text_submitted(command):
    var error = expression.parse(command)
    if error != OK:
        print(expression.get_error_text())
        return
    var result = expression.execute()
    if not expression.has_execute_failed():
        $LineEdit.text = str(result)