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

Make script referable

Hi,  There is probably something very simple I am missing, but how to I create a script so I can that I can refer to functions within it from other scripts? Many thanks
1490479467
The Aaron
Pro
API Scripter
So, the simplest thing you can do is a script like this: var myFunc=function(){ }; then call like this: myFunc(); better would be to put them in an object, effectively namespacing them: var MyLibrary = { myFunc: function(){ }, myOtherFunc(){ } }; call like this: MyLibrary.myOtherFunc(); better still is to use the Revealing Module Patern: &nbsp; <a href="https://wiki.roll20.net/API:Cookbook#Revealing_Mod" rel="nofollow">https://wiki.roll20.net/API:Cookbook#Revealing_Mod</a>... If you want to see that in practice, look at pretty much any one of my full scripts. TurnMarker1 calls a function in GroupInitiative, just search for GroupInitiative in the code.&nbsp; Let me know if you need any help or code review.&nbsp;
Cool, thank you again Aaron, i will take a look in the morning
Sorry Aaron, i am probably missing something basic here: if(curValue &lt;= 0 && deadNPC === true && type == 'Monster') { obj.set("status_dead", true); SetAuraNone(obj); if(state.HealthColors.auraDeadFX !== "None") PlayDeath(state.HealthColors.auraDeadFX); jonsxp.addxptopeeps(obj); } should call the function from my script: var jonsxp = jonsxp || (function(){ "use strict"; var addxptopeeps = function(obj) { var xptogive = xptool.GetNPCXP(obj.id); xptool.AddXPToPool(xptogive); } }()); but i receive&nbsp;this error when it is called: TypeError: Cannot read property 'addxptopeeps' of undefined TypeError: Cannot read property 'addxptopeeps' of undefined am i missing soemthing&nbsp;silly?
1490550291
The Aaron
Pro
API Scripter
With the Revealing Module Pattern, you need to return the objet that will be publicly visible. &nbsp;In your example, you just need to add that part (bolded below): var jonsxp = jonsxp || (function(){ "use strict"; var addxptopeeps = function(obj) { var xptogive = xptool.GetNPCXP(obj.id); xptool.AddXPToPool(xptogive); }; return { addxptopeeps: addxptopeeps }; }()); A few things to note: Your function inside the jonsxp closure could be named anything you want, say xpAddHandler. &nbsp;The name it will have to the public is the key in the returned object: { addxptopeeps: xpAddHandler } . &nbsp;This is important as it allows you to have names inside your closure that make sense to the context of your module, and names in your public interface that make sense to clients of it. In this simple example, you may feel like there is a great deal of overhead for something no more complicated than jonsxp = { addxptopeeps: function(obj) { /* ... */} }; and you'd be right. &nbsp;The real power in using the Revealing Module Pattern is in how you can decompose your function into many different sub-functions and operations on internal state without exposing them to being manipulated incorrectly from outside the module. Hope that helps!
That is perfect, thank you so very much. with the return statement, am i right the first is the local name and the second is the public name or is it the other way round?
1490571864

Edited 1490581867
PaprikaCC
API Scripter
The first is the public name. The second is the local name. At its most basic, the return statement in a script built with a Revealing Module pattern does this. return { key: 'value', addxptopeeps: addxptopeeps, }; log(jonsxp); // { key: value, addxptopeeps: [Function: addxptopeeps] } log(jonsxp.key); // 'value' log(jonsxp.addxptopeeps()); // calls whatever is in addxptopeeps, which is currently a function. EDIT: Forgot a () after addxptopeeps
1490578594
The Aaron
Pro
API Scripter
Very minor correction: log(jonsxp.addxptopeeps()); // calls whatever is in addxptopeeps, which is currently a function. need to have the () to execute it, otherwise it is just referring to the functionnobject.&nbsp;
Thats&nbsp;perfect, thank you both so much