Node - Store a Max message as a variable
Hey all! I'm brand new to Node, and it's been a while with Max. I'm not the best programmer either, but I've got a project in mind!
To start, I'm having trouble storing a message as a variable and printing it to the console with a separate bang. Sending the message prints it properly to the max console, but the separate bang does nothing. Any tips?
// Variable set by message
const data1 = maxAPI.addHandler("vt", (msg) => {
maxAPI.post(msg);
return msg;
});
// Bang prints message to Max console
maxAPI.addHandler("bang", (data1) => {
maxAPI.outlet(data1);
});
I am a newbie, but I think that if you want to store a variable, it should be var, not const.
And I suggest you give all the patch for people to check it. For example what does vt stand for?
Here is the Max patch to go along with the script. "vt" is basically a prepend of a message that I want to handle a particular way. Specifically, bang it, and outlet the message.
This is exactly what I want. I'm having trouble wrapping my head around the why.
Couple of things:
- I thought JS required that a variable be assigned a value when it was declared. Perhaps NodeJS does not require this? I thought declaring "messagestored" the way you have would be invalid.
- Does the "msg" parameter not pass to the second handler because it is not a global variable? By returning and assigning it to "messagestored", then it is available as a global variable?
This is tricky stuff. Your help is much appreciated! :)
Hey, you guys are basically on the right track here. Couple of quick things:
1. var, const and let. Variables declared with const can't be changed after they're defined. Variables declared with let can. As for var, there are special cases where it makes sense, but it's pretty rare, and let is usually the better choice.
2. In your handler function for bang, the function is called without any arguments (a bang message doesn't have any arguments after all). So when you do:
maxApi.addHandler("bang", (data) => {console.log(data)})
`data` will always be undefined. That's why you're not getting anything back out of node.script when you call `outlet`. What were you expecting to get back out, anyway? A bang?
3. Handler functions are welcome to return something if they want. But Node for Max is just going to ignore it.
Hope that helps!!
MC
From what i've learned so far, only 'const' needs to be initialized when declared, but 'let' and 'var' variables do not(this might be according to the standard of Javascript used, i'm learning 'ES6' but not sure what standard N4M is using).
Spot on. I was making a number of rudimentary mistakes!
Sam
3. Handler functions are welcome to return something if they want. But Node for Max is just going to ignore it.
Now this is making sense to me! MC, I didn't have to return anything from that msg handler, either. It's much simpler. The message simply needed to be assigned to the global variable in that handler. So, it now looks like this (it looks a little different because I also changed the formatting):
// Global variable initialized, but not assigned anything.let vtStored;
maxAPI.addHandlers({
// When message " vt msg" is received, assign "msg" to global variable.vt: (msg) => {
vtStored = msg;
},
// When bang is received, post vtStored to max console.bang: () =>{
maxAPI.post(vtStored);
}
});
Thanks to all of you! :)
@Raja when it comes to the Standard. Node for Max is shipping/based on the Node LTS release and the package shipped with Max 8.0.0 bundles Node v8.11.3 LTS and NPM v5.6.0. So where does that leave you in terms of supported EcmaScript features in this runtime - The at first sight big yet super helpful support table/overview over athttps://node.green/ is the answer!
Hope that helps.
Florian
How does one process any message without using addHandlers to specify whether its a specific type of message such as a bang? Would this involve the keyword 'anything'?
@SHAIKAT you can use MESSAGE_TYPE.ALL when registering your handler, see here