Error with code in jsextensions
I moved some js code into a library and put it in jsextensions so that I can reuse it. But when I access one of the functions in this library I get following error...
js: processinglib.js: Javascript ReferenceError: outlet is not defined, line 115
Why would outlet not be defined? Is it not a global method?
Are you calling it when the object is instantiated? The outlets might not have had time to be created - try deferring the function call, moving it from your global code, or setting it up in a loadbang() function. It's hard to tell if the cause is anything else without seeing your code.
Yes, I am calling the function after the patcher and objects are instantiated. I am triggering it manually. Are you saying that when Max starts up, it loads the files in jsextensions and outlet is undefined? According to Max documentation, outlet is a method within the jsthis object. Does the jsthis object not exist at startup? Why would it not find it by the time I call the function?
I meant that you couldn't have something like
outlet(0,"hello")
by itself in your code if it's not part of a function. Try loading just that line in a [js] object and re-open the file - you'll get an error message because the outlet doesn't exist in the global code. Do you have a snippet of your code that replicates the issue so I can take a closer look?
This is the function that is causing the error. It is loaded in a file that is in jsextensions.
function drawProcessingScene()
{
var theImage = null;
//jsthis.draw();
mgfx.identity_matrix();
theImage = new Image(mgfx.pop_group());
mgfx.image_surface_draw(theImage);
theImage.tonamedmatrix(outmatrix.name);
outlet(0, "jit_matrix", outmatrix.name);
frameCount++;
gc();
}
Okay I simplified it even more. If you put this function in a file in jsextensions, it will error out.
function outletRelay(msg)
{
outlet(0, msg);
}
Could this be some problem with the jsthis object? Perhaps it does not exist when source in the jsextensions directory is loaded?
How and when are you calling it from your main patch? Could you ZIP up all the files and attach them here so I can take a run exactly what you're running?
Hmmmm, I tried passing the scope across with no luck (perhaps someone knows more about this than me), otherwise my suggestion would be including your patcher-interfacing code in the main js and any utility functions in the jesextensions, for example:
function bang() {
outlet(0,outletRelay("test message"));
}
and
function outletRelay(msg) {
return msg + "123";
}
I really feel this is an issue that needs clarification from someone at Cycling. What is the expectation of what I can access from code that is loaded in jsextentions? Is the jsthis object instantiated at that time? Why can I not access the function "outlet"?
Your functions in jsextensions are compiled as global code outside the concept of the js "this" object. When you call outlet() an an ordinary js instance it is calling this.outlet(), but in the jsextensions code there is no similar this. You will need to change your functions to something like the following where you pass in your js "this" object to the function in order to be able to call the outlet functions of that object.
function doit(myObject)
{
myObject.outlet(0,"bang");
}
Hope this helps.
Thanks Joshua for your insight. It is working much better now.