Starfinder Playlist
  1. #1

    Registering an event handler for a deleted list item

    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",a djdx);
    end

  2. #2
    damned's Avatar
    Join Date
    Mar 2011
    Location
    Australia
    Posts
    26,638
    Blog Entries
    1
    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.

  3. #3
    damned's Avatar
    Join Date
    Mar 2011
    Location
    Australia
    Posts
    26,638
    Blog Entries
    1
    Any time Strength is changed it runs two scripts

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

    All the other attributes do similar things.


    Code:
    			<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
    Code:
    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+10;
    	Debug.console("nDefenceImpair: ", nDefenceImpair);
    	nodeWin.getChild("def.impair.score").setValue(nDefenceImpair);
    end
    Update Endurance

    Code:
    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

  4. #4

  5. #5

  6. #6
    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?

  7. #7
    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.

  8. #8
    Here is some additional information related to DB.addHandler to help you.
    https://www.fantasygrounds.com/refdoc/DB.xcp#addHandler
    Dominic Morta
    Ruleset Developer
    Smiteworks

    How to zip up your campaign if the Developers ask for it-How to zip up your campaign if the Developers ask for it

    How to provide an Unity Connection issue?-Connection Issues and What to Provide

    Unity Updater issue?-Updater Issues

    Classic and Unity Port Forwarding?-Fantasy Grounds Connections Explained

    Comcast or Cox ISP User?-Comcast XFinity and Cox Users

    Have a suggestion?-Idea Informer

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
STAR TREK 2d20

Log in

Log in