How to copy event from inside the event

I know this is similar to this topic but the answer there didn’t quite work for me.

I am trying to hack together a kind of livecoding interface using events. I would like to define functions on my event that return a modified copy of the event but I can’t figure out how to access the equivalent of the this keyword from other languages.

When its run, aside from the error. The console should show the value of currentEnvironment to be the outer environment and not the event. How do I access this for an event?

Here is my example:

(~x ={|name, synth|

	var api = (
		synth: synth,
		name: name,
		controlPats: (),
		play: {
			|server|
			var s =  (type: \set, id: ~synth.id);
			var id = ~synth.id;
			var ps = ~controlPats.collect({
				|p,key|
				Pbind(*[type: \set, id: id ]++[key, p.pat, \dur, p.dur])
			});
			Pdef(name, Ppar(
				ps
			)).play
		},
		c: {
			|key, pat, dur(1)|
			var e = ().putAll(currentEnvironment);
			"e is... ".postln;
			e.postln;
			e.controlPats[key] = (pat: pat, dur: dur);
			e
		}
	);
	api
};

~synth !? {~synth.free; ~synth=nil};
~synth = ~synth??{{
	var env = EnvGen.ar(Env.perc, \gate.kr(1));
	var osc = SinOsc.ar(\freq.kr(440), mul: env*\amp.kr(1));
	Pan2.ar(osc, \pan.kr(0))
}.play};


~x.(\fred, ~synth).c.(\degree, Pseq([1,2,3],inf)).play
)

hen’t looked through your example carefully but…

when you call a function inside an Event the first argument of the function will be essentially this

e = (a:5, b: {|self| self.a + 1});
e.b; // (notice that you don't have to call `.value` on e.b. b is treated as a method call)

if you want a pseudo-method to return an altered copy of an Event you can do like so

e = (a:5, b: {|self| self.copy.put(\c,9)})
e.b

pass in arguments to a pseudo-method like so:

e = (a:5, b: {|self num| self.copy.put(\c,num)})
e.b(8)
1 Like

Edit: nevermind! i figured it out. For pseudo-methods you don’t need the extra . for the call. In the example below I should have written a.c(\degree, Pseq([1,23])) and not a.c.(\degree, Pseq([1,23]))
Thank you! That explains so much about what was going on in my example. Can you have arguments other than self in these pseudo-methods? For example:

a = (
  c: {
    |self, key, pat, dur(1)|
    key.postln;
    pat.postln;
  }
);
a.c.(\degree, Pseq([1,2,3]));

prints out nil for key and pat