In the GDScript manual, I saw that a GD coroutine object is created when ‘await’ is used. However, since the GD coroutine object was not returned when calling ‘await’, I was curious where the GD coroutine object was.
I tested it to see if a GD coroutine object was created inside the node.
I created a coroutine class for my task that inherited Node and called ‘await’ in the run function.
I tested whether the GD coroutine object was actually created inside the node.
class_name CoroutineManager
static var _co : Coroutine
class Coroutine extends Node:
func run():
print("test 1")
await get_tree().create_timer(2).timeout
print("test 2")
pass
static func test_run(node : Node):
_co = Coroutine.new()
node.add_child(_co)
_co.run()
static func myFree():
_co.free()
When calling test_run, a My Coroutine object is created and the run function is called.
print(“test 1”)
Call and rest for 2 seconds (await get_tree().create_timer(2).timeout)
print(“test 2”) was called
What I wanted was to remove the node that called ‘await’ while resting for 2 seconds (await get_tree().create_timer(2).timeout).
If print(“test 2”) is not called,
I can see that a GD Coroutine has been created inside the node that called ‘await’.
-------# This is another external code
print("Main Start")
var mgr = CoroutineManager.new()
mgr.test_run(self)
await get_tree().create_timer(1).timeout
mgr.myFree()
print("Main End")
The key here is the code above
After calling the test_run function
via “await get_tree().create_timer(1).timeout”
As soon as I rest for 1 second, the node (class Coroutine) object I created is removed.
When I run this code, as expected, print(“test 2”) is not called and
If the wait time is greater than 2, print(“test 2”) is called.
The conclusion is that when ‘await’, a GD Coroutine object is created inside the node that called ‘await’, and when the node is removed, the GD Coroutine is also removed.
If I misunderstood, please reply.