:github_url: hide .. _class_Expression: Expression ========== **Hereda:** :ref:`RefCounted` **<** :ref:`Object` Una clase que almacena una expresión que puedes ejecutar. .. rst-class:: classref-introduction-group Descripción ---------------------- 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 be ``sqrt(pow(3, 2) + pow(4, 2))``. In the following example we use a :ref:`LineEdit` node to write our expression and show the result. .. tabs:: .. code-tab:: gdscript var expression = Expression.new() func _ready(): $LineEdit.text_submitted.connect(self._on_text_submitted) func _on_text_submitted(command): var error = expression.parse(command) if error != OK: print(expression.get_error_text()) return var result = expression.execute() if not expression.has_execute_failed(): $LineEdit.text = str(result) .. code-tab:: csharp private Expression _expression = new Expression(); public override void _Ready() { GetNode("LineEdit").TextSubmitted += OnTextEntered; } private void OnTextEntered(string command) { Error error = _expression.Parse(command); if (error != Error.Ok) { GD.Print(_expression.GetErrorText()); return; } Variant result = _expression.Execute(); if (!_expression.HasExecuteFailed()) { GetNode("LineEdit").Text = result.ToString(); } } .. rst-class:: classref-introduction-group Tutoriales -------------------- - :doc:`Evaluando expresiones <../tutorials/scripting/evaluating_expressions>` .. rst-class:: classref-reftable-group Métodos -------------- .. table:: :widths: auto +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`execute`\ (\ inputs\: :ref:`Array` = [], base_instance\: :ref:`Object` = null, show_error\: :ref:`bool` = true, const_calls_only\: :ref:`bool` = false\ ) | +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_error_text`\ (\ ) |const| | +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_execute_failed`\ (\ ) |const| | +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Error` | :ref:`parse`\ (\ expression\: :ref:`String`, input_names\: :ref:`PackedStringArray` = PackedStringArray()\ ) | +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Descripciones de Métodos ------------------------------------------------ .. _class_Expression_method_execute: .. rst-class:: classref-method :ref:`Variant` **execute**\ (\ inputs\: :ref:`Array` = [], base_instance\: :ref:`Object` = null, show_error\: :ref:`bool` = true, const_calls_only\: :ref:`bool` = false\ ) :ref:`🔗` Ejecuta la expresión que fue previamente analizada por :ref:`parse()` y devuelve el resultado. Antes de usar el objeto devuelto, debería comprobar si el método falló llamando a :ref:`has_execute_failed()`. Si has definido las variables de entrada en :ref:`parse()`, puedes especificar sus valores en el array de entradas, en el mismo orden. .. rst-class:: classref-item-separator ---- .. _class_Expression_method_get_error_text: .. rst-class:: classref-method :ref:`String` **get_error_text**\ (\ ) |const| :ref:`🔗` Returns the error text if :ref:`parse()` or :ref:`execute()` has failed. .. rst-class:: classref-item-separator ---- .. _class_Expression_method_has_execute_failed: .. rst-class:: classref-method :ref:`bool` **has_execute_failed**\ (\ ) |const| :ref:`🔗` Devuelve ``true`` si :ref:`execute()` ha fallado. .. rst-class:: classref-item-separator ---- .. _class_Expression_method_parse: .. rst-class:: classref-method :ref:`Error` **parse**\ (\ expression\: :ref:`String`, input_names\: :ref:`PackedStringArray` = PackedStringArray()\ ) :ref:`🔗` Parses the expression and returns an :ref:`Error` code. 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. .. |virtual| replace:: :abbr:`virtual (Normalmente, este método debería ser sobreescrito por el usuario para que tenga algún efecto.)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (Este método no tiene efectos secundarios. No modifica ninguna de las variables miembro de la instancia.)` .. |vararg| replace:: :abbr:`vararg (Este método permite agregar cualquier número de argumentos después de los descritos aquí.)` .. |constructor| replace:: :abbr:`constructor (Este método se utiliza para construir un tipo.)` .. |static| replace:: :abbr:`static (Este método no necesita una instancia para ser llamado, por lo que puede llamarse directamente utilizando el nombre de la clase.)` .. |operator| replace:: :abbr:`operator (Este método describe un operador válido para usar con este tipo como operando izquierdo.)` .. |bitfield| replace:: :abbr:`BitField (Este valor es un entero compuesto como una máscara de bits de las siguientes banderas.)` .. |void| replace:: :abbr:`void (Sin valor de retorno.)`