PDA

View Full Version : Registering an event handler for a deleted list item



AmadanNaBriona
March 31st, 2020, 03:04
Another noob question.

I have a stat, adjDX, which is normally equal to DX.

adjDX is reduced by wearing armor. Every piece of armor has a "dexpen" attribute. So basically, I want:

1. For adjDX to default to DX, and change when DX is changed.
2. For adjDX to be adjusted when armor is added to the character.
3. For adjDX to be adjusted when armor is removed from the character.

I have 1 and 2 working, but not 3.

The function below works, when added as a script invoked by the onUpdate() function for the dexterity node on the charsheet, and invoked whenever a new piece of armor is dragged onto the character.
What I haven't figured out how to do is invoke it if a defenselist item is deleted. I've tried various combination of DB.addHandler, e.g. "DB.addHandler(defenselistode, "onUpdate", updateAdjDX)", but (a) it doesn't work; (b) how would I pass the nodeChar node as an argument?

I guess I could add another invocation of the function to the delete button on the equipment sheet? That seems kludgy and so far I haven't gotten it work right.

function updateAdjDX(nodeChar)
local adjdx = 0;
-- First get basic Dexterity --
adjdx = adjdx + DB.getValue(nodeChar, "attributes.dexterity", 0);

defenses = DB.getChild(nodeChar, "combat.defenselist");
if defenses then
for _,defense in pairs(defenses.getChildren()) do
adjdx = adjdx + defense.getChild("dexpen").getValue()
end
end
DB.setValue(nodeChar,"attributes.adjdx","number",adjdx);
end

damned
March 31st, 2020, 03:51
I dont think you need a handler

On each field that impacts this set a script to run to recalculate the derived value from the source values.
There is several examples of this in Fragged Empire extension for MoreCore.

damned
March 31st, 2020, 03:55
Any time Strength is changed it runs two scripts

CharacterUpdate.updateImpair(nodeWin);
CharacterUpdate.updateEndurance(nodeWin)

All the other attributes do similar things.



<number_charabilitytemp name="Strength" source="abilities.strength.temp">
<anchored to="strength" />
<target>strength</target>
<modifierfield>abilities.strength.tempmodifier</modifierfield>
<tooltip textres="char_tooltip_temp" />
<script>
function onValueChanged()
local nodeWin = window.getDatabaseNode();

CharacterUpdate.updateImpair(nodeWin);
CharacterUpdate.updateEndurance(nodeWin);


end
function onClickDown(button,x,y)
if Input.isAltPressed() and getValue() &gt; -5 then
local nodeWin = window.getDatabaseNode();
local nStrength = nodeWin.getChild("abilities.strength.tempmodifier").getValue();
ModifierStack.addSlot("Strength", nStrength);
end
return true;
end
</script>
</number_charabilitytemp>


UpdateImpair

function updateImpair(nodeWin)
local nEquipment = nodeWin.getChild("four").getValue();
local sCover = nodeWin.getChild("five").getValue();
local nCover = tonumber(sCover) or 0;
local nStrength = nodeWin.getChild("abilities.strength.tempmodifier").getValue();
local nReflexes = nodeWin.getChild("abilities.reflexes.score").getValue();
local nImpairMod = nodeWin.getChild("def.impair.mod").getValue();
local nDefenceImpair = nReflexes+nEquipment+nCover+nStrength+nImpairMod+1 0;
Debug.console("nDefenceImpair: ", nDefenceImpair);
nodeWin.getChild("def.impair.score").setValue(nDefenceImpair);
end


Update Endurance


function updateEndurance(nodeWin)
local nStrength = nodeWin.getChild("abilities.strength.tempmodifier").getValue();
local nEquipDef = nodeWin.getChild("four-a").getValue();
local nHealth = (nStrength*5)+nEquipDef+10;
Debug.console("nHealth: ", nHealth);
nodeWin.getChild("health").setValue(nHealth);
end

damned
March 31st, 2020, 03:56
You can see Imapir and Edurance derive from different numbers of stats even.

damned
March 31st, 2020, 03:58
Hah. I didnt read it very well. You do need a handler. Have the handler run the same script.

AmadanNaBriona
March 31st, 2020, 04:07
Right, so what is the appropriate event for checking when an item is added to the list, when an item is deleted from the list, and if an individual field in the list in changed? Do I need to an a handler for all three events?

AmadanNaBriona
March 31st, 2020, 04:33
Reply to self: answer is yes.

DB.addHandler(listNode.getPath(), "onChildAdded", updateAdjDX);
DB.addHandler(listNode.getPath(), "onChildDeleted", updateAdjDX);
DB.addHandler(listNode.getPath(), "onChildUpdate", updateAdjDX);

I also had to add code in updateAdjDX to make sure it finds the correct node for the character first.

superteddy57
March 31st, 2020, 07:38
Here is some additional information related to DB.addHandler to help you.
https://www.fantasygrounds.com/refdoc/DB.xcp#addHandler