Forums > Javascript

Error with code in jsextensions

July 26, 2013 | 4:49 pm

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?


July 28, 2013 | 2:06 pm

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.


July 29, 2013 | 7:25 am

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?


July 29, 2013 | 3:47 pm

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?


July 29, 2013 | 4:03 pm

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();

}


July 30, 2013 | 3:04 pm

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?


July 30, 2013 | 3:38 pm

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?


July 30, 2013 | 4:25 pm

Okay, here you go.

Attachments:
  1. jstest.zip

July 30, 2013 | 5:33 pm

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";
}

August 5, 2013 | 7:41 am

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"?


August 5, 2013 | 12:43 pm

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.


August 5, 2013 | 4:10 pm

Thanks Joshua for your insight. It is working much better now.


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