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

[Script] Choice: A Minimalist Dynamic Table Roller

I wanted to make a quick macro to pick a random compass direction for each doorway in a 3x3 room grid and realized there wasn't an easy way to pick a random value from a list without creating a rollable table. I decided that instead of making 9 different rollable tables I'd write a minimal script to provide a random choice from a supplied list of options: on('chat:message', function(msg) {     if (msg.type !== "api" ) {         return;     }     var args = msg.content.split(" ");     var command = args.shift();     switch(command) {         case '!choice':             var myList = [];             var i;             for (i = 0; i < args.length; i++) {                  myList.push(args[i]);             }                          sendChat('Choice', '/w '+msg.who+' '+myList[randomInteger(myList.length)-1]);     } }); For example saying this:     !choice N S E W Will result in something like this response:     (From Choice): (GM) E Not really a ground-breaking or original concept, but I figured I'd share.
1567617440
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Fun! If you made something other than a space character the splitter, you could even put in whole phrases to choose between.
1567618399

Edited 1567633990
The Aaron
Roll20 Production Team
API Scripter
Nice! You can simplify the array part from:             var myList = [];             var i;             for (i = 0; i < args.length; i++) {                  myList.push(args[i]);             } To: let myList = args.slice(1); Edit: Good point by Scott below:
1567619327
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I'd actually ask, why are you making the myList array? once you shift the command out of the arg array, it's already what you want, so you could just do this: on('chat:message', function(msg) {     if (msg.type !== "api" ) {         return;     }     var args = msg.content.split(" ");     var command = args.shift();     switch(command) {         case '!choice':             sendChat('Choice', '/w '+msg.who+' '+args[randomInteger(args.length)-1]);     } }); Also, just to make sure you're aware, the randomInteger function does not replicate the quantum roll functionality of a normal dice roll on Roll20. Instead it replicates the back up system, which is still "more" random than the standard javascript randomInteger function. If you want to utilize quantumRoll, you'll need to utilize sendChat and it's callback functionality to do so.
1567624228
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
It's probably random enough to appear random. Which is all anyone really needs. :)
1567626672
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Oh, for sure, but just putting it out there for clarity since the way the wiki is worded can make it seem like quantumroll and randomInteger are the same.
1567634134
The Aaron
Roll20 Production Team
API Scripter
If I remember correctly, the only difference is where the seed value for the PRNG function comes from.  Quantum Roll is seeded by the fluctuations of light particles, and the API from ???  (I though Riley said it gets seeded by Quantum Roll on startup, but I could be misremembering.)