PDA

View Full Version : Persistently toggling an icon



Miserere
August 6th, 2008, 06:04
I am making a new character sheet for a custom ruleset. In front of each skill I want a little checkbox icon that will be checked if the skill is related to the character's profession, or unchecked if it is not (the default). I have figured out how to make a toggling checkbox that switches back and forth between checked and unchecked, but at the moment this is not recorded in the database and so all skills are unchecked when you reopen the character sheet. I thought I had the right code to make this persistent, but apparently I am missing something. Can someone help? Thanks in advance.

Here is the relevant code for the checkbox object in the XML:

<genericcontrol>
<icon>indicator_checkoff</icon>
<script file="scripts/charsheet_skillstate.lua" />
</genericcontrol>


Here is the Lua script referenced in the object:

function toggle()
if (onOff == 1) then
onOff = 0;
setIcon("indicator_checkoff");
else
onOff = 1;
setIcon("indicator_checkon");
end
end

function onClickDown(button, x, y)
toggle();
end

function onInit()
-- Set selection
onOff = window.getDatabaseNode().createChild("switch", "number");
if (onOff == 1) then
setIcon("indicator_checkon");
end
end

Foen
August 6th, 2008, 06:42
Hi Miserere, and welcome to the FG boards (and the addictive world of FG modding) :D

There is an easy way and a hard way to do this, I think.

The easy way is to use the template checkbox control already predefined in the standard ruleset:



<checkbox name="skillstate">
<bounds>0,2,14,14</bounds>
... etc ...
</checkbox>


The hard way is to modify your code so that it does what you want. I think you've nearly cracked it already, you are correctly retrieving the state but you aren't saving the state back to database.

Try modifying the function 'toggle' as follows:


function toggle()
if (onOff == 1) then
onOff = 0;
setIcon("indicator_checkoff");
else
onOff = 1;
setIcon("indicator_checkon");
end
window.getDatabaseNode().createChild("switch","number").setValue(onOff);
end


Hope that helps

Stuart

Miserere
August 6th, 2008, 22:35
Thanks so much! I tried your "hard way" solution and it didn't work, but the "easy way" solution was very east to implement and it's working like a charm.