PDA

View Full Version : [programming] Activate Character Sheet Values



pr6i6e6st
September 16th, 2019, 19:20
Hey guys! I’m back with yet another question. Probably a simple answer out there.

In my AlienRPG ruleset, if you open a character sheet, and do not go to the “abilities” tab first, but instead try to toggle the “stressed” button (which checks the values of numbers in the abilities tab) you get an error about a “nil value”. Basically, the numbers don’t exist until the “abilities” tab has been opened.

This flows over to the combat tracker as well. If I have a PC in the combat tracker when I start up the campaign, and open the combat tracker before the character sheet has been opened, it fails to find/index the “radsmin” value on charsheet_main2 (the one with all the fancy graphics)

How can I activate those values or that tab upon starting the combat tracker or opening the character sheet?

Moon Wizard
September 16th, 2019, 20:37
In those cases, you need to have the stressed button or combat tracker options use the DB.setValue() functions to set the data on the numbers; or use DB.getValue() to query those numbers. In that way, if the fields don't exist, then DB.setValue() will create them when it sets the value, and DB.getValue() allows you to specify a default value if a field doesn't exist yet.

Regards,
JPG

pr6i6e6st
September 16th, 2019, 22:21
In those cases, you need to have the stressed button or combat tracker options use the DB.setValue() functions to set the data on the numbers; or use DB.getValue() to query those numbers. In that way, if the fields don't exist, then DB.setValue() will create them when it sets the value, and DB.getValue() allows you to specify a default value if a field doesn't exist yet.

Regards,
JPG

I will try this out. Do I need to include any kind of getDataBaseNode or getChild() functions? Or should DB.getValue() all that’s needed to set that value?

Trenloe
September 17th, 2019, 03:12
I will try this out. Do I need to include any kind of getDataBaseNode or getChild() functions? Or should DB.getValue() all that’s needed to set that value?
setValue() will set the value. But DB.getValue() does allow you to specify a default, which will be returned if the node doesn't already exist - this will avoid the nil error, but may not give you what you need.

But, how are you trying to get the data? Via the GUI controls or via the database? It's always best to go via the database, so that you're not relying on controls being activated (e.g. tabs opened) before you access them. But, even this is not 100% fool-proof, as database fields won't be created unless the controls have been activated or code written to create the fields. Some info on a related issue: https://www.fantasygrounds.com/forums/showthread.php?50720-LUA-Help&p=453491&viewfull=1#post453491

pr6i6e6st
September 17th, 2019, 03:22
setValue() will set the value. But DB.getValue() does allow you to specify a default, which will be returned if the node doesn't already exist - this will avoid the nil error, but may not give you what you need.

But, how are you trying to get the data? Via the GUI controls or via the database? It's always best to go via the database, so that you're not relying on controls being activated (e.g. tabs opened) before you access them. But, even this is not 100% fool-proof, as database fields won't be created unless the controls have been activated or code written to create the fields. Some info on a related issue: https://www.fantasygrounds.com/forums/showthread.php?50720-LUA-Help&p=453491&viewfull=1#post453491

here is what i have for the stressed button on the character sheet main page


<buttongroup_counter name="isstressed">
<anchored to="dateframe" width="20">
<right anchor="right" offset="-10" />
<top offset="40" />
</anchored>
<sourcefields>
<current>hiddenstress</current>
</sourcefields>
<values>
<maximum>1</maximum>
</values>
<tooltip text="Unchecked = automatic Stress reduction / Checked = no automatic Stress reduction" />
<script>
function onValueChanged()
local Hungry = window.getDatabaseNode().getChild(".hungry").getValue();
local Thirsty = window.getDatabaseNode().getChild(".thirsty").getValue();
local Sleepy = window.getDatabaseNode().getChild(".sleepy").getValue();
local Cold = window.getDatabaseNode().getChild(".cold").getValue();

if window.hiddenstress.getValue() == 0 and Hungry == 0 and Thirsty == 0 and Sleepy and Cold == 0 then
window.stressroll.onToggleon();
window.hiddenstress.setValue(0);
end
if window.hiddenstress.getValue() == 1 or Hungry == 1 or Thirsty == 1 or Sleepy == 1 or Cold == 1 then
window.stressroll.onToggleoff();
window.hiddenstress.setValue(1);
end


end
</script>
</buttongroup_counter>
<number_ct_crosslink name="hiddenstress" source="hiddenstress">
<anchored to="isstressed" position="above" offset="30,-40" width="30" height="20" />
<invisible />
</number_ct_crosslink>



and here's the code for the hungry number to link with the combat tracker and toggles the stressed button above.



<number_ct_crosslink name="hiddenstress" source="hiddenstress">
<anchored to="conditionsframe" position="insidetopright" offset="30,0" width="30" height="20" />
<invisible />
<script>
function ToggleOn()
setValue(1);
end
function ToggleOff()
setValue(0);
end
</script>
<invisible />
</number_ct_crosslink>

(labels and other stuff between here)


<number_ct_crosslink name="hungry" source="hungry">
<anchored to="conditionsframe" position="insidetopleft" offset="73,15" width="13" height="15" />
<invisible />
<script>
function onValueChanged()
nHungry = getValue();
nThirsty = window.thirsty.getValue();
nSleepy = window.sleepy.getValue();
nCold = window.cold.getValue();
local StateTime = CalendarManager.getCurrentTimeString();
local ActName = window.getDatabaseNode().getChild(".name").getValue();

if nHungry == 0 then
local msg = {font = "chatfont", text = "" .. StateTime .. ": " .. ActName .. " is no longer Hungry.", secret = false};
Comm.deliverChatMessage(msg);
if nThirsty == 0 and nSleepy == 0 and nCold == 0 then
if window.hiddenstress.getValue() == 1 then
window.hiddenstress.ToggleOff();
end
end
end
if nHungry == 1 then
window.hiddenstress.ToggleOn();
local msg = {font = "chatnpcfont", text = "" .. StateTime .. ": " .. ActName .. " is Hungry.", secret = false};
Comm.deliverChatMessage(msg);
end
end
</script>
</number_ct_crosslink>

pr6i6e6st
September 17th, 2019, 04:25
i might have fixed the error messages? i'm not entirely sure but i can't replicate it anymore.

Trenloe
September 17th, 2019, 11:26
Like I said in the post I linked - if the database fields are on another tab, that isn’t the main tab so the fields aren’t created until you go to that other tab for the first time (just once after creating the character), don’t use getChild but use createChild.