Files
godot-docs-l10n/classes/es/class_expression.rst

159 lines
7.8 KiB
ReStructuredText

:github_url: hide
.. _class_Expression:
Expression
==========
**Hereda:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_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<class_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>("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>("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<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\ ) |
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`String<class_String>` | :ref:`get_error_text<class_Expression_method_get_error_text>`\ (\ ) |const| |
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`has_execute_failed<class_Expression_method_has_execute_failed>`\ (\ ) |const| |
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`parse<class_Expression_method_parse>`\ (\ expression\: :ref:`String<class_String>`, input_names\: :ref:`PackedStringArray<class_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<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>`
Ejecuta la expresión que fue previamente analizada por :ref:`parse()<class_Expression_method_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()<class_Expression_method_has_execute_failed>`.
Si has definido las variables de entrada en :ref:`parse()<class_Expression_method_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<class_String>` **get_error_text**\ (\ ) |const| :ref:`🔗<class_Expression_method_get_error_text>`
Returns the error text if :ref:`parse()<class_Expression_method_parse>` or :ref:`execute()<class_Expression_method_execute>` has failed.
.. rst-class:: classref-item-separator
----
.. _class_Expression_method_has_execute_failed:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **has_execute_failed**\ (\ ) |const| :ref:`🔗<class_Expression_method_has_execute_failed>`
Devuelve ``true`` si :ref:`execute()<class_Expression_method_execute>` ha fallado.
.. rst-class:: classref-item-separator
----
.. _class_Expression_method_parse:
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` **parse**\ (\ expression\: :ref:`String<class_String>`, input_names\: :ref:`PackedStringArray<class_PackedStringArray>` = PackedStringArray()\ ) :ref:`🔗<class_Expression_method_parse>`
Parses the expression and returns an :ref:`Error<enum_@GlobalScope_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.)`