class_expression.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Expression.xml.
  6. .. _class_Expression:
  7. Expression
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A class that stores an expression you can execute.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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.
  15. An example expression text using the built-in math functions could be ``sqrt(pow(3, 2) + pow(4, 2))``.
  16. In the following example we use a :ref:`LineEdit<class_LineEdit>` node to write our expression and show the result.
  17. .. tabs::
  18. .. code-tab:: gdscript
  19. var expression = Expression.new()
  20. func _ready():
  21. $LineEdit.text_submitted.connect(self._on_text_submitted)
  22. func _on_text_submitted(command):
  23. var error = expression.parse(command)
  24. if error != OK:
  25. print(expression.get_error_text())
  26. return
  27. var result = expression.execute()
  28. if not expression.has_execute_failed():
  29. $LineEdit.text = str(result)
  30. .. code-tab:: csharp
  31. private Expression _expression = new Expression();
  32. public override void _Ready()
  33. {
  34. GetNode<LineEdit>("LineEdit").TextSubmitted += OnTextEntered;
  35. }
  36. private void OnTextEntered(string command)
  37. {
  38. Error error = _expression.Parse(command);
  39. if (error != Error.Ok)
  40. {
  41. GD.Print(_expression.GetErrorText());
  42. return;
  43. }
  44. Variant result = _expression.Execute();
  45. if (!_expression.HasExecuteFailed())
  46. {
  47. GetNode<LineEdit>("LineEdit").Text = result.ToString();
  48. }
  49. }
  50. .. rst-class:: classref-introduction-group
  51. Tutorials
  52. ---------
  53. - :doc:`Evaluating Expressions <../tutorials/scripting/evaluating_expressions>`
  54. .. rst-class:: classref-reftable-group
  55. Methods
  56. -------
  57. .. table::
  58. :widths: auto
  59. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`Variant<class_Variant>` | :ref:`execute<class_Expression_method_execute>`\ (\ inputs\: :ref:`Array<class_Array>` = [], base_instance\: :ref:`Object<class_Object>` = null, show_error\: :ref:`bool<class_bool>` = true, const_calls_only\: :ref:`bool<class_bool>` = false\ ) |
  61. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`String<class_String>` | :ref:`get_error_text<class_Expression_method_get_error_text>`\ (\ ) |const| |
  63. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`bool<class_bool>` | :ref:`has_execute_failed<class_Expression_method_has_execute_failed>`\ (\ ) |const| |
  65. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`parse<class_Expression_method_parse>`\ (\ expression\: :ref:`String<class_String>`, input_names\: :ref:`PackedStringArray<class_PackedStringArray>` = PackedStringArray()\ ) |
  67. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. .. rst-class:: classref-section-separator
  69. ----
  70. .. rst-class:: classref-descriptions-group
  71. Method Descriptions
  72. -------------------
  73. .. _class_Expression_method_execute:
  74. .. rst-class:: classref-method
  75. :ref:`Variant<class_Variant>` **execute**\ (\ inputs\: :ref:`Array<class_Array>` = [], base_instance\: :ref:`Object<class_Object>` = null, show_error\: :ref:`bool<class_bool>` = true, const_calls_only\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_Expression_method_execute>`
  76. 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>`.
  77. 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.
  78. .. rst-class:: classref-item-separator
  79. ----
  80. .. _class_Expression_method_get_error_text:
  81. .. rst-class:: classref-method
  82. :ref:`String<class_String>` **get_error_text**\ (\ ) |const| :ref:`🔗<class_Expression_method_get_error_text>`
  83. Returns the error text if :ref:`parse<class_Expression_method_parse>` or :ref:`execute<class_Expression_method_execute>` has failed.
  84. .. rst-class:: classref-item-separator
  85. ----
  86. .. _class_Expression_method_has_execute_failed:
  87. .. rst-class:: classref-method
  88. :ref:`bool<class_bool>` **has_execute_failed**\ (\ ) |const| :ref:`🔗<class_Expression_method_has_execute_failed>`
  89. Returns ``true`` if :ref:`execute<class_Expression_method_execute>` has failed.
  90. .. rst-class:: classref-item-separator
  91. ----
  92. .. _class_Expression_method_parse:
  93. .. rst-class:: classref-method
  94. :ref:`Error<enum_@GlobalScope_Error>` **parse**\ (\ expression\: :ref:`String<class_String>`, input_names\: :ref:`PackedStringArray<class_PackedStringArray>` = PackedStringArray()\ ) :ref:`🔗<class_Expression_method_parse>`
  95. Parses the expression and returns an :ref:`Error<enum_@GlobalScope_Error>` code.
  96. 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.
  97. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  98. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  99. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  100. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  101. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  102. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  103. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  104. .. |void| replace:: :abbr:`void (No return value.)`