dcm
November 12, 2022, 12:13am
1
How does yieldAndReset work?
In this example I would have thought that the Routine would need to reset >before< calling play to return something other than nil.
However, this is not the case. It is good that it works - I would like to know why it works.
~a_part = Routine({
1.wait;
"dog".postln;
1.wait;
"cat".postln;
~a_part.play;
2.wait;
~a_part.yieldAndReset;
});
~a_part.play;
rdd
November 12, 2022, 2:45am
2
Number>>wait is simply Object>>yield , i.e.
// prints the clock time and x after 1 second
Routine({ 1.yield.postln; "x".postln }).play
Yielding anything other than a number at a scheduled routine stops it from continuing, i.e.
// doesn't print the clock time or continue to x
Routine({ "1".yield.postln; "x".postln }).play
Object>>yieldAndReset is the mechanism for restarting a routine from the inside.
// prints x every second, doesn't print clock time or continue to y
Routine({ "x".postln; 1.yieldAndReset.postln; "y".postln }).play
The routine you wrote is very subtle! To just print cats and dogs perhaps:
Routine({ "dog".postln; 1.wait; "cat".postln; 2.yieldAndReset }).play
Or perhaps:
{ { "dog".postln; 1.wait; "cat".postln; 2.wait }.loop }.r.play
1 Like