PDA

View Full Version : Getting link references to module/DB entries



dsaraujo
January 12th, 2022, 01:10
Hi, I'm writing a Random Encounter Generator for PF2. I'm pretty much done with the code to calculate the budget and find appropriate monsters (I get a list of all creatures using DB.getChildrenGlobal("reference.npcdata"), let me know if this is a problem).

When I try to create a new Battle (Encounter) record, I'm trying to use NPCManager.addLinkToBattle(), but that requires to pass a sLinkRecord. Where can I find this link from the DB?

Trenloe
January 12th, 2022, 02:22
NPCManager.addLinkToBattle is a function that adds an existing encounter, random encounter or NPC record to an existing (or new) encounter (battle). You need to have created a new battle first, or have an existing one you want to add to.

The function is defined as: function addLinkToBattle(nodeBattle, sLinkClass, sLinkRecord, nMult)
nodeBattle = the node for the new/existing encounter that you want to add to.
sLinkClass = battle (an existing encounter), battlerandom (a random encounter - the randomness of the encounter will be calculated when adding) or npc for an individual bestiary record.
sLinkRecord = the full database path (as a string) to the battle, battlerandom or npc record.
nMult = the number of NPCs in the entry in the encounter.

Looking at the definition for DB.getChildrenGlobal here: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644582/DB#getChildrenGlobal
It can be seen that the data returned is "The child nodes are returned as databasenode objects in a table with keys corresponding to the names of the child nodes. If the database node does not exist, an empty table is returned." Each row in the LUA take is a key, value - with the key being the database node as a string and the value being a database node object (https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644722/databasenode). Use the key value for the relevant record as sLinkRecord when you call NPCManager.addLinkToBattle.

Hope this helps.

dsaraujo
January 12th, 2022, 15:58
As usual, that nails, thanks Trenloe. All that I need are the key values. Hopefully I will have a functional version by the end of the day!

Trenloe
January 12th, 2022, 16:19
If you just have the target record databasenode you can use .getNodeName: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644722/databasenode#getNodeName

dsaraujo
January 12th, 2022, 16:28
Edit: getNodeName() gave me the reference full path!

Unfortunately the keys are just numeric values and not the reference link.
So something like


local creatures = DB.getChildrenGlobal("reference.npcdata");
local nCreatures = 1;
-- Filter for only level appropriate creatures
for key, localNPC in pairs(creatures) do
local nNPCLevel = DB.getValue(localNPC, "level");
if isCreatureInRange(nNPCLevel, nPartyLevel, nBudgetSize) then
encounterList[nCreatures] = localNPC;
encounterKeys[nCreatures] = key;
print(key);
nCreatures = nCreatures + 1;
end
end

Would only print numbers.

Trenloe
January 12th, 2022, 16:58
That seems to be potentially a bug in the FGU API.

In that case use localNPC.getNodeName: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644722/databasenode#getNodeName