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

Call other scripts from within a script, much like calling a class in java?

January 29 (11 years ago)

Edited January 29 (11 years ago)
DXWarlock
Sheet Author
API Scripter
This might be outside the realm of the API scripting. but is it possible to make scripts others call on, like the way you call methods in other class files?

For example instead of having to do the whole 'find player, find character sheet, find attribute on sheet' in every script that needs to pull an attribute, I could have a script of 'commonly used' items and call them and pass variables. like instead of having to do this 20 times in 10 scripts(a short one to keep it simple):

var oCharacter = getObj("character", obj.get("represents"));
var oA = findObjs({_type: "attribute",name: "STR",_characterid: oCharacter.id});

Having to do that every time to get one, I could have it on another script of common used methods called 'MyCalls' as 'GetAtt' and call it with like
"MyCalls.GetAtt(Bob,STR); 
or such.
anyone know if this is possible?

January 29 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Yes.... just make a function that does whatever you need.
January 29 (11 years ago)

Edited January 29 (11 years ago)
DXWarlock
Sheet Author
API Scripter
how do you call the other script without doing at sendChat !command type thing? And then return the results to the other script?
I tried
myFunction(argument1,argument2)
then a script full of these types:

function myFunction(var1,var2)
{
some code
}
but it did nothing when I called it from another script. I can get it to do whatever that call does, but not pass anything back.
January 29 (11 years ago)

Edited January 29 (11 years ago)
DXWarlock
Sheet Author
API Scripter
For example I tested this

Got this as a function in a script by itself:

function myWho(who){
findObjs({_type: 'character',name: who})[0];
}

and sent this:

var test = myWho(msg.who);
log(test);

And it returns an 'undefined' on log for test.
But if I test it like this is one script:

var test = findObjs({_type: 'character',name: msg.who})[0];
log(test);

it works.
January 29 (11 years ago)
You don't need to call the other script explicitly. All the scripts you load into the API exist in the same namespace and they are loaded in the order their tabs appear. If you create myFunction in Script1 you can call it from Script2. If you'd like to encapsulate the functions into an object, you'll need to look into Javascript's prototypal object oriented features. To reuse your example:

var MyCalls = {};
MyCalls.getAttr = function(name, charid) {
/* function goes here */ }