I'm trying to loosely-couple OSC message bindings to the function that handles them:
f = {|msg| msg.postln};
OSCFunc({|msg, time, addr, recvPort| f(msg)}, '/2/push1')
I thought this was pretty straightforward. I could substitute f at any point, and therefore muck about the handling of messages from the path /2/push1
at will.
But when I hit the button (sending a message with path /2/push1
), I get an error saying:
Message 'f' not understood.
So I'm guessing f
has a different meaning within the scope of the function declared in the call to OSCFunc
. I guess it has a different Environment
?
I also tried putting the function in a regular variable:
(
var myFunction = {|msg| msg.postln};
OSCFunc({|msg, time, addr, recvPort| myFunction(msg)}, '/2/push2');
)
But this results in the same error.
Is there a way around this? Surely I don't have to place an entire function body within OSCFunc
every time?
No, your problem is just a SuperCollider syntax issue - it lies in what you've written here:
f(msg)
I think that you're hoping this "invokes" the function f
with msg
as an argument. However, SuperCollider's syntax isn't quite like that - it actually interprets that as being an equivalent way of calling msg.f()
, which is why it throws an error saying that msg
knows no f
message. Instead, you need to use the value
message on your Function:
f.value(msg)
I can't find a tutorial that spells this out right now, so instead here's a link to the Function helpfile.