Merge pull request #34280 from zaksnet/fix-yield-documentation

Fix documentation for yield
This commit is contained in:
Rémi Verschelde 2019-12-16 08:49:32 +01:00 committed by GitHub
commit 74977277fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1377,23 +1377,26 @@
You can also use [code]yield[/code] to wait for a function to finish: You can also use [code]yield[/code] to wait for a function to finish:
[codeblock] [codeblock]
func _ready(): func _ready():
yield(do_something(), "completed") yield(countdown(), "completed") # waiting for the countdown() function to complete
yield(do_something_else(), "completed") print('Ready')
print("All functions are done!")
func do_something(): func countdown():
print("Something is done!") yield(get_tree(), "idle_frame") # returns a GDScriptFunctionState object to _ready()
print(3)
func do_something_else(): yield(get_tree().create_timer(1.0), "timeout")
print("Something else is done!") print(2)
yield(get_tree().create_timer(1.0), "timeout")
print(1)
yield(get_tree().create_timer(1.0), "timeout")
# prints: # prints:
# Something is done! # 3
# Something else is done! # 2
# All functions are done! # 1
# Ready
[/codeblock] [/codeblock]
When yielding on a function, the [code]completed[/code] signal will be emitted automatically when the function returns. It can, therefore, be used as the [code]signal[/code] parameter of the [code]yield[/code] method to resume. When yielding on a function, the [code]completed[/code] signal will be emitted automatically when the function returns. It can, therefore, be used as the [code]signal[/code] parameter of the [code]yield[/code] method to resume.
If you are planning on calling the same function within a loop, you should consider using [code]yield(get_tree(), "idle_frame")[/code] also. In order to yield on a function, the resulting function should also return a [code]GDScriptFunctionState[/code]. Notice [code]yield(get_tree(), "idle_frame")[/code] from the above example.
</description> </description>
</method> </method>
</methods> </methods>