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 .
×

Storing a Dice Roll as a Variable

1396996389

Edited 1396996425
Hello! I'm new to the idea of macros and API's, and I wanted to know if there is a way to store a roll as a variable within an ability so that I can store that information for later use. For example, say I want to compare one roll to different levels of success: Accuracy: [[ 1d100 ]] (make this roll equal to some variable X) Percent Regular Success: [[ floor(( X / @{Skill}) *100) ]] Percent Difficult Success: [[ floor(( X / (@{Skill} / 2)) *100) ]] Percent Extreme Success: [[ floor(( X / (@{Skill} / 5)) *100) ]]
Doing it without the API appears to be impossible. Doing it with the API is possible, but tricky. If you decide to get the mentorship in order to get the API, I can share with you some code that sort of does that. Probably wouldn't be too tough to modify it.
1397003454
Gen Kitty
Forum Champion
I'd be interested in that code, honestly :>
I don't foresee being a mentor anytime soon. However if you'd like to post your code here or something for future reference, that'd be great. Maybe another individual with mentorship might be interested.
1397006228

Edited 1397006269
Fair enough. :) It's in here . There's a lot of code in there, so the important steps are: 1. You have to actually roll the value through chat. I use the sender of that message as an easy way to catch it later, although you could possibly do it differently. For example, you might do this: sendChat("@accuracy", "[[1d100]]"); I also had a variable set up so that I knew to wait on other things until the result came in. If the steps listed above were going to be manual, you could skip this. But if you really wanted to code it all in one macro, you'd need to prevent the next line from executing until the result came, so something like this: macro_results["@accuracy"] = "WAITING"; (You'll need macro_results anyway to store and retrieve the results, you just might be able to skip setting it to "WAITING" at this point.) 2. Catch the result as a chat message: // Interrupts all chat messages to see if we've been waiting for them. on("chat:message", function (msg) { if (msg.who.indexOf("@") == 0) { // ... } }); and you can probably significantly simplify what I did from here and just fill in the macro_results["@accuracy"] with the real result, via 3. Parsing out the chat message: // There's totally a better way, but this gets the total from an inline roll. // It assumes that it's the only one on the line, but my code doesn't use this // unless it is anyway. (I was having trouble with the better way.) getInlineTotal = function(msg, i) { var total = ""; var roll = JSON.stringify(msg.inlinerolls); if (!isnull(roll)) { var pos = roll.lastIndexOf("total"); pos = roll.indexOf(":", pos); var pos2 = roll.indexOf(",", pos); total = roll.substring(pos + 1, pos2); } return total; }; isnull = function(o) { return typeof o == 'undefined' || o == null || o.length == 0; }; 4. If you're trying to automate all this, then have a function running on an interval that loops through macro_results and doesn't proceed until the result is != WAITING. Or put that code inside of the code that catches the chat, whichever is easiest for you. If you're running one macro after another manually, then just have any message with "@" look through your macro_results array and replace the values if it finds it. (Or use some flag that isn't already being used for something else in Roll20; probably that would be better) . Let me know if you have any questions! On another note, I'm seriously considering trying to update my script to handle this scenario, but probably not in the next few days.
That is some pretty neat code! Hopefully I'll learn more about JavaScript in the future. xD