Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | michaelpbl |
A number of people have advocated using the yield function to create a delay. In particular, in his excellent tutorial (1), Ombarus advocates the folowing code:
extends Node2D
func _ready():
method2()
func method2():
print("Called method2")
yield(method3(), "completed")
print("Exiting method2")
func method3():
print("Called method3")
yield(get_tree().create_timer(2.0), "timeout")
print("Exiting method3")
Which produces the following output:
Called method2
Called method3
------ 2 second delay ------
Exiting method3
Exiting method2
(I have changed his method numbers, simply because I want to insert one more method.) Now if we change the code as follows:
extends Node2D
func _ready():
method1()
func method1():
print("Called method1")
var var1: Node2D
var1 = method2()
var var2 = var1.getMyParent()
print("method2 returned ", str(var1))
print("Exiting method1")
func method2():
print("Called method2")
yield(method3(), "completed")
print("Exiting method2")
return $Child
func method3():
print("Called method3")
yield(get_tree().create_timer(2.0), "timeout")
print("Exiting method3")
Here Child is a simple Node2D with a script attached with a single getMyParent() function. Now Godot generates an error:
Trying to assign value of type 'GDScriptFunctionState' to a variable of type 'Node2D
If I comment out the var2 assignment, no error is generated.
In my game I am dealing cards and using using a 0.5 sec delay after each card dealt to make the deal more natural. The yield function works on normal deals, say 5 cards to each player. But when I try to deal until the first jack shows up and then return the player who got it, I run into this error. I have tried a number of workarounds such as moving the yield into one of the called functions, delegating the delay to a delay function, etc. but none of them work.
Can anyone explain what is going on here and suggest a way around it?