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

Can an API script call another API script?

August 01 (11 years ago)
As per the title. Is there a way from within an API to execute another API?

Meaning from the table top, I run !command, then this API script runs !sub-command.

August 01 (11 years ago)
The Aaron
Roll20 Production Team
API Scripter
Yes!

You can look at my isGM() function / module and the CharDup script on line 53 where it is calling it for an example. Also line 167 where it checks to make sure the other script is present.
August 01 (11 years ago)
Awesome, thank! Now I need to tinker to try and make this work.
September 06 (10 years ago)
Sorry to bring this back up... having trouble making this work. I am not trying to call a function in another script, I am trying to execute the entire script. Again I have !command being processed and withing !command if a certain flag is set I want to execute another entire script !generatemacros.

I have seen many examples online of calling functions from other script, but not executing the full script. My use case is that I am currently running my Hero Lab importer, this is doing nothing more than populating the sheet. I then created a script that would populate base macros, what I am trying to do is automate it so that I can import and setup base macros in one command by executing two different scripts.
September 06 (10 years ago)

Edited September 06 (10 years ago)
DXWarlock
Sheet Author
API Scripter
Why not make the 'generatemacros' script a function, and have !generatemacros have one entry of calling that function, and your other script call that function when its needed also.

How I do all mine, since I have a lot of things I was doing over and over in scripts. For example I have a !whisperGM that people can send me formatted messages, but ended up needing it in sending blind rolls, loot rolls, etc..so I change it from all the code inside the !whisperGM, to calling a function that I moved all the code into. so everything else could access it also.

there was a thread about this a while back with the same question. I believe someone pointed out a way to do it, just everyone was also pointing out how its bad bad practice to do so...but I cant find it for the life of me to link to help you out. :\
September 06 (10 years ago)
The problem is I am very inexperienced... so making the entire script a function is something I guess I don't understand. Basically like I said I am importing monsters from an xml file and if an npc flag is set, I want to automatically add a bunch of default macros (which I already have a script that does this)... then as I continue to process the file I will add special abilities and things as I come across them.

September 06 (10 years ago)

Edited September 06 (10 years ago)
DXWarlock
Sheet Author
API Scripter
Dont feel bad, I was a javascript noobie too when coming here (Well I still am, but a "bash on it until it works with a vague idea of what I'm doing" noobie now). I know enough to frankenstien a script together to do what I want..someone like Aaron would look at my scripts and cry I think...but they work..lol
If you post the script for the generatemacros we/someone would be happy to help Im sure.

Converting it to a function might be a bit of a hassle, but trust me in the long run its good practice. I thought the same thing to start, and spent more time trying to not use functions and link commands than it would have been just to convert them, once I was 5-6 scripts in. and WAY easier to hook into things you was doing before later down the line on your new scripts.
September 06 (10 years ago)
Ok, well here we go... be gentle :)

https://github.com/beeke01/PSMacro/blob/master/PSM...

The above script is creating a blob of macros (yes, I know tons...) that are building Powercard macros. Basically I use this to populate a new sheet, then people can select the macros they use and/or rename/move them around. It's definitely not the most efficient script and method for doing this... but as you said... it works.
September 06 (10 years ago)

Edited September 06 (10 years ago)
DXWarlock
Sheet Author
API Scripter
This should be right I believe (backup your other first, Ive been known to break scripts without even trying)

The only things I saw that needed to be passed was character name and GMonly --1 and --2..correct?
So to call the function it would be MakeMacros(Character, GMonly);

Or whatever variables you name the 'Character' and 'GMOnly' in your other script also..

https://gist.github.com/anonymous/041d04cb04a894c5...
September 06 (10 years ago)

Edited September 06 (10 years ago)
William, thank you, I will test that out shortly! *edit* And yes, you are correct, the only two parameters are the Character and GMOnly flag.

Now that the primary "meat" of the script is indeed a function, could you provide an example of how I would call that function in another script?
September 06 (10 years ago)

Edited September 06 (10 years ago)
DXWarlock
Sheet Author
API Scripter
No problem, like I said I THINK i got all the things it needs being passed to it.

To use it its just define the 2 variables for character and GMonly..and send it as MakeMacros(var1,var2).

Like below cWho is a Character object, and GM = the true/false flag.
(this example doesn't check if character is a real character or player, and false is hard coded and such..was trying to make as simple one as possible).

Long as you pass a character object, and a true/false to it its all it needs. How you get them before calling it is up to your preference.

on("chat:message", function (msg) {
	if(msg.type != "api") return;
	var msgTxt = msg.content;
	if(msgTxt.indexOf("!command") !== -1) {
		var cWho = findObjs({_type: 'character',name: msg.who});
		var GM = false;
		MakeMacros(cWho, GM);
	};
});
September 06 (10 years ago)
Ok, so if I am clear on it, if I am already processing a command (!import in my case), and I already have the Character ID and GMOnly flag, then it should be a simple matter of doing the following at the proper time int he script:

MakeMacros(character.id, GM)

Correct?
September 06 (10 years ago)

Edited September 06 (10 years ago)
DXWarlock
Sheet Author
API Scripter
You got it :)

You might want to make it consistent so above change:
var Character = findObjs({_type: "character",name: CharacterName});
to
var Character = findObjs({_type: "character",name: CharacterName})[0];

and:
Character = Char[0].id;
to
Character = Char.id;

Just so it works the same all the time. Because now the way you had it its expecting it to be something it needs to get [0] of all the time. In later scripts you might be sending the Character already '[0]'d out.

You don't want one call passing a list of characters and function expecting that to pull first one..and another working with one already sorted out of the list..simpler to just send it a single character to start with, since thats what it needs in the end.

In simpler terms:Try to do all the sorting/converting and consistency of the variables needed outside the function before you call it, not inside it. So that way you know your always sending exactly what it needs before hand.

Not sure if thats good/correct practice..but it SURE makes debugging easier on you when your new and trying to figure out why its not working.
September 06 (10 years ago)
hmm... just trying to run the full script above as you have it, I am getting and error.

TypeError: object is not a function at MakeMacros (evalmachine.:74:2) at Sandbox. (evalmachine.:29:3) at eval (

Trying to puzzle out what is failing...
September 06 (10 years ago)
DXWarlock
Sheet Author
API Scripter
Because I was moving things around, I figured it out...:P

Move
//Variables
var AddAttribute = AddAttribute || {};
var AddAbility = AddAbility || {};


back to the very top of the script. (or anywhere outside the function really)

Like:
//Variables
var AddAttribute = AddAttribute || {};
var AddAbility = AddAbility || {};
//Main
on("chat:message", function (msg) {
.....
September 06 (10 years ago)
I was just about to come and post the same, thank you this was a very helpful learning experience!! Hopefully I can now integrate this into my other script and finally fully automate monster imports!

Thank you very much for your time and help!
September 06 (10 years ago)

Edited September 06 (10 years ago)
DXWarlock
Sheet Author
API Scripter
No problem :) helps me learn too.
Most my learning so far has been by breaking something, cursing at it, squinting at it, sighing, searching google like mad, then 3 hours later have Aaron or someone help me realize what I did wrong..haha
September 07 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
=D

William, I totally wouldn't weep at your code. I work in software as a career and most of the people I work with write much worse code! =D (though I might sigh dejectedly on occasion!)

I highly recommend Javascript: The Good Parts by Douglas Crockford if you want to sharpen you Javascript game. It's a fast read, well written and very handy! My code looked pretty much just like the above before I ready it (seriously, go look at my Mystara Calendar script if you don't believe me!).

Nice job and good luck, respectively!