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

How to call a function in one script from another script?

1519839055

Edited 1519839119
GiGs
Pro
Sheet Author
API Scripter
I have managed to this before, by accident, but i can't repeat it when i actually want it to happen. I have two dummy scripts set up to try to figure out how to get this working. What am I missing? The two scripts are almost identical, and are listed below. The important part is the getFromSource function in the first script, which is trying to call a sourceFunction in the second script. Just calling the function by name doesnt work, nor does prepending the script 'namespace'. If I try let eureka = sourceFunction(); I get ReferenceError: sourceFunction is not defined If I try let eureka = sourceTest.sourceFunction(); I get TypeError: sourceTest.sourceFunction is not a function Do i have to do something to expose the function in someway? Maybe in the RegisterEventhandlers function? I include the complete scripts below, but they are basic scaffolding, just to test this process. Script One var originTest = originTest || (function() {     'use strict';     var version = '0.1',     handleInput = function(msg) {         var args = msg.content.split(",");                  if(msg.type == "api" && msg.content.indexOf("!originTest") !== -1) {             var args = msg.content.split("--");     getFromSource();         } else { return; }     },          getFromSource = function() { let eureka = sourceTest.sourceFunction();   // TypeError: sourceTest.sourceFunction is not a function //let eureka = sourceFunction(); // ReferenceError: sourceFunction is not defined     sendChat('originTest', eureka);              },          registerEventHandlers = function() {         on('chat:message', handleInput); };          return {     RegisterEventHandlers: registerEventHandlers };      }()); on("ready",function(){ 'use strict'; originTest.RegisterEventHandlers(); }); Script Two var sourceTest = sourceTest || (function() {     'use strict';     var version = '0.1',     handleInput = function(msg) { // this is a dummy script so doesnt need anything here     },          sourceFunction = function() {     return 'Eureka - we have landed!'; },          registerEventHandlers = function() {         on('chat:message', handleInput); };          return {     RegisterEventHandlers: registerEventHandlers };      }()); on("ready",function(){ 'use strict'; sourceTest.RegisterEventHandlers(); });
1519839521
GiGs
Pro
Sheet Author
API Scripter
Lol, naturally about a week after banging my head against this problem off-and-on, I discover the solution mere minutes after posting. For anyone else stumbling with this issue, the trick is in the return function. Add the name you want to refer to the function, and the name of the function, like so return { sourceFunction: sourceFunction,     RegisterEventHandlers: registerEventHandlers }; Then call it using the namespace:  sourceTest.sourceFunction
1519843465
The Aaron
Pro
API Scripter
Great! =D The only caveat I'd warn you about is be sure that you aren't trying to call it before the ready event occurs (in this case).  If one of them called the other like this: var originTest = originTest || (function() {     'use strict';     var version = '0.1', something = sourceTest.sourceFunction(),     handleInput = function(msg) { It could be undefined depending on the order the scripts are specified in the API.
1519844617
GiGs
Pro
Sheet Author
API Scripter
Thanks for the warning. The reason I was trying to figure this out was for an npc generator that will respond to chat commands, and then near the end of the script call a couple of generators from another script (for instance, a name generator, and random appearance features), so I think that should be okay.