this.patcher.getnamed not finding objects

Marc Carlton's icon

I'm having trouble with Javascript not finding objects by their script names. I'm somewhat new to Max MSP (only in my first year or so) and I'm afraid I'm missing something critical for how maxmsp / javascript works.

My JS file and patcher are in directories listed under file prefs. My code appears to be simple and accurate, yet my code referencing objects by script names return as nulls. IE in a patch I have a JS object with the following line of code, and an object with script name "pitchMap", yet it returns null.

//js LoopLayer.js global code
var pitchMap = this.patcher.getnamed("pitchMap");
function loadPitchMap(curve){
    switch(curve){
        case "clear":
            pitchMap.message("clear");
        break;
        case "flat":
            pitchMap.message("clear");
            pitchMap.message(0, 1);
            pitchMap.message(1000, 1);
        break;
        case "up":
            pitchMap.message("clear");
            pitchMap.message(0, 0);
            pitchMap.message(1000, 2);
        break;
        case "down":
            pitchMap.message("clear");
            pitchMap.message(0, 2);
            pitchMap.message(1000, 0);
        break;
    }
}

//returns: js: LoopLayer.js: Javascript TypeError: pitchMap is null, line 66

//function object properties

What are other things I can trouble shoot?
Thanks!

Morgan's icon

Hmm, works for me.

I saved your script in my search path, and read it in with the argument. What's in the subpatch right at the top of your screenshot?

Marc Carlton's icon

Thanks Morgan.
I've discovered something and would love someone to elaborate on or point me to more reading. I simply move the JS object to the left side of my patch, and JS can now find those objects. I remember reading about the order of execution from right to left - but perhaps I need to understand that more. I am experiencing the same strange behavior in several of my projects and I imagine it has to do with me not understanding this.

Morgan's icon

Maybe check your installation of js? If I don't save your script i can get a 'no function. . .' error, but if it's compiled the named object is found just fine on my computer. . .

For ordering, the trigger object is crucial. Makes lots of things unambiguous.

Basically ordering is done for each event, checking the position of the *inlets*, bottom right first, directly up from there before moving to the left

send and receive can get weird, as well as multiple loadbangs.

In the search bar in the top right frame of a patcher window, If you search 'message order and debugging', you should find tutorial 5, also readable here (but without the tut patch)
https://docs.cycling74.com/max7/tutorials/basicchapter05

This article also has a lot of more advanced points
https://cycling74.com/articles/event-priority-in-max-scheduler-vs-queue/

Morgan's icon

Ah, just had an idea. Maybe you named the function object after loading in the script? I managed to recreate your error that way. Wrapping the getnamed call in an init that you can call to refresh could fix it, but if you've saved and opened the whole patch, it should sort out anyway.

//js LoopLayer.js global code
var pitchMap;
init();
function loadPitchMap(curve){
    switch(curve){
        case "clear":
            pitchMap.message("clear");
        break;
        case "flat":
            pitchMap.message("clear");
            pitchMap.message(0, 1);
            pitchMap.message(1000, 1);
        break;
        case "up":
            pitchMap.message("clear");
            pitchMap.message(0, 0);
            pitchMap.message(1000, 2);
        break;
        case "down":
            pitchMap.message("clear");
            pitchMap.message(0, 2);
            pitchMap.message(1000, 0);
        break;
    }
}
function init(){
        pitchMap = this.patcher.getnamed("pitchMap");
}

Marc Carlton's icon

Thanks again Morgan! I really appreciate your help. That's interesting. I actually experience this even when restarting max and even different multiple machines.
Perhaps wrapping getnamed defines in an init as you suggest could be a good practice. Kind of weird.

Morgan's icon

Sure thing.

hmm. If you're naming the ui object with a message going in, that'll happen after the js loads.

Marc Carlton's icon

I name objects via the inspector "scripting name" and then use the typical Global JS code:
var pitchMap = this.patcher.getnamed("pitchMap");
Also putting that in init seems to fix the issue. I don't know what why.

Morgan's icon

Probably an order of execution thing

Leigh Marble's icon

See the section on “Global Code” here.

What not to do [in Global Code]: Refer to your object’s Patcher (see the Patcher Object for more information).

That’s why setting the Max object handle “pitchMap” works from within an init() function, but not from Global Code. Max hasn’t finished setting up the Patcher object yet when the JS global stuff runs.

estevancarlos's icon

I think I'm having a similar problem but no solution. I created an init() that assigns a named object to a variable. Not working. The variables are reported as "not defined". I've tried running the init() at the top, the bottom and within a function before the variable is used.

Leigh Marble's icon

What are you using to fire the init function?

Can you post your code here?

Barry Despenza's icon

Hi can someone help me fix a patch

Barry Despenza's icon

I keep getting error messages