class_expression.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the Expression.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_Expression:
  6. Expression
  7. ==========
  8. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  9. A class that stores an expression you can execute.
  10. Description
  11. -----------
  12. 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.
  13. An example expression text using the built-in math functions could be ``sqrt(pow(3, 2) + pow(4, 2))``.
  14. In the following example we use a :ref:`LineEdit<class_LineEdit>` node to write our expression and show the result.
  15. .. tabs::
  16. .. code-tab:: gdscript
  17. var expression = Expression.new()
  18. func _ready():
  19. $LineEdit.connect("text_submitted", self, "_on_text_submitted")
  20. func _on_text_submitted(command):
  21. var error = expression.parse(command)
  22. if error != OK:
  23. print(expression.get_error_text())
  24. return
  25. var result = expression.execute()
  26. if not expression.has_execute_failed():
  27. $LineEdit.text = str(result)
  28. .. code-tab:: csharp
  29. public Expression expression = new Expression();
  30. public override void _Ready()
  31. {
  32. GetNode("LineEdit").Connect("text_submitted", this, nameof(OnTextEntered));
  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. object result = expression.Execute();
  43. if (!expression.HasExecuteFailed())
  44. {
  45. GetNode<LineEdit>("LineEdit").Text = result.ToString();
  46. }
  47. }
  48. Methods
  49. -------
  50. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`Variant<class_Variant>` | :ref:`execute<class_Expression_method_execute>` **(** :ref:`Array<class_Array>` inputs=[], :ref:`Object<class_Object>` base_instance=null, :ref:`bool<class_bool>` show_error=true **)** |
  52. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`String<class_String>` | :ref:`get_error_text<class_Expression_method_get_error_text>` **(** **)** |const| |
  54. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`bool<class_bool>` | :ref:`has_execute_failed<class_Expression_method_has_execute_failed>` **(** **)** |const| |
  56. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`parse<class_Expression_method_parse>` **(** :ref:`String<class_String>` expression, :ref:`PackedStringArray<class_PackedStringArray>` input_names=PackedStringArray() **)** |
  58. +---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. Method Descriptions
  60. -------------------
  61. .. _class_Expression_method_execute:
  62. - :ref:`Variant<class_Variant>` **execute** **(** :ref:`Array<class_Array>` inputs=[], :ref:`Object<class_Object>` base_instance=null, :ref:`bool<class_bool>` show_error=true **)**
  63. Executes the expression that was previously parsed by :ref:`parse<class_Expression_method_parse>` and returns the result. Before you use the returned object, you should check if the method failed by calling :ref:`has_execute_failed<class_Expression_method_has_execute_failed>`.
  64. If you defined input variables in :ref:`parse<class_Expression_method_parse>`, you can specify their values in the inputs array, in the same order.
  65. ----
  66. .. _class_Expression_method_get_error_text:
  67. - :ref:`String<class_String>` **get_error_text** **(** **)** |const|
  68. Returns the error text if :ref:`parse<class_Expression_method_parse>` has failed.
  69. ----
  70. .. _class_Expression_method_has_execute_failed:
  71. - :ref:`bool<class_bool>` **has_execute_failed** **(** **)** |const|
  72. Returns ``true`` if :ref:`execute<class_Expression_method_execute>` has failed.
  73. ----
  74. .. _class_Expression_method_parse:
  75. - :ref:`Error<enum_@GlobalScope_Error>` **parse** **(** :ref:`String<class_String>` expression, :ref:`PackedStringArray<class_PackedStringArray>` input_names=PackedStringArray() **)**
  76. Parses the expression and returns an :ref:`Error<enum_@GlobalScope_Error>` code.
  77. You can optionally specify names of variables that may appear in the expression with ``input_names``, so that you can bind them when it gets executed.
  78. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  79. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  80. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  81. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  82. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  83. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`