Forums > Javascript

patcher.getnamed not finding named objects


Aug 24 2012 | 5:29 pm

I have a max send object in my patch that I have named "test_send".
In my js script, I make the following call…

function loadbang()
{
obj = patcher.getnamed("test_send");
}

obj ends up being null! Can someone please explain why this
is not working?

Thanks,
Anthony

Aug 27 2012 | 7:51 am

As you call getnamed at loadbang, maybe it’s called before the object has been named… I had similar problems with named objects and JavaScript and had to use a deferlow (so no loadbang function in the JS).

I read in the doc (http://cycling74.com/docs/max6/dynamic/c74_docs.html#jsbasic -> Global Code) that you cannot Refer to your object’s Patcher (see the Patcher Object for more information) in the Global Code.

p

Aug 27 2012 | 8:46 am

What you describe happens to me, too. I usually try to set everything up after I’m sure the patch has loaded completely.

If you’re just trying to send something through the object, you can just use ‘.messnamed()’ to address the send. Otherwise, you could either initialize your local assignments from outside the patch, or try using a Task() object and use its .schedule() method in loadbang() to call a seperate function that makes your scripting assignments:

var obj;
var getstuff = new Task(this, getstuffnow);
function loadbang(){
  getstuff.schedule(1000);
}
function getstuffnow(){
  obj = this.patcher.getnamed("test_send");
}

Tasks have been flaky on me in the past, but I’ve found them to be much more reliable in Max6.

edit::I just noticed after rereading Patrick’s post that you’re using the variable "patcher" to refer to the patcher without the jsthis instance. If you’ve defined "patcher=this.patcher" in global code, that might be why its not working. It should be:

this.patcher.getnamed('whatever')

Aug 27 2012 | 7:21 pm

Hey guys, thanks for your replies. But I am still seeing some odd behavior…

So I moved my js object init code to a function that I manually trigger
once the patcher is already loaded.

dsend = patcher.getnamed("test_send");

This line still returns null. Just so that I am clear on this,
getnamed should return the object whose script name matches
"test_send". Yes? Am I missing something?

Anthony

Aug 27 2012 | 8:25 pm

Again, the syntax should be:

dsend = this.patcher.getnamed("test_send");

Aug 27 2012 | 9:48 pm

I tried both, no difference.

this.patcher.getnamed and patcher.getnamed should resolve to the
same thing. Unless I have defined a variable called "patcher" of my own.

Aug 27 2012 | 9:55 pm

Anthony,

Does your send object have the varname set to ‘test_send’? The send ‘name’ that you set in the box as an argument is different than a varname.

-Ben

Aug 27 2012 | 10:14 pm

Oh, right. Of course it should :) I should have thought about that a moment longer.

Aug 28 2012 | 4:34 pm

Ben, I am setting the scripting name of the object. I am not sure if this is the same
thing as the varname. The send name and scripting name are different.

But I think I have found the problem. It appears that if my js object is in a subpatcher,
and the send object is in the root patcher, this.patcher.getnamed does not find the object.

It seems like patcher.getnamed does not have a global scope. Is this correct?

Aug 28 2012 | 6:10 pm

Getnamed can only see where you point it which, in your example, is in this.patcher. If you want it to look into a subpatcher or parent patcher you have to tell it explicitly where to go!

Aug 28 2012 | 6:19 pm

Is there a way I can tell it to start at the root patcher?

Aug 28 2012 | 6:44 pm

Okay I have come up with something that works. The following function
will find a named object globally. Which is the way I think getnamed should
be implemented in the first place.

function FindObj(objname)
{
var root = this.patcher;
while(root.parentpatcher != null)
root = root.parentpatcher;

var obj = root.firstobject;
while(obj != null)
{
if(obj.varname == objname)
return obj;

nextobj = obj.nextobject;
if(obj == nextobj )
break;
else
obj = nextobj ;
}
return null;
}

Sep 05 2012 | 8:38 am

write custom init function and trigger by another loadbang + deferlow object

Sep 06 2012 | 5:27 pm

Again, the scope of getnamed() is the patcher. How does your suggestion solve this problem?

Viewing 14 posts - 1 through 14 (of 14 total)

Forums > Javascript