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

Completely lost trying to get API scripts to work

I've been trying to write a script to kill any unit my cursor is on and replace it with another token (a dead body) whenever I type !death1. For whatever reason I haven't been able to get much headway writing this, and I was wondering if anyone could at least point me in the right direction to start. I'm very new to these API scripts, and I've been having a lot of trouble figuring it out, so I'd appreciate it if someone could at least give me a place to start writing a script like that.
1469282338

Edited 1469282756
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
The API doesn't have any access to where your cursor is (at least I don't think so, there's lots of cool things you could do if it does). Try using  msg.selected or passing the token_id of the target token(s) via a chat command like !kill @{selected|token_id). The "!" tells the chat handler that it is an API message, and so won't be displayed in chat. The "kill" is for filtering out commands that are not for your API via if or switch statements in your code. Alternatively, you may want to take a look at  rollable table tokens , as that would be a pretty easy way to set this up as well (although, you would admittedly have a ton of tables).
1469415941
Gold
Forum Champion
Unfortunately I don't know of a good way to replace it with a DeadBody.PNG other than the Rollable Table Tokens suggestion that Scott said. Here is what I do for visually marking the kill of a dead troop, (using the existing Token-Mod API, using built-in Roll20 FX, plus the giant red X status marker, plus a Chat report, all rolled into a Macro). !token-mod --set bar2_value|0 statusmarkers|dead /desc It seems that @{selected|token_name} is defeated. /fx splatter-blood @{selected|token_id} @{selected|token_id} /fx glow-blood @{selected|token_id} @{selected|token_id} /fx bubbling-blood @{selected|token_id} @{selected|token_id}
1469457772

Edited 1469458950
The Aaron
Pro
API Scripter
If you're talking about actually writing a JavaScript API script, it is as simple as: on('ready',function(){     "use strict";     var getCleanImgsrc = function (imgsrc) {         var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)(.*)$/);         if(parts) {             return parts[1]+'thumb'+parts[3];         }         return;     },     deadImg=getCleanImgsrc(' <some img URL in a user library> ');     if(deadImg) {         on('chat:message',function(msg){           if('api'===msg.type && msg.content.match(/^!kill\b/) ){               _.chain(msg.selected)               .map(function(o){                   return getObj('graphic',o._id);               })               .reject(_.isUndefined)               .each(function (o) {                   o.set({imgsrc: deadImg});               });           }         });     } else {         sendChat('Kill Script','/w gm <div style="background:red;font-weight:bold;color:white;border:2px solid white; padding .3em;">Your deadImg url is not in a user library.</div>');     } }); I wrote this on my phone, so some gentle debugging might be necessary. =D Edit: Still might need some debugging, but I adjusted it again... =D Be sure to replace the highlighted part with your image url.
1469458079

Edited 1469458962
The Aaron
Pro
API Scripter
Note : your dead image needs to be in a user library and needs to be the thumb version. You can call this function on it to verify it is in the right place and correct it to the right type (thumb).