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)
privateExpression_expression=newExpression();publicoverridevoid_Ready(){GetNode<LineEdit>("LineEdit").TextSubmitted+=OnTextEntered;}privatevoidOnTextEntered(stringcommand){Errorerror=_expression.Parse(command);if(error!=Error.Ok){GD.Print(_expression.GetErrorText());return;}Variantresult=_expression.Execute();if(!_expression.HasExecuteFailed()){GetNode<LineEdit>("LineEdit").Text=result.ToString();}}
Tutorials
Methods
execute(inputs:Array = [], base_instance:Object = null, show_error:bool = true, const_calls_only:bool = false) | |
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.
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.