PDA

View Full Version : random table entry



jkeller
October 5th, 2025, 14:56
I'm trying to create an extension that needs to get a random entry from a given table (not a LUA table, an FG table).

I don't want to do an actual (asynchronous) dice roll, I just want a random entry. I assume it will be something like this, along with a math.random call, but I can't find any docs or examples explaining what to do.

local aDice, nMod = TableManager.getTableDice(nameTable);
local nRollResults = DiceManager.evalDice(aDice, nMod);
local aTableResults = TableManager.getResults(nameTable, nRollResults, 1);

Am I on the right track? Can someone point me to an example?

Thanks!

jkeller
October 5th, 2025, 21:49
I'm getting closer.

local myTable = TableManager.findTable("Some Table");
local rows = DB.getChildList(myTable, "tablerows");
local nRandomRow = math.random(#rows);
local tableRow = TableManager.getResults(myTable, nRandomRow, 1);
local value = tostring(tableRow[1].sText);

The random row looks good, but for some reason getResults always seems to return the first row only. (The hard-coded 1 refers to the column; there's only 1 column in the table.)

LordEntrails
October 6th, 2025, 00:43
5E has the ability to add a critical hit and critical fumble tables. From what I remember (years ago) those tables are rolled on in a manner you are looking for. No idea where in the code this is done, but you can find more info about the option here:
fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996642014/5E+Campaign+Options#House-Rules-(GM)

jkeller
October 6th, 2025, 01:29
Thanks! I found it in the manager_action_attack.lua.

I *could* use that, but unfortunately it does the visible dice roll, which is asynchronous. It's great for forcing a roll that you want to be displayed, but it's not really what I want in this case.

ActionAttack.notifyApplyHRFC("Critical Hit");

jkeller
October 6th, 2025, 17:13
I got it working. I don't fully understand the code, so some of the variable names may be misleading, but this function will return a random value from a given (1-column) table:



function getRandomTableValue(someTable)
-- gather the values
local values = {}
local rows = DB.getChildList(someTable, "tablerows");
for _,row in ipairs(rows) do
local nodeResults = DB.getChild(row, "results");
local aChildren = DB.getChildren(nodeResults);
for _,v in pairs(aChildren) do
table.insert(values, StringManager.trim(DB.getValue(v, "result", "")))
end
end

local nRandomRow = math.random(#rows);
if nRandomRow <= #values then
return values[nRandomRow];
end
return "";
end

RosenMcStern
October 7th, 2025, 09:15
DiceManager.evalDiceString(s) emulates a dice roll without displaying any dice. You could use that, too. Especially if the table uses a standard die.

DiceManager also has conversion functions to obtain a string from dice value, in case your roll is in die format and not a string expression like "2d6+2".

jkeller
October 7th, 2025, 13:47
Good to know, thanks!

It's a simple/flat roll on a table, but I don't know how many rows are in the table, and there may be any number. So, math.random should be fine I think.