Passing parameter to looping Routine/Task

I am trying to pass a parameter to a Routine running a loop. A simple case would be:

(
r = Routine.new({
	arg item;
	var i = 0;
	loop{
		i = i + 1;
		postf("% %\n", i, item);
		5.0.yield;
	}
});
r.play;

//r.play("potato") - doesn't work
)
What I want is for the r to accept an argument e.g:
r.play(“hello”); however ‘play’ doesn’t appear to accept such arguments; neither does Task unless something’s staring me in the face. Is there something about Thread objects that prohibits this? Help would be much appreciated… Thx

You can pass a value to a routine when calling .next, but you need to make one tweak for that:

r = Routine.new({
	arg item;
	var i = 0;
	loop {
		i = i + 1;
		postf("% %\n", i, item);
		// HERE:
		item = 5.0.yield;
	}
});

But .play doesn’t evaluate the routine directly. It schedules the routine to be evaluated later on a clock. So 1/ the play arguments are about scheduling – they are not passed into the routine, and 2/ because it’s the clock that calls the routine, you can’t control the argument that the clock passes in (which is the current clock time).

You’d have to play a different routine, and use that Routine to call the first one.

r = Routine.new({
	arg item;
	var i = 0;
	loop {
		i = i + 1;
		postf("% %\n", i, item);
		item = 5.0.yield;
	}
});

t = Routine {
	var delta = 0;
	while { delta.notNil } {
		delta.wait;
		delta = r.next(your_arg_here);
	}
}.play;

There may be a better approach, but we’d have to understand why you need to pass an argument to a call that isn’t under your direct control.

hjh

Thx for your reply hjh. Your closing remark caused me to question whether I really did need to relinquish control in that way; just the nudge I needed to consider whether there may be better possibilities.

BTW. The forum has to be up there with the best of them for getting help in any language and I include StackExchange etc. Many others could learn much from scsynth.org.

PS Tried to ‘accept’ your answer but could find the button. :slight_smile: