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 with making simple Group Initiative script

1519233905

Edited 1519234310
I'm trying to create a script that would allow me to run a macro like the following; !AddInit ?{Initiative|0} at which point the script would add any selected tokens to the tracker at the value provided by the macro variable. I have the following code so far; on("chat:message", function(msg) {      if (msg.type == "api" && msg.content.indexOf("!AddInit") !== -1 && msg.who.indexOf("(GM)") !== -1) {           try{                _.each(msg.selected, function(selected) {                     ???                });           } catch(err){return;}      }; }); I've verified that this code will run an operation once for each selected token. What I need to figure out now are the following; a) How do I "read" the value following "!AddInit"? b) How do I add a selected token to the tracker? I've tried picking apart existing group initiative scripts, but everything I've found is too difficult for me to parse with the amount of experience I have with scripting for Roll20; I've run into a bunch of cases where something I thought would work ran into a bunch of snags. I thought I had it with a sendChat command until I found out &{tracker} and Macro Variables don't work within sendChat, for example.
1519241059

Edited 1519390984
The Aaron
Pro
API Scripter
a) How do I "read" the value following "!AddInit"? This is just string parsing, provided you don't have inline rolls.&nbsp; If you DO have inline rolls, I've got a function for you:&nbsp; &nbsp; <a href="https://wiki.roll20.net/API:Cookbook#processInline" rel="nofollow">https://wiki.roll20.net/API:Cookbook#processInline</a>... Just copy that over and run it on msg.content to put the values in for the inline rolls (really, you could just do that and then you'd transparently support it if you needed it later). msg.content will contain what was in the chat box after expanding any roll queries or attribute references (try doing log(msg.content) to see what it looks like).&nbsp; So in the above case, it might look like: !AddInit 17 If you'd been prompted for an Initiative, and then typed in 17. There are lots of ways of parsing that, I find it easiest to break it into an array and just access the elements, so you might do: let args = msg.content.split(/\s+/); let initNumber = args[1]; b) How do I add a selected token to the tracker? The turn order information is stored in a JSON string on the Campaign object.&nbsp; The string can be empty as well.&nbsp; You can turn it into a structure you can work with using JSON.parse().&nbsp; To get an array of the turns, you can use this code: let turnOrderEntries=JSON.parse(Campaign().get('turnorder'))||[]; Next, you need to inject the tokens you have selected into the turn order object.&nbsp; Easiest is putting them on the end: turnOrderEntries.push({ id: token.id, pr: initNumber, pageid: token.get('pageid') }); Then you can turn that structure back into a string and set it on the Campaign object (I'm also sorting it descending here): Campaign().set({ turnorder: JSON.stringify( &nbsp; _.sortBy(turnOrderEntries,(o)=&gt;-o.pr) ) }); Hope that helps!
1519314983

Edited 1519315930
Thanks, that was very helpful and got me all through question a very quickly. I'm still hitting snags on question b, though; nothing I do seems to be making anything happen. This is what I have right now, using some of the code you provided and examples from the wiki; on("chat:message", function(msg) {&nbsp; &nbsp; &nbsp; if (msg.type == "api" && msg.content.indexOf("!AddInit") !== -1 && msg.who.indexOf("(GM)") !== -1) { &nbsp; &nbsp; &nbsp; &nbsp; try{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let args = msg.content.split(" "); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let initNumber = args[1]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let turnOrderEntries=JSON.parse(Campaign().get("turnorder"))||[]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_.each(msg.selected, function(selected) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turnOrderEntries.push({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: selected._id, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pr: initNumber, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageid: selected._pageid &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Campaign().set("turnorder", JSON.stringify(_.sortBy0(turnOrderEntries,(o)=&gt;-o.pr))); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch(err){return;} &nbsp; &nbsp; &nbsp;}; }); I'm not getting any errors with this, but as I said nothing really happens. (I verified with a sendChat line earlier that initNumber is getting set properly, however.)
1519316554
GiGs
Pro
Sheet Author
API Scripter
I'd remove the try / catch block, or at least put a "sendChat('Error',err); inside the catch block. As it is your try block is hiding information from you - if you have an error, the macro will fail silently giving no clue what's wrong. having the sandbox crash during testing is a good thing - it often gives you information to help track down the issue. i do notice a typo: you have SortBy0( instead of&nbsp;r SortBy(&nbsp; (a 0 there shouldnt be there). I dont think that's causing your issue though.
Actually, I think that 0 might have been it, because after removing that and the try/catch it seems to be working! Thanks, both of you!
1519320666
The Aaron
Pro
API Scripter
Great!