Getting the integer value of a Number Object

jmca's icon

Is there a way to get the value of a Number object with JS?

I tried:

var aVal = theNumObj.message("int");
var aVal = theNumObj["int"];

I figured if you can set the value of a Number obj you could also get the value. It seems odd to me that I can't find anything about this in the docs, unless I'm totally missing something.

Luke Hall's icon

It's not possible to get the currently displayed number in the [number] object this way in javascript. The message() function is used to send data to an object but can't be used to retrieve it.

Your best bet is to just connect your [number] to a [prepend some_id] and then straight into your [js] where you can use a global some_id(x) function to receive it.

There is a messy/hacky way that this can all be done in javascript so that the value is not sent out the [number] outlet when queried. It involves creating a [grab] object, connecting it in a loop with your target object and the [js] object and retrieving the value that way. You need to give each object you want to query a scripting name. Here's an example, change the values of the ui objects the click on the message.

lh

Max Patch
Copy patch and select New From Clipboard in Max.
// lh.grab.js

inlets = 1;
outlets = 2;

function anything() {
    all = arrayfromargs(messagename,arguments);
    outlet(0,all[0]+":",all[1],"n");
}

function grab(objname) {
    theui = this.patcher.getnamed(objname);
    thegrab = this.patcher.newdefault(0, 0, "grab", 1);
    this.patcher.connect(this.box, 1, thegrab, 0);
    this.patcher.connect(thegrab, 1, theui, 0);
    thepre = this.patcher.newdefault(0, 0, "prepend", objname);
    this.patcher.connect(thegrab, 0, thepre, 0);
    this.patcher.connect(thepre, 0, this.box, 0);
    outlet(1, "bang");
    this.patcher.remove(thegrab);
    this.patcher.remove(thepre);
}

// EOF
jmca's icon

Thanks for detailed example. It's a bummer there's no JS getter functions built into the object(s). 2 other solutions that I came across are:

- Using global object variables with send/receive objects
- Using pattr with send/receive objects (I'm still hazy on this one)

So I think I'm understanding correctly that generally JS to Maxobj communication is a one way route. Maxobj don't really have getter methods (other than the listed attributes) built in, so only output to JS are via outlets.

Eddy's icon

I did something similar recently by using a dedicated inlet and outlet for the 'query path'. When my Javascript wants to read the value of a number box it sends a bang out of an outlet dedicated to that purpose, which is connected to the inlet of the number box in question. The outlet of the number box is connected to a dedicated inlet on my JS object which receives the value.

This doesn't scale that well, but it works great for a handful of number boxes, which is all I needed. I'm sure there are more elegant ways (as shown above) but for a 'quick solution' it was fine in my app.

Eddy

Emmanuel Jourdan's icon

For pattr aware object you might get some chance with that:

function bang()
{
var obj = this.patcher.getnamed("toto");
outlet(0, obj.getvalueof());
}
`