How to use multiple inputs in js?

hodok's icon

Hi, I did RTFM, but didn't understand how to use more than one inlet in js object. Function msg_float(x) gives only value from the first inlet.
For example, how to divide first value by the second?

Christopher Dobrian's icon

Use msg_float(), and check to see which inlet the message came in. Like, if I wanted to make a "times" object, the purpose of which is to multiply two numbers, I would do this:

inlets = 2; // create two inlets
var multiplier; // this is a global variable, so it's saved internally (0 initially by default)
function msg_float(f) {
     if (inlet == 1) { // if the number came in the right inlet
          multiplier=f; // set the multiplier to that value, but
     }
     else { // if not, it must have come in the left inlet, so
          outlet(0, f*multiplier); // multiply and send out the result
     }
}

That way, the Max patch only sends numbers in (no need for a special message such as setmultiplier!), and your script figures out what to do with it based on which inlet it came in.

Note that, to be safe, you'd probably also want to have a msg_int() function in your script, like this:function msg_int(i) { msg_float(i); }
so that your times object could also handle ints successfully.

hodok's icon

Thanks! That helped me a lot.

hodok's icon

BTW, why the next code wouldn't work?

inlets = 2;
outlets = 1;

function msg_float(x)
{
    if (inlet == 0){var p = x;}
    if (inlet == 1){var q = x;}
    var pq = (p*q);
    outlet(0, pq);
    }

Christopher Dobrian's icon

In your script all the variables are local variables that only exist as long as the function is running (and are then discarded), so none of the values is retained. Thus, p and q are both 0 when the function begins, only one of them gets changed by the input, so the product of them is always 0. For them to retain their value from one function call to the next, you must declare them outside of any function.

Emmanuel Jourdan's icon

Actually the problem is that the variable are declared in a block in a function. So you can declare variable in functions, they will be visible for the entire function, and if you declare variables in a block they are visible for that block (the thing between {}). And as Christopher mentioned, if you want to have variable who have a longer life time (across multiple calls of your function) you have to declare them globally i.e. outside of any function.

Lee's icon

Javascript doesn't actually have block scope - only global and function scope so in this case p and q are local to msg_float() anyway, and it's still the fact that they're no global that is the issue

Emmanuel Jourdan's icon

Damn you're right Lee. It looks like I've been doing too much C this days ;-)

Lee's icon

yeah, it's a subtle one... i still try to code as if they matter tho, just have to be aware of the hoisting that goes on... i intend to port some of the code over at some point aswell, so that'll make the code better for it's final target