Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Aaron | |
Old Version | Published before Godot 3 was released. |
I have some code that I try to pause using yield and signals, but it’s not pausing and continuing right.
I have this node structure:
node (node.gd)
event 1 (event.gd)
event 2 (event.gd)
event 3 (event.gd)
The “event” nodes have an execute
function, where an event does an operation and then recursively calls execute
on its children. An event’s operation or the operations of its child events may involve animations and other long actions, so I want to pause when running a child before moving on to the next child. I’ve been trying to do with with yield and signals emitted by an event node.
event.gd
extends Node
signal event_complete
func execute():
print(get_name(), ": starting self event")
_do_event()
print(get_name(), ": self event finished")
if get_child_count() > 0:
print(get_name(), ": doing child events")
for i in range(get_child_count()):
var c = get_child(i)
assert(c extends get_script())
print("firing event: ", c.get_name())
c.execute()
yield(c, "event_complete")
print("finished firing event: ", c.get_name())
if get_child_count() > 0:
print(get_name(), ": child events finished")
self.emit_signal("event_complete")
func _do_event():
# Test code. Could be anything.
print(get_name(), ": hello world")
node.gd
onready var event_1 = get_node("event 1")
...
func fire_event_1():
print("node firing event: ", event_1.get_name())
event_1.execute()
yield(event_1, "event_complete")
print("node finished firing event: ", event_1.get_name())
So during execute
function an event does its operation (_do_event
) then calls execute
for each of its children, pausing until the current child emits event_complete
, then emits event_complete
itself. The root node likewise calls the first event’s execute
function then waits until the first event is finished.
None of the yields are working right.
Somehow in the node script it never gets to the “node finished firing event” print statement the first few times. And when I call the node’s function fire_event_1
the first time it never calls execute
on “event 3”, but it does when I call fire_event_1
a second time.
This is what’s printed when fire_event_1
is called for the first time:
node firing event: event 1
event 1: starting self event
event 1: hello world
event 1: self event finished
event 1: doing child events
firing event: event 2
event 2: starting self event
event 2: hello world
event 2: self event finished
What’s printed when fire_event_1
is called a second time:
node firing event: event 1
event 1: starting self event
event 1: hello world
event 1: self event finished
event 1: doing child events
firing event: event 2
event 2: starting self event
event 2: hello world
event 2: self event finished
finished firing event: event 2
firing event: event 3
event 3: starting self event
event 3: hello world
event 3: self event finished
And what’s printed when fire_event_1
is called a third time:
node firing event: event 1
event 1: starting self event
event 1: hello world
event 1: self event finished
event 1: doing child events
firing event: event 2
event 2: starting self event
event 2: hello world
event 2: self event finished
finished firing event: event 2
firing event: event 3
event 3: starting self event
event 3: hello world
event 3: self event finished
finished firing event: event 3
event 1: child events finished
node finished firing event: event 1
node finished firing event: event 1
What is the proper way to pause and resume my code during an event node’s execute
function? Why do are my yield statements not resuming when I thought they’d resume given their signal parameters?