Expression.xml 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="Expression" inherits="RefCounted" version="4.0">
  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. func _ready():
  14. $LineEdit.connect("text_submitted", self, "_on_text_submitted")
  15. func _on_text_submitted(command):
  16. var error = expression.parse(command)
  17. if error != OK:
  18. print(expression.get_error_text())
  19. return
  20. var result = expression.execute()
  21. if not expression.has_execute_failed():
  22. $LineEdit.text = str(result)
  23. [/gdscript]
  24. [csharp]
  25. public Expression expression = new Expression();
  26. public override void _Ready()
  27. {
  28. GetNode("LineEdit").Connect("text_submitted", this, nameof(OnTextEntered));
  29. }
  30. private void OnTextEntered(string command)
  31. {
  32. Error error = expression.Parse(command);
  33. if (error != Error.Ok)
  34. {
  35. GD.Print(expression.GetErrorText());
  36. return;
  37. }
  38. object result = expression.Execute();
  39. if (!expression.HasExecuteFailed())
  40. {
  41. GetNode&lt;LineEdit&gt;("LineEdit").Text = result.ToString();
  42. }
  43. }
  44. [/csharp]
  45. [/codeblocks]
  46. </description>
  47. <tutorials>
  48. </tutorials>
  49. <methods>
  50. <method name="execute">
  51. <return type="Variant" />
  52. <argument index="0" name="inputs" type="Array" default="[]" />
  53. <argument index="1" name="base_instance" type="Object" default="null" />
  54. <argument index="2" name="show_error" type="bool" default="true" />
  55. <description>
  56. 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].
  57. If you defined input variables in [method parse], you can specify their values in the inputs array, in the same order.
  58. </description>
  59. </method>
  60. <method name="get_error_text" qualifiers="const">
  61. <return type="String" />
  62. <description>
  63. Returns the error text if [method parse] has failed.
  64. </description>
  65. </method>
  66. <method name="has_execute_failed" qualifiers="const">
  67. <return type="bool" />
  68. <description>
  69. Returns [code]true[/code] if [method execute] has failed.
  70. </description>
  71. </method>
  72. <method name="parse">
  73. <return type="int" enum="Error" />
  74. <argument index="0" name="expression" type="String" />
  75. <argument index="1" name="input_names" type="PackedStringArray" default="PackedStringArray()" />
  76. <description>
  77. Parses the expression and returns an [enum Error] code.
  78. You can optionally specify names of variables that may appear in the expression with [code]input_names[/code], so that you can bind them when it gets executed.
  79. </description>
  80. </method>
  81. </methods>
  82. </class>