Updating argument inside of the loop

Hello,

Sorry for the noob question, but is it somehow possible for the argument of the while loop to update/refresh itself with the current status of the global variable (~switch1 when I change it’s value from1 to 0 elsewhere) from within ?

~check = {arg switch;
	while({switch > 0},{
	"waiting".postln;
	1.wait;
	})}

Routine({
	1.postln;
	1.wait;
	~switch1 = 0;
	~switch2 = 0;
	~check.value(~switch1 + ~switch2);
	2.postln;
	1.wait;
	3.postln;
	1.wait;
	~switch1 = 1;
	~check.value(~switch1);  //here gets stuck
	4.postln;
	1.wait;
	5.postln;
	1.wait;
}).play

~switch1 = 0  // I would like to resume it by executing this

I would like to avoid using many of these :

~check1 = {
	while({~switch1 > 0},{
	"waiting".postln;
	1.wait;
	})};

~check2 = {
	while({~switch1 + ~switch2 > 0},{
	"waiting".postln;
	1.wait;
	})}

Thank you !

Simple answer: provide a function instead of a value to your ~check:

~check = {arg switch;
        // calling value here, since switch is now a function
	while({switch.value() > 0},{
	"waiting".postln;
	1.wait;
	})}

Routine({
	1.postln;
	1.wait;
	~switch1 = 0;
	~switch2 = 0;
	~check.value({~switch1 + ~switch2}); // giving a function here
	2.postln;
	1.wait;
	3.postln;
	1.wait;
	~switch1 = 1;
	~check.value({~switch1});  //here gets stuck (giving a function here too)
	4.postln;
	1.wait;
	5.postln;
	1.wait;
}).play

~switch1 = 0

Buttt… for these kind of tasks also check out Condition class

2 Likes

well Condition looks awful useful - not sure how I missed that…

Oof that was embarassingly simple. Thank you !