Triggering function with an Ugen

Hi,
I would like to trig a function with an Ugen.

~myFunction = { | amp=0.5 | 
var env=EnvGen.kr(Env.perc, doneAction: 2));
Out.ar(0, SinOsc.ar * amp * env)
}
// ~myFunction is a trivial function as an example:
// Then I want to trig (that is to say to play in that case) ~myFunction with Dust.ar(0.1) for instance...

Thanks in advance.
Yann

Careful - this may not do what you think it’s doing! ~myFunction here is probably intended to define a SynthDef, but doesn’t actually play anything by itself unless you either do ~myFunction.play or actually
define a SynthDef with it and then play it. Let me give you two examples to play around with:

Here’s one that maybe matches what you’re going for: use Dust to trigger a function, and play your simple SinOsc ugen:

(
// Send a reply back from the server when Dust triggers
~dust = {
	var trigger;
	trigger = Dust.kr(1);
	SendReply.kr(
		trigger, 
		cmdName: '/dustTrigger', 
		values: [trigger]
	);
}.play;

// A simple synthdef function you can .play
~simpleSynth = { 
	|amp=0.5| 
	var env=EnvGen.kr(Env.perc, doneAction: 2);
	Out.ar(0, SinOsc.ar * amp * env);
};

// Respond to that reply
OSCdef(\dustTrigger, {
	|msg|
	msg.postln;
	~simpleSynth.play;
}, '/dustTrigger'); // <-- this has to match your cmdName
)

If you’re playing the same Synth multiple times, .play is not very efficient - you probably want to define a SynthDef and re-use that instead. The same thing as above, but rewritten to use a SynthDef:

(
// Send a reply back from the server when Dust triggers
~dust = {
	var trigger;
	trigger = Dust.kr(1);
	SendReply.kr(
		trigger, 
		cmdName: '/dustTrigger', 
		values: [trigger]
	);
}.play;

// A simple synthdef function you can .play
SynthDef(\simpleSynth, { 
	|amp=0.5| 
	var env=EnvGen.kr(Env.perc, doneAction: 2);
	Out.ar(0, SinOsc.ar * amp * env);
}).add;

// Respond to that reply
OSCdef(\dustTrigger, {
	|msg|
	msg.postln;
	Synth(\simpleSynth, 
		args:[\amp, msg[3]]  // this is how you'd pass the value from SendReply on to your new synth
	);
}, '/dustTrigger'); // <-- this has to match your cmdName
)

The first one is exactly what I was looking for.
Thanks again.