PDA

View Full Version : Automatically update label value



AmadanNaBriona
April 7th, 2020, 19:27
This seems like it should be easy, and I have read how to add handlers (https://www.fantasygrounds.com/refdoc/DB.xcp#addHandler) and custom handler functions (https://www.fantasygrounds.com/modguide/scripting.xcp), but the tricks I am using aren't working so far.

So let's say I have a simple function in a char manager script file (extraneous checks omitted):



function calcEncumbrance(nodeChar)
local enc = DB.getValue(nodeChar, "encumbrance.load");
local st = DB.getValue(nodeChar, "attributes.strength");
// calculate encumbrance, generate a string value, e.g. "Heavy", "Light", "None", etc.
return encumbranceString;
end


I want it to be used like so:



<frame_char name="encumbranceframe">
<bounds>15,-219,240,-15</bounds>
</frame_char>
<simplenumber name="encumbranceload" source="encumbrance.load">
<script>
function onValueChanged()
// want to fire off a signal here to make "encumbrance" call changeMyvalue()
end
</script>
<anchored to="encumbranceframe" position="insidetopleft" offset="160,30" width="55" height="20" />
<frame name="fieldlight" offset="7,5,7,5" />
<readonly />
</simplenumber>
<label name="encumbrance_label">
<anchored to="encumbranceframe" position="insidetopleft" offset="15,30" />
<static textres="char_label_totalweight" />
</label>
<label name="encumbrance">
<anchored to="encumbranceframe" position="insidetopleft" offset="15,60" />
<script>

function onInit()
changeMyValue();
end


function changeMyValue()
local charNode = CharacterManager.getCharsheet(window);
local sEnc = CharacterManager.calcEncumbrance(charNode);
setValue(sEnc);
end
</script>
</label>

Trenloe
April 7th, 2020, 19:36
Moved this thread to the Workshop forum, which is the correct place to ask about coding advice.

Trenloe
April 7th, 2020, 19:38
Where your red entry is put encumbrance.changeMyValue();

See the [control_name] scope mentioned here: https://www.fantasygrounds.com/wiki/index.php/Developer_Guide_-_Rulesets_-_Scripting#Script_Block_Scope

AmadanNaBriona
April 7th, 2020, 19:57
Thanks - the block scope helped (I think I skimmed over that before). But calling encumbrance directly from a "sibling" control didn't work. What did work was window['encumbrance'].changeMyValue().