PDA

View Full Version : Toggling a button via script



AmadanNaBriona
April 2nd, 2020, 21:11
I have assigned a "Leader" button to CT entries. Clicking it designates that character as the leader of their faction ("friend", "foe", "neutral", etc.)

Only one character can be the leader of a faction. If I click a leader button, I want anyone in that faction who was previously the leader to be removed as leader.

I can set the value for a faction's leader in the DB and remove the previous leader. However, how can I uncheck the button of the previous leader on the CT?

function onLeaderChanged()
local nodeRecord = getDatabaseNode();
local bLeader = leader.getValue();
-- can only be one leader
if bLeader then
DB.setValue(nodeRecord, "leader", "boolean", true);
local sFaction = DB.getValue(nodeRecord, "friendfoe", "");
for _,v in pairs(CombatManager.getCombatantNodes()) do
if DB.getValue(v, "friendfoe", "") == sFaction then
if nodeRecord ~= v then
DB.setValue(v, "leader", "boolean", false);
-- NEED CODE HERE - I want to toggle the "leader" button for v
end
end
end
end

Debug.chat(nodeRecord,bLeader,sClass,sFaction);
end

superteddy57
April 2nd, 2020, 21:25
I think the method I would use to approach this would be to do a for loop to go through each window and set leader false.

Something like:
for _,w in pairs(getWindows()) do
w.leader.setValue(0 or whatever value turns it off)
end

It's a rough idea, but hopefully it helps

AmadanNaBriona
April 2nd, 2020, 23:23
Thank you, that worked, basically. I didn't realize that setting the value also toggles the button itself.

Moon Wizard
April 2nd, 2020, 23:27
@AmadanNaBriona,

It depends on the implementation of the button. If the button defines more than one state, then you can change the state by using setValue. (The underlying value type is number.) If no states defined (or only a single state), then the value is always zero.

Regards,
JPG