Godot: yield with function doesn't work

Created on 11 Dec 2019  路  11Comments  路  Source: godotengine/godot

Godot version:
3.2 beta 3
OS/device including version:
Linux Manjaro
Issue description:
In the documentation for the yield you can see that:

You can also use yield to wait for a function to finish:

func _ready():
    yield(do_something(), "completed")
    yield(do_something_else(), "completed")
    print("All functions are done!")

func do_something():
    print("Something is done!")

func do_something_else():
    print("Something else is done!")

but if you try to yield a function you will get that error:

First argument of yield() not of type object.

Steps to reproduce:
yield(print("ok"), "completed")

discussion documentation

Most helpful comment

Hello guys,
It seems that in order to yield, the function you are yielding also needs to inherit from GDScriptFunctionState. So the documentation should be changed to :

func _ready():
    yield(do_something(), "completed")
    yield(do_something_else(), "completed")
    print("All functions are done!")

func do_something():
    yield(get_tree(), "idle_frame")
    print("Something is done!")

func do_something_else():
    yield(get_tree(), "idle_frame")
    print("Something else is done!")

Notice the yield(get_tree(), "idle_frame") in do_something functions.
Of course yield(get_tree(), "idle_frame") can be anything but this seems reasonable because it will only yield for an idle frame. Sorry i though this was optional and as a result documented it as optional.

If you are planning on calling the same function within a loop, you should consider using [code]yield(get_tree(), "idle_frame")[/code] also. <--- From the docs

All 11 comments

@grevius5 See the documentation.

GDScriptFunctionState yield( Object object=null, String signal="" )

print("ok") returns null. This is not an object.
To wait for a function to complete, you should use something like the following:

extends Node

func f():
    print('f 1')
    yield(get_tree().create_timer(3), 'timeout')
    print('f 2')

func _ready():
    print('_ready 1')
    yield(f(), 'completed')
    print('_ready 2')

See also #31494.

@dalexeev so is wrong the example in the documentation because it calls simple functions with only a print

@grevius5 I tried to find the phrase on the Internet, but did not find an exact match. Could you provide a link?
191211-1

Official documentation:
191211-2

I have this in my 3.2 beta 3 godot internal documentation
godot 3-2 beta 3 doc

Indeed, this example (apparently incorrect) was added recently. #33872
@zaksnet @akien-mga

Hello guys,
It seems that in order to yield, the function you are yielding also needs to inherit from GDScriptFunctionState. So the documentation should be changed to :

func _ready():
    yield(do_something(), "completed")
    yield(do_something_else(), "completed")
    print("All functions are done!")

func do_something():
    yield(get_tree(), "idle_frame")
    print("Something is done!")

func do_something_else():
    yield(get_tree(), "idle_frame")
    print("Something else is done!")

Notice the yield(get_tree(), "idle_frame") in do_something functions.
Of course yield(get_tree(), "idle_frame") can be anything but this seems reasonable because it will only yield for an idle frame. Sorry i though this was optional and as a result documented it as optional.

If you are planning on calling the same function within a loop, you should consider using [code]yield(get_tree(), "idle_frame")[/code] also. <--- From the docs

_Ready():

func _ready():

:smile:

_Ready():

func _ready():

馃槃

Been losing my mind lately going c# to GDScript >.<

func _Ready():

func _ready():

GDScript is a case-sensitive programming language!
Do not rush like that. :smile: :smile: :smile:

await should solve most of these incorrect usages in the future hopefully: #32034, #33224 (meaning if you do pass null to await, it should handle that without errors).

I've also written some docs on this in godotengine/godot-docs#2644.

Fixed in #34280.

Was this page helpful?
0 / 5 - 0 ratings