PDA

View Full Version : Cycler Behaviour and Default Values



damned
July 16th, 2018, 10:10
Hi team,

Im trying to create a string-cycler with numeric values - eg Descriptions shown but Numbers stored.
How do I eliminate the blank value that always appears in all my cyclers?
This is an issue as Im running an onValueChanged script on the cycler and it does some math so when it gets around to the blank value it generates a script error.

Samples of code:

<template name="cycler_cover">
<button_stringcycler>
<parameters>
<labels>None|Light|Heavy|Entrenched</labels>
<defaultlabelres mergerule="replace">None</defaultlabelres>
<values>0|2|4|6</values>
</parameters>
</button_stringcycler>
</template>




[code] <cycler_cover name="five" source="five">
<anchored to="combatframe" position="insidetopright" offset="20,112" width="64" />
<default>0</default>
<script>
function onValueChanged()
local rActor = ActorManager.getActor("pc", window.getDatabaseNode());
local nodeWin = window.getDatabaseNode();
local nReflexes = nodeWin.getChild("abilities.reflexes.tempmodifier").getValue();
local nEquipment = nodeWin.getChild("four").getValue();
local nCover = nodeWin.getChild("five").getValue();
local nDefence = nReflexes+nEquipment+nCover+10;
nodeWin.getChild("defence").setValue(nDefence);
end
</script>
</cycler_cover>

Any suggestions?

Ikael
July 16th, 2018, 10:16
Not sure if you can remove the blank value itself but to play safe you could do something like

nCover = nCover == "" and 0 or tonumber(nCover)

damned
July 16th, 2018, 12:08
Not sure if you can remove the blank value itself but to play safe you could do something like

nCover = nCover == "" and 0 or tonumber(nCover)

Hi Aki

I am not sure I follow your code?

Ikael
July 16th, 2018, 13:25
https://lua-users.org/wiki/TernaryOperator

More importantly, you could make blank value to represent zero. You will anyways need to convert string values into numbers to use them arithmetically. So when you read the value you could convert blank into zero, unless the controller allows you to skip blank, which I am not sure you could do.

damned
July 16th, 2018, 13:58
https://lua-users.org/wiki/TernaryOperator

More importantly, you could make blank value to represent zero. You will anyways need to convert string values into numbers to use them arithmetically. So when you read the value you could convert blank into zero, unless the controller allows you to skip blank, which I am not sure you could do.

I have tried a couple of statements but it looks like the problem is possibly different.
If i do a /save while the cycler is on a blank value then the field is removed from the db altogether

<defence type="number">20</defence>
<four type="number">0</four>
<health type="number">15</health>

If the cycler is on any of the values including None / 0 then it saves the value.

<defence type="number">16</defence>
<five type="string">2</five>
<four type="number">0</four>
<health type="number">15</health>

If there is no way to stop the blank values being shown - and thus the field being removed - is there a way to check if the field is present?

Ikael
July 16th, 2018, 14:03
FG does remove any and all blank string typed databasenodes to save space. You could read the value safely with DB.getValue(nodeWin, "five", "")
Alternatively you can check if node exists with

if nodeWin.getChild("five") then
...
end

damned
July 16th, 2018, 15:30
Aki thank you for your assistance.
This is what I ended up with - yes its ugly - but it only took me 6 hours.



local sCover = nodeWin.getChild("five").getValue();
if not sCover then
local sCover = 0;
end
local nCover = tonumber(sCover);

if nCover == nil then
nCover = 0;
end

damned
July 16th, 2018, 16:05
some smart alec just came along and did:


local sCover = DB.getValue(nodeWin,"five","0");
local nCover = tonumber(sCover) or 0;

or


local sCover = nodeWin.getChild("five").getValue();
local nCover = tonumber(sCover) or 0;


which is I think what you were steering me towards Aki.