Array output from js object outlet broken?
I am simply trying to outut a javascript array as a list in Max, but this is not working. Instead of seeing a nice list, I see a list of "jsobject23423423 ..." items at the outlet of my object. Yet, the output from post() in the Max window is exactly right - a sequence of numbers. ??????
function makeList()
{
var l = new Array();
for (var i=0; i
l.push(i);
return l;
}
// output list of numbers
function bang()
{
var theList = makeList();
post(theList + "n");
outlet(0, theList);
}
You code works nicely.
I just had to add the following lines of course :
inlets = 1;
outlets = 1;
size = 7; // array length
Sorry, the problem seems to be elsewhere - that was just a piece of my code. I've tracked it down properly - it has to do with adding a spliced element to an array. I expect to get an array of numbers (ints) back, but instead I get an array of complex js objects!
code here reproduces the problem:
var size=10;
var theList = new Array();
function loadbang()
{
size=Math.floor(Math.random()*10+2);
post("size: " + size + "n");
theList = new Array();
}
function makeList()
{
var l = new Array();
for (var i=0; i
l.push(i);
return l;
}
// output list of numbers
function bang()
{
theList = makeList();
//post(theList + "n");
var a = new Array();
for (var n=0; n
{
var e = theList.splice(0,1);
// this should add a number!
a.push(e);
// this will print out a number
post(e + "n");
}
// now outputs array of js objects?
outlet(0, a);
}
A workaround - use parseInt() to make sure that the spliced array element is properly cast to int. But this should not matter, Max should be smart enough to cast the array elements properly - it does it already via the post() function! This is a bug, me thinks.
if you try to splice one element from an array, you still end up with an array (of one element) and not a number.
so the array (a) you created, is an array of one-element-arrays. max doesn't unroll arrays of higher dimensions, so it outputs the elements of the first dimension, which are jsobjects (arrays in this case).Quote:Max should be smart enough to cast the array elements properly - it does it already via the post() function!
i'm not sure max does this cast when post() is called. what you see in the max window is a printout of one-element arrays.
Uses comas to separate the js array and the string, otherwise the toString() method of the Array is used (if one of the operand of the + operator). post() and outlet() are clever enough to automatically unroll one level of an array.
post(theList, "n");
No, EJ, I'm afraid they're *not*. post() may be, but try the code I've posted and you'll see that outlet() doesn't unroll a list, it only converts it to a js object string. This is problematic, and not at all expected behavior. A list should be unrolled into a valus that Max can handle by default, not converted to a string representing the js object itself. For example, what good would it ever do to have a printout of the jsbojectXXXXX reference in Max? Output should be fully evaluated or else it is at best useless, at worst confusing and unexpected.
Cheers
Evan
I can't reproduce that using the latest version of Max with none of the above js. outlet() unroll the list as expected. Please attach an example and a patch just to make sure it's not a character encoding issue of some sort. JS files are supposed to be UTF-8.
it a feature, not a bug
as descriped before the splice function
does not return a number. its an array.
in your case with 1 element.
if you fill your array like this, everything works fine:
// this should add a number!
a.push(e[0]);
normaly max can not handle moredimensional arrays.
so its a feature, that the post method unrolls
the array
in this case its a bit unclear, because of the different
handling between the post() and the outlet() function.
I dig out this 9 years old thread, as I face a similar problem. But is seems a bit subtler in my case. As long as I outlet from a js
object an array composed int, everything works fine. The message box is fulfilled with a message like 22 11 45 45
. Great!
But if ever I outlet an array composed of strings, then I receive message composed like jsobect 255045736 jsobect 25504572 jsobect 25504578
.
What am I supposed to do to outlet properly an array composed of string from a js
object ?
Thanks in advance for any hint that would help me to solve this.
Regrards / Ug
I agree with Lists right now, it's so unexpected that an outlet from JS would be converted to some strange jsobject identifier.. does anyone have a simple solution for obtaining a value of an outlet that is straight up what the variable IS and not some jsobject? What code or patchers should I run in conjuction with this? Thanks!