Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Include Scripts & Sharing Scripts

As I use the awesome scripting functionality, I am finding a desprate need to refactor my scripts to share commonality.  So here are my questions Is there a way for two scripts within a campaign to share code, either through an include or something namespaced? Given that, is there plans for scripts to then be includable across campaigns, either publicly or privately by DM account?
1378236093

Edited 1378236131
Lithl
Pro
Sheet Author
API Scripter
API scripts already share code. If you've got multiple script "files", they're executed from left to right. (This is why script authors usually namespace their functions and global variables, to help avoid collisions.) I can't comment on future plans, though.
1378252725

Edited 1378253018
Hmm, retesting.  I have been able to reach a reference to hi().  Still a clumsy way to handle load order, left to right, but its better than nothing.  Thanks Brian
1378254031

Edited 1378254160
Lithl
Pro
Sheet Author
API Scripter
Just tested. Two scripts. Left ("Test 2"): function test2Func() { sendChat('', 'test2.js'); } var test2Var = 'test2'; on('ready', function() {   log('test2.js onReady begin');   try   {   log('calling test.js');   testFunc();   test2Var += testVar;   }   catch(e){}   log('test2.js onReady end'); }); Right ("Test"): function testFunc() { sendChat('', 'test.js'); } var testVar = 'test'; on('ready', function() {   log('test.js onReady begin');   try   {   log('calling test2.js');   test2Func();   testVar += test2Var;   }   catch(e) {}   log('test.js onReady complete'); }); Log output: "test2.js onReady begin" "calling test.js" "test2.js onReady end" "test.js onReady begin" "calling test2.js" "test.js onReady complete" Chat output: test.js test2.js I tried replicating your script as well, and it worked fine. Hell, because it's all functions involved, switching the order still worked, too. Edit: Was writing my post before you deleted your previous one. I'm actually kind of curious how you managed to get the reference error, since I didn't get one no matter how I arranged your same code.
Brian, I suspected after I sent the post that I have failed to save script 'A' before moving on to script 'B' in my example, explaining the failed reference since it wasn't a function in memory at the time of execution.  Thanks again for your post, I am relieved that I can refactor some of my code to reuse common functions.
1378354454
Lithl
Pro
Sheet Author
API Scripter
Happy to help!
1378359805
Alex L.
Pro
Sheet Author
If your building scripts for public use that have dependencies it would be an idea to use a bit of code like this in the on ready event // Requires community.command if(community == undefined || !("command" in community)) { log("You must have community.command installed in a script tab before the tab this script is in to use pigalot.requests.phrases."); throw "Can't find community.command!"; } This checks if the main namespace "community" exists and if it contains the sub namespace command.