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

[HELP] Using APIs to Call Other Scripts with Chat

So I'm trying to make a quick little script that will use a combination of other scripts to apply a set of damage attributes to a token. This requires that the selected token is the one that is affected. I spent some time trying to figure out how to grab the specific character ID from a selection, then I tried to simply use the chat output from the API to call other scripts like Custom Status Markers.

on("chat:message", function(msg) {
  if(msg.type == "api" && msg.content.indexOf("!damage ") !== -1) {
    var severity = msg.content.replace("!damage ", "");
    var sendingPlayer = getObj('player', msg.playerid);
    var d20Result = randomInteger(20); 
    var message = _.clone(msg),
			args,attr,amount,chr,token,who;
	
    log(message);
    severity = severity.toLowerCase();
    
	var bodyPart = "";
	var bodyTag = "";
	switch (d20Result) {
		case 1:
		case 2:
			bodyPart = "h-lleg";
			bodyTag = "leg";
			break; 
		case 3:
		case 4:
			bodyPart = "h-rleg";
			bodyTag = "leg";
			break; 
		case 5:
		case 6:
			bodyPart = "h-rarm";
			bodyTag = "arm";
			break; 
		case 7:
		case 8:
			bodyPart = "h-larm";
			bodyTag = "arm";
			break; 
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:
		case 17:
		case 18:
			bodyPart = "h-torso";
			bodyTag = "torso";
			break; 
		case 19:
		case 20:
			bodyPart = "h-head";
			bodyTag = "head";
			break; 
		default: 
			bodyPart = "h-other";
			bodyTag = "other";
	}
	var hcolor = "";
	var hvalue = 0;
	switch (severity) {
		case "minor":
			hcolor = "none"
			hvalue = "1"
			break; 
		case "major":
			hcolor = "yellow"
			hvalue = "2"
			break; 
		case "mortal":
			hcolor = "red"
			hvalue = "4"
			break; 
		default:
			hcolor = "none"
			hvalue = "0"
	}
	
	
	log("bodyPart: "+bodyPart+" hvalue:"+hvalue);
	log("bodyTag: "+bodyTag+" hcolor:"+hcolor);
	
	sendChat(msg.who,   "!CustomStatusMarkersSetMarker " + bodyTag); 
    sendChat(msg.who,   "!CustomStatusMarkersSetTintForMarker " + bodyTag + " " + hcolor);
    sendChat(msg.who,   "!setatt @{selected|token_id} " + bodyPart + " " + hvalue);
    
    sendChat(msg.who, severity + "  [[" + d20Result + "]]  ");
    sendChat(msg.who, bodyPart + "  [[" + hvalue + "]]  " + bodyTag + "  " + hcolor);
  }
});
The chat messages at the end are the commands I'm attempting to put through chat, however I get
"ERROR: Unable to find character selected in chat command."
in the API output console when I do. I'm wondering if there is a more direct solution.
March 22 (7 years ago)
The Aaron
Pro
API Scripter
Your script gets all the currently selected items in the msg.selected property. However, when calling API commands via sendChat, all information must be in the text of the message. The called scripts will not receive a msg.selected. Further, @{selected} and @{target} can’t be used. 

As long as the scripts you are calling can take token ids or character ids on the command line, you should be able to extract that information from your msg.selected and pass it on. 
March 22 (7 years ago)

Edited March 22 (7 years ago)
I remember hearing somewhere that the API scripts are loaded into the same instance of JS in the sandbox. One part I'm confused about though, is each script is confined to the scope of itself. Is there a way I can call the setMarker function from the customstatusmarkers API from the scope of my own? Passing it on, as you mentioned.

The only post I could find about this was here
https://app.roll20.net/forum/post/537282/calling-o...

And this seems to be using some form of processing chat commands that I don't really understand. (also 4 years ago)
March 22 (7 years ago)
The Aaron
Pro
API Scripter
All API scripts are concatenated together when the API server starts.  There's no difference between having 100 tabs with 1 script each, or 1 tab with 100 scripts. (other than how annoying it is to find everything in a single tab!)

They all exist in the same global scope, so if you define a variable in the global scope in one script you could access it in another.

If you control both scripts, the much better approach is to expose an interface from one that other scripts can use.  I do this via the Observer Pattern, which you can see in GroupInitiative, Bump, TokenMod, etc.  You could expose a function for other scripts to call (that's basically what I do for Observer).

If you don't control the other script and don't want to have a custom version of it, you can:
1) get in touch with the author and have them add an interface
2) send chat messages it will pick up.

When sending chat messages, you will have to have everything in the chat message, you can't rely on the msg.selected or other execution contexts (like the player that executed the command).
Stephen usually seems pretty easy to get a hold of, I'll message him about the customstatusmarkers script.
the setAtt one is a repurposed ammo script so I'll see about solving that one myself.
March 22 (7 years ago)
The Aaron
Pro
API Scripter
Sounds good. =D