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?
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?
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]
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 ["",""]; } } }