How to retreive data from max send objects (aka receive inside JS)recev

Salvator's icon

Hello,

I'm fine with sending data from JS to max land (using messnamed) but how to do the other way around ?
How to receive inside JS data that are sent from my max patch through 'send' object ?

Thanks for any help,

Salvator

Salvator's icon

Anyone ?

Salvator

Luke Hall's icon

It could be done by writing a javascript that uses global functions. You could replace your [send]s with a js that stores a global that you can use in your other js and that also uses the messnamed thing to act as a regular [send]. I'm about to head to work but if you want I'll put something together this evening and post it here.

lh

glBeatriz's icon

It's been a while since this post, but I'm curious: how do you declare global functions in js? could you post a simple example of how to communicate two javascripts?
thanks in advance!

Macciza's icon

Hi
Try sticking a js object in a patch and get help on it . . ..
It will tell you "For more info about the js object, pease consult the Javascript in Max documentation, and the various examples located in ./examples/javascript. "
In these you will find what you need to know, there is even a globalvar example patch . . .
Cheers
MM

Luke Hall's icon

Here's some code that will work like a simple version of the [value] object, the only difference being that you don't set a name for the object, every instance will be linked.

glob = new Global("uniquename");

function anything() {
    glob.tosend = arrayfromargs(messagename, arguments);
}

function bang() {
    outlet(0,glob.tosend);
}
Peter Castine's icon

Umm… how about hooking up a [receive] object to one of your [js] inlets?

It has the advantage that you can double-click on the [send] and the recipient will be in the popup menu. Big help for debugging.

glBeatriz's icon

Thanks for the answers
@Macciza I did know a little bit about the global variables, but I thought that they were "variables", not functions, that´s the reason why I asked. But surely I missed something in the documentation, I'm quite new with js and scripting in general.

Max Patch
Copy patch and select New From Clipboard in Max.

I'll check out Luke's example

//js file code.js

inlets =1;
outlets=1;

function printHello() {
outlet(0, "hello");

}

//end js file

flies's icon

BTW: in javascript, you can assign a variable to contain a function. This is occasionally useful when, for instance, you want to determine which function you want to use on the fly. see http://stackoverflow.com/questions/4908378/javascript-array-of-functions for example.