0.2.0-dev Download ZIP
88 lines ·3.3 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="Expression" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | A class that stores an expression you can execute. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | 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. |
| 8 | An example expression text using the built-in math functions could be [code]sqrt(pow(3, 2) + pow(4, 2))[/code]. |
| 9 | In the following example we use a [LineEdit] node to write our expression and show the result. |
| 10 | [codeblocks] |
| 11 | [gdscript] |
| 12 | var expression = Expression.new() |
| 13 | |
| 14 | func _ready(): |
| 15 | $LineEdit.text_submitted.connect(self._on_text_submitted) |
| 16 | |
| 17 | func _on_text_submitted(command): |
| 18 | var error = expression.parse(command) |
| 19 | if error != OK: |
| 20 | print(expression.get_error_text()) |
| 21 | return |
| 22 | var result = expression.execute() |
| 23 | if not expression.has_execute_failed(): |
| 24 | $LineEdit.text = str(result) |
| 25 | [/gdscript] |
| 26 | [csharp] |
| 27 | private Expression _expression = new Expression(); |
| 28 | |
| 29 | public override void _Ready() |
| 30 | { |
| 31 | GetNode<LineEdit>("LineEdit").TextSubmitted += OnTextEntered; |
| 32 | } |
| 33 | |
| 34 | private void OnTextEntered(string command) |
| 35 | { |
| 36 | Error error = _expression.Parse(command); |
| 37 | if (error != Error.Ok) |
| 38 | { |
| 39 | GD.Print(_expression.GetErrorText()); |
| 40 | return; |
| 41 | } |
| 42 | Variant result = _expression.Execute(); |
| 43 | if (!_expression.HasExecuteFailed()) |
| 44 | { |
| 45 | GetNode<LineEdit>("LineEdit").Text = result.ToString(); |
| 46 | } |
| 47 | } |
| 48 | [/csharp] |
| 49 | [/codeblocks] |
| 50 | </description> |
| 51 | <tutorials> |
| 52 | <link title="Evaluating Expressions">$DOCS_URL/tutorials/scripting/evaluating_expressions.html</link> |
| 53 | </tutorials> |
| 54 | <methods> |
| 55 | <method name="execute"> |
| 56 | <return type="Variant" /> |
| 57 | <param index="0" name="inputs" type="Array" default="[]" /> |
| 58 | <param index="1" name="base_instance" type="Object" default="null" /> |
| 59 | <param index="2" name="show_error" type="bool" default="true" /> |
| 60 | <param index="3" name="const_calls_only" type="bool" default="false" /> |
| 61 | <description> |
| 62 | Executes the expression that was previously parsed by [method parse] and returns the result. Before you use the returned object, you should check if the method failed by calling [method has_execute_failed]. |
| 63 | If you defined input variables in [method parse], you can specify their values in the inputs array, in the same order. |
| 64 | </description> |
| 65 | </method> |
| 66 | <method name="get_error_text" qualifiers="const"> |
| 67 | <return type="String" /> |
| 68 | <description> |
| 69 | Returns the error text if [method parse] or [method execute] has failed. |
| 70 | </description> |
| 71 | </method> |
| 72 | <method name="has_execute_failed" qualifiers="const"> |
| 73 | <return type="bool" /> |
| 74 | <description> |
| 75 | Returns [code]true[/code] if [method execute] has failed. |
| 76 | </description> |
| 77 | </method> |
| 78 | <method name="parse"> |
| 79 | <return type="int" enum="Error" /> |
| 80 | <param index="0" name="expression" type="String" /> |
| 81 | <param index="1" name="input_names" type="PackedStringArray" default="PackedStringArray()" /> |
| 82 | <description> |
| 83 | Parses the expression and returns an [enum Error] code. |
| 84 | You can optionally specify names of variables that may appear in the expression with [param input_names], so that you can bind them when it gets executed. |
| 85 | </description> |
| 86 | </method> |
| 87 | </methods> |
| 88 | </class> |