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

Scripting and Tables

So I've read through the wiki and I think I know how to access table and table objects via the API, but is there a simple way to just roll on a table then manipulate the results from there? Or did I miss something?
1637894317
David M.
Pro
API Scripter
I think I've seen from other recent posts that you are familiar with Scriptcards. You can assign a roll variable to a rollable table result using something like: --=ThisRoll|[T#TableName] Then you can access the result for later use by using: [$ThisRoll. tableEntryText]  and/or  [$ThisRoll. tableEntryImgURL ]
Sorry, I was unclear, David. I meant from the API writing in javascript. 
1637923449
Kurt J.
Pro
API Scripter
Here is the function I use in ScriptCards to roll on a rollable table. Pass it a table name and it will generate a roll on the table, taking the weighting of items in the table into account. It returns an array where the first item is the text of the entry and the second is the URL of the image. If the table doesn't exist, it will return an array with two empty strings. function rollOnRollableTable(tableName) { var theTable = findObjs({type: "rollabletable", name:tableName })[0]; if (theTable !== undefined) { var tableItems = findObjs({type: "tableitem", _rollabletableid: theTable.id}); if (tableItems !== undefined) { var rollResults = {}; var rollIndex = 0; var lastRollIndex = 0; var itemCount = 0; var maxRoll = 0; tableItems.forEach(function(item) { var thisWeight = parseInt(item.get("weight")); rollIndex += thisWeight; for (var x = lastRollIndex+1; x <= rollIndex; x++) { rollResults[x] = itemCount; } itemCount += 1; maxRoll += thisWeight; lastRollIndex += thisWeight; }); var tableRollResult = randomInteger(maxRoll); return [tableItems[rollResults[tableRollResult]].get("name"),tableItems[rollResults[tableRollResult]].get("avatar")]; } else { return ["",""]; } } }
Awesome! Thanks Kurt!