Calling a function with a parameter defined in the JS code but from outside?

Roald Baudoux's icon

Hello,

I have some function to handle arrays in JS. The function has a parameter, that is the array to be passed to the function.

It's something like:

var points = [];

function myfunction(myarray) {
}

I can call the function within JS using this code:
myfunction(points)
However, is it possible to call the same function with the same parameter but with a message from the patcher containing the JS object, not from the JS code? Messages like myfunction(points) or myfunction points do not function.

Roald Baudoux's icon

Mhhh, no JS-guru in the neighbourhood?

Iain Duncan's icon

As far as I know, you would need to make a handler that accepts a string message and evals that message in JS. It's likely to be cumbersome because Max message syntax and JS syntax don't map well to each other, and dynamic evaluation in JS is clunky anyway between JS syntax and JS string handling.

If this is the kind of thing you are interested in doing and you don't need your scripting to be JS, the Scheme for Max is designed to do this specifically, and Scheme syntax and Max message syntax map to each other very well. It's as simple as sending a message to inlet 0 of "my-function points". I walk through it in the intro video if you're interested. totally understand if a new language is not what you're after though!

https://github.com/iainctduncan/scheme-for-max

Roald Baudoux's icon

Thank you @Iain. I have never thought about Scheme. Should have a look.

Iain Duncan's icon

No prob, hit me up if you need help getting going. The more early beta testers the better! I am in the process of writing a beginners guide to S7 Scheme too and will put it up in the next week or so. The videos on the youtube channel should give you a sense of whether it's the kind of thing you'd like. :-)