Structure in JS patch not working's picture

Why is it this simple JS code doesn't parse?

function (__structure Points_A) main ()
   if (!_testMode)
   var Points = new Array();
   for (i=0; i<2; i++)   
         Points[i] = new Object();
         Points[i]["X"] = .1.;
         Points[i]["Y"] = 0.1;
         Points[i]["Z"] = .3;
   result.Points_A = Points

When the very similar but more complex code like this is fine:

var    scale = 0.5
function (__number J, __structure Point_Structure, __structure Points_A, __structure Points_B) 
   main (__number segments, __number rotations, __number range)
   if (!_testMode)
   var   height = - range;
   var result = new Object();
   var points = new Array();
   rot = 0;
   groove = 3.141592653589793 * 2/3;
   Rstep = rotations * 2*3.141592653589793 / segments;
   Zstep = 2 * range/segments;
   var j = 0;
   for (i=0; i<segments*2; i=i+2) 
         points[i]= new Object();
         points[i]["X"] = Math.sin(rot) * scale;
         points[i]["Z"] = Math.cos(rot) * scale;
         points[i]["Y"] = height;
//         The next line creates an object *AND* populates it with data from I'm not sure where from… Try it! (comment out the 3 lines following it at same time)
         points[i+1]= new Object();
//         These 3 lines can be removed and points[i+1] still gets data put in it… weird
         points[i+1]["X"] = Math.sin(rot+groove) * scale;
         points[i+1]["Z"] = Math.cos(rot+groove) * scale;
         points[i+1]["Y"] = height+0.0;
         rot += Rstep;
         height += Zstep;
   result.J = j
   result.Point_Structure = points;
   return result;

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

cwright's picture
Re: Structure in JS patch not working

the simple JS is simply wrong. You're referencing "result" without defining it.

try adding var result = new Object(); like the complex example.

sbn..'s picture
Re: Structure in JS patch not working

For errors such as this one, that the syntax-checker in the patch inspector doesn't catch, try looking at the console. You can do that with

There, you should see messages from QC JavaScript, something like:

Execution failed: Can't find variable "result"'s picture
Re: Structure in JS patch not working

Thanks cwright. I knew it was going to be a stupid mistake... 1am my time. I get so frustrated with JS patch and start thinking it's the flaky one (and I'm still not 100% convinced it isn't ;) ) not me.

I often resort to pulling some code out of a bigger code into it's own JS patch to try and get what seems like a simple thing to work. That's how result came to be undefined in this case.

Have you ever been suspicious of the JS patch. I've noticed if I leave the viewer running while I type code (risking system wide lock-up I know) the patch can get so very dissatisfied with anything I type, even if it's code that worked before. Re-selecting the patch in editor can sometimes help, I think.

I used to teach Adobe products and whenever students said "Ai/Ps/etc is buggy I can't…" and my favourite response was "I know the bugs and 95% of the time I hear 'I can't…' it's user error". Do I have to hear that about JS patch?

OS 10.5's picture
Re: Structure in JS patch not working

Hi sbn,

Thank you. I am actually a diagnosed phobic. I will attempt to overcome my fear now I have a big incentive to use it. Is there any kind of doc / tute about using it you know of? I'l check Apple developers site but I often get lost in there…

5/03/10 2:02:09 PM Quartz Composer[415] *** Message from <QCJavaScript = 0x0591B7F0 "JavaScript_1">:
Execution failed at time 11.765 
5/03/10 2:02:09 PM Quartz Composer[415] *** Message from <QCPatch = 0x09E4AC60 "(null)">:
Execution failed at time 11.765 
5/03/10 2:02:09 PM Quartz Composer[415] *** <RenderView: 0x6d488b0>: Render failed at time 11.764917s 

That JS generates this console msg. Not to sure on what I can read from that —probably nothing right?

toneburst's picture
Re: Structure in JS patch not working

It can be flaky. I've had crashes while using it, and like you, have experienced it choking on what seems to be perfectly good code. The vast majority of the time my JS code doesn't work though, it IS user-error.

a|x's picture
Re: Structure in JS patch not working

Thanks for the confirm, toneburst. On both counts ;)

When it goes off it seems to go off even if I cut and paste the code to another JS patch. Restarting QC doesn't necessarily help but I don't have any data to back that up feeling.

The other day I got up and retyped exactly the same code that was jamming the night before and it worked first Run. Maybe reboot is the answer when it's misbehaving??