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 besqrt(pow(3,2)+pow(4,2)).

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

varexpression=Expression.new()func_ready():$LineEdit.text_submitted.connect(self._on_text_submitted)func_on_text_submitted(command):varerror=expression.parse(command)iferror!=OK:print(expression.get_error_text())returnvarresult=expression.execute()ifnotexpression.has_execute_failed():$LineEdit.text=str(result)

Tutorials

Methods

Variant

execute(inputs:Array = [], base_instance:Object = null, show_error:bool = true, const_calls_only:bool = false)

String

get_error_text()const

bool

has_execute_failed()const

Error

parse(expression:String, input_names:PackedStringArray = PackedStringArray())


Method Descriptions

Variantexecute(inputs:Array = [], base_instance:Object = null, show_error:bool = true, const_calls_only:bool = false)🔗

Executes the expression that was previously parsed byparse() and returns the result. Before you use the returned object, you should check if the method failed by callinghas_execute_failed().

If you defined input variables inparse(), you can specify their values in the inputs array, in the same order.


Stringget_error_text()const🔗

Returns the error text ifparse() orexecute() has failed.


boolhas_execute_failed()const🔗

Returnstrue ifexecute() has failed.


Errorparse(expression:String, input_names:PackedStringArray = PackedStringArray())🔗

Parses the expression and returns anError code.

You can optionally specify names of variables that may appear in the expression withinput_names, so that you can bind them when it gets executed.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.