Having some troubles setting object-properties using javascript
I'm trying to figure out how to do some stuff with javascript and the js object, but i'm having some basic problems.
- i'm succesfully creating objects using the newdefault function on this.patcher
- i found out that i can see the correct propertynames i should use by clicking the 'show attribute names' in the inspector
- i can succesfully set an objects scripting name through the varname property
- i can also print that property correctly by doing post(object.varname);
So, i thought i had that part figured out. However, this doesnt seem to work when i create a live.dial and want to set the 'showname' property. Or some other ones it has. Also, when i post that property, i get to see 'jsobject' with an id, instead of the value it clearly has (in the inspector).
What am i doing wrong here?
I just got started with js in max, and trying to figure out how things work. Since i saw that i could set the varname through someObject.varname, I assumed that i could set any attribute that way, but that doesnt seem to be the case. I just found the docs (https://cycling74.com/docs/max5/vignettes/js/jsmaxobj.html) and see that obj.varname indeed exists on the object, but other attributes probably need to be set differently.
I looked at your js-file, and see that you get an attribute's value by doing object.getattr('someAttr'), that does work correctly indeed (i tried object.someAttr to do that). A question i have here is: why don't i see that in the docs? I'd expect that getattr-method to be listed here: https://cycling74.com/docs/max5/vignettes/js/jsmaxobj.html
So how would i set that attribute then? I tried setattr('name', value) but that doesn't work.
I found out that i should set attributes through the object.message method, although i cant seem to do that to the live.toggle object. I can set a normal toggle to its selected state by doing:
toggle.message('set', 1); // or toggle.set(1), or toggle.bang();
but whenever i use a live.toggle object, it doesnt respond to those messages, while the help says it should. Am i overlooking something here?
Well, that is really weird. I opened your example and it did indeed work just fine. Going back to my own project, doing a obj.message(1) only works when it's a regular toggle. I did some searching and came to this:
function createToggle(type)
{
toggle = this.patcher.newdefault(Math.random()*300, Math.random()*300, type);
toggle.message(1);
}
The function creates a toggle but again: turning it on doesnt work when it's a live toggle:
createToggleType toggle -> works fine
createToggleType live.toggle -> works fine except for the toggle not being selected
However, when i retrieve an existing live.toggle and send the same message, it *does* work:
function turnLiveToggleOn()
{
this.patcher.getnamed('aLiveToggle').message(1);
}
Seems like a bug to me...
You can't assume the object is ready to receive a value just after you created it. You might want to use a clock to make the assignment later.