Bidmaron
January 12th, 2019, 06:01
I am working on some PF2 stuff, and I need to be able to delete a PC from the CT using code. To my surprise, there is not a Combat Manager call to remove an entity from the CT, although there is code to add an entity. There is, however, code to intercept the deletion of a node from the list in the database node that constitutes the CT entities. The relevant code I have gathered below:(or maybe I am still missing some relevant code somewhere?)
function onDesktopInit()
DB.addHandler(CT_COMBATANT_PATH, "onDelete", onDeleteCombatantEvent);
...
DB.addHandler("charsheet.*", "onDelete", onCharDelete);
end
local aCustomDeleteCombatantHandlers = {};
function setCustomDeleteCombatantHandler(f)
table.insert(aCustomDeleteCombatantHandlers, f);
end
function removeCustomDeleteCombatantHandler(f)
for kCustomDelete,fCustomDelete in ipairs(aCustomDeleteCombatantHandlers) do
if fCustomDelete == f then
table.remove(aCustomDeleteCombatantHandlers, kCustomDelete);
return true;
end
end
return false;
end
function onDeleteCombatantEvent(nodeCT)
for _,fCustomDelete in ipairs(aCustomDeleteCombatantHandlers) do
if fCustomDelete(nodeCT) then
return true;
end
end
return false;
end
function onCharDelete(nodeChar)
local nodeCT = getCTFromNode(nodeChar);
if nodeCT then
DB.setValue(nodeCT, "link", "windowreference", "npc", "");
end
end
This raised some questions about how the handlers work. I searched for where in the CoreRPG there might be a call to DB.deleteNode, which I had believed was the only thing that would invoke the onDelete handler. I found only 4 instances, and they were all in the item manager. So then that got me to thinking that if you delete a node, it must always invoke two events:
1.onDelete for the deleted node.
2.(if there is a parent node)onChildDeleted for the parent of the node.
I figured that had to be the case because something somewhere had to fire the onDelete handler. So, a few follow-on questions ensue:
1. What is the order that those two delete events/handlers fire?
2. In the code above that traps the onDelete event, it is interesting that it is looking for a registered function that returns a true value, in which case it doesn't call any further registered functions. What happens when that function result gets back to the FG engine. Does returning false override the deletion of the node? If the function result matters on any of the handlers then the API docs needs to be changed to reflect the guidance for handler function results.
Finally, what I'd really like to know, aside from these academic questions, is what is the recommended method to remove a CT participant from code when the need arises to do so? Should I just go delete the combattracker.list child and somewhere I couldn't find (perhaps some code buried in an xml script) the CT interface will get magically updated?
function onDesktopInit()
DB.addHandler(CT_COMBATANT_PATH, "onDelete", onDeleteCombatantEvent);
...
DB.addHandler("charsheet.*", "onDelete", onCharDelete);
end
local aCustomDeleteCombatantHandlers = {};
function setCustomDeleteCombatantHandler(f)
table.insert(aCustomDeleteCombatantHandlers, f);
end
function removeCustomDeleteCombatantHandler(f)
for kCustomDelete,fCustomDelete in ipairs(aCustomDeleteCombatantHandlers) do
if fCustomDelete == f then
table.remove(aCustomDeleteCombatantHandlers, kCustomDelete);
return true;
end
end
return false;
end
function onDeleteCombatantEvent(nodeCT)
for _,fCustomDelete in ipairs(aCustomDeleteCombatantHandlers) do
if fCustomDelete(nodeCT) then
return true;
end
end
return false;
end
function onCharDelete(nodeChar)
local nodeCT = getCTFromNode(nodeChar);
if nodeCT then
DB.setValue(nodeCT, "link", "windowreference", "npc", "");
end
end
This raised some questions about how the handlers work. I searched for where in the CoreRPG there might be a call to DB.deleteNode, which I had believed was the only thing that would invoke the onDelete handler. I found only 4 instances, and they were all in the item manager. So then that got me to thinking that if you delete a node, it must always invoke two events:
1.onDelete for the deleted node.
2.(if there is a parent node)onChildDeleted for the parent of the node.
I figured that had to be the case because something somewhere had to fire the onDelete handler. So, a few follow-on questions ensue:
1. What is the order that those two delete events/handlers fire?
2. In the code above that traps the onDelete event, it is interesting that it is looking for a registered function that returns a true value, in which case it doesn't call any further registered functions. What happens when that function result gets back to the FG engine. Does returning false override the deletion of the node? If the function result matters on any of the handlers then the API docs needs to be changed to reflect the guidance for handler function results.
Finally, what I'd really like to know, aside from these academic questions, is what is the recommended method to remove a CT participant from code when the need arises to do so? Should I just go delete the combattracker.list child and somewhere I couldn't find (perhaps some code buried in an xml script) the CT interface will get magically updated?