PDA

View Full Version : updating a total based off a list getting deleted



kalannar
July 23rd, 2014, 21:13
I am creating an extension to keep track of player kingdoms. Each kingdom can have a settlement, and when they add values to society i need to update the kingdom value of fame. That works correctly, the only problem i run into is that when i delete a settlement, it calls the update on fame but i guess it happens before the database removes the node because it still thinks the settlement node is there and updates fame based off it. Is there away to tell of a node is marked to be deleted? Or a different way I should be doing this? Here is the code i am using to update fame:



function onInit()
update();
local node = window.getDatabaseNode();
DB.addHandler(DB.getPath(node, "settlementlist.*.society.total"), "onUpdate", update);
DB.addHandler(DB.getPath(node, "settlementlist.*.society.total"), "onAdd", update);
DB.addHandler(DB.getPath(node, "settlementlist.*.society.total"), "onDelete", update);
end

function onClose()
local node = window.getDatabaseNode();
DB.removeHandler(DB.getPath(node, "settlementlist.*.society.total"), "onUpdate", update);
DB.addHandler(DB.getPath(node, "settlementlist.*.society.total"), "onAdd", update);
DB.addHandler(DB.getPath(node, "settlementlist.*.society.total"), "onDelete", update);
end

function update()
Debug.chat("Called Update on fame society");
local node = window.getDatabaseNode();
local total= 0;
for _,v in pairs(DB.getChildren(node, "settlementlist")) do
Debug.chat(v);
Debug.chat(DB.getValue(v,"society.total",0));
total = total + DB.getValue(v,"society.total",0);
end
if total == 0 then
total = 0
else
total = math.floor(total/10);
end
setValue(total);
end

Trenloe
July 23rd, 2014, 21:32
I think the onDelete event is ran while the database node is still present - as the event includes the databasenode being removed: https://www.fantasygrounds.com/refdoc/databasenode.xcp#onDelete

You might want to write a separate delete() function specifically for the onDelete event and read the databasenode being deleted, get the society.total value for that node and remove this from the current total.

Moon Wizard
July 23rd, 2014, 21:47
You can also use the the DB.addHandler with an "onChildDeleted" event tied to the parent node. This is called after the node is deleted.

Regards,
JPG

kalannar
July 24th, 2014, 15:55
The onChildDeleted event worked like a charm. Thank you guys for all your help.