Para qué se usa la función yield()--------- function explanation yield ()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ariel

he observado que se usa mucho la funcion yield,pero no comprendo bien como funcióna.
Alguien podria explicarme en español para que se usa y que significan sus parametros.
Gracias a todos.Saludos.

google translate:
I have observed that the yield function is used a lot, but I do not understand well how it works.
Someone could explain me in Spanish so that it is used and what its parameters mean.
Thanks to all. Greetings.

Sorry, I may do it in English, but I’m not sure, that Google translator will make it in correct way in Spanish…

rojekabc | 2018-12-24 08:56

:bust_in_silhouette: Reply From: SIsilicon

La función de rendimiento se utiliza para hacer estas cosas llamadas coroutines. Una guía en gdscript permite esencialmente “pausar” la ejecución de una función, y luego terminar de ejecutarla más tarde. Por ejemplo, esto

func mi_func():
    print("Hola")
    yield()
    print("mundo!")

func _ready():
    y = mi_func() # function gets paused and saved here.
    print("mi bueno")
    y.resume() # continue the function execution.

imprime esto.

Hola
mi bueno
mundo!

La función de rendimiento también se puede utilizar con señales. Cuando se emite la señal, la función se reanuda a dicho rendimiento.

yield(objeto_con_señal, "la señal")

# Reanudar la ejecución del siguiente cuadro.
yield(get_tree(), "idle_frame")

# Reanudar la ejecución cuando la animación haya terminado de reproducirse.
yield(get_node("AnimationPlayer"), "finished")

# Espere 5 segundos, luego reanude la ejecución.
yield(get_tree().create_timer(5.0), "timeout")