this.patcher.connect() Not working on initial load?

Chris Wright's icon

I have a script where in global code it creates a number of boxes and connects them to the js object. When I first open the patch (saved with just the js object ready to create objects) each patchcord that should connect produces an error saying "newobj . patchcord inlet/outlet out of range: deleting patchcord".
If I then open the script and save it, it generates all the objects and connects them all as it should when it loads. Is there an order that im neglecting to consider in my code? The following is the simplest example of what I'm doing that for me doesn't generate patchcords on loading:

inlets = 1;
outlets = 1;

var me = this.box, p = this.patcher, loc = me.rect;

    p.remove(p.getnamed("number1"));
    p.remove(p.getnamed("number2"));

var numA = p.newdefault(loc[0], loc[1]+30, "number");
    numA.varname = "number1";
    p.connect(me, 0, numA, 0);
    
var numB = p.newdefault(loc[0], loc[1]-30, "number");
    numB.varname = "number2";
    p.connect(numB, 0, me, 0);

Thanks for any help you can provide :)

Lee's icon

try putting in loadbang() function

Chris Wright's icon

Sorry just realised, thank you! Last time I used loadbang it didnt seem to do anything but that was probably my coding skills or lack thereof but this has indeed fixed my problem.

Thanks again!

Lee's icon

js will start executing global code as it is parsed - this doesn't necessarily mean that the patcher loading it has completely initialised - for example, you won't be able to send messages to outlets until this happens, even though you've declared - I'm guessing that this will be the same when trying to access the patcher - when you recompile the js, the patcher env has already been set up so it will work - your js will be sent a loadbang() message when the patcher env has been setup...

Chris Wright's icon

Ah that makes sense. Sorry I edited the previous, didn't realise you'd replied. I thought it may have something to do with that.

Thank you :)

Lee's icon

cool :)

Chris Wright's icon

Would you happen to know if it's possible to then call the loadbang function when the file is saved or loaded after the patcher is opened? I've tried using save() and embedmessage("loadbang") but i couldn't get it to do anything, even just posting on load :/

Lee's icon

what are you referring to by "file" - the js file?

Chris Wright's icon

Yeah, so if I create a box js pointing to the javascript file, I want it to draw all the boxes and patchcords defined in the script automatically if that's possible.

Lee's icon

maybe this is what you're after?

autowatch=1

var global = new Global( "myglobal" );

if ( global.init )
    loadbang();

function loadbang() {
    global.init = true;
    
    post( "load\n" );
}

this should fire the loadbang() when the patch is loaded and then every time the file is saved...

Chris Wright's icon

That worked for a smaller test script that I tried with it but didn't seem to work as well with a larger script, although it works if you right click the js object and load the script from there. It still presents the same errors if instead a new object is created and 'js myScript' is entered.

Lee's icon

yes, that is assuming only 1 instance of the script - it uses the same global variable - the variable would have to be varied by instance

interesting that is affected by file size tho...

tbh, I just send an 'init' message into all my js objects to re-init them after recompile so never tried to do anything like this...

Chris Wright's icon

OK. I'll probably just stick with sending a loadbang to the patcher if I need to run the script after it's loaded. Thanks for your help!