Please can anybody point me at a mod that will dynamically update a collection of radiobuttons in a GUI, so that only one is checked at anyone time, then I can nick their code (with full acknowledgement of course).
Better still, post some code here please?
Radiobuttons - Handling on_gui_checked_state_changed
Re: Radiobuttons - Handling on_gui_checked_state_changed
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Radiobuttons - Handling on_gui_checked_state_changed
I didn't even know factorio had premade radio button graphics, are those used anywhere in the base game?
I made a demo script (and darkfrei posted during those 10 mins it took me to write it, haha, you're fast :P):
I made a demo script (and darkfrei posted during those 10 mins it took me to write it, haha, you're fast :P):
Code: Select all
/c
game.player.gui.center.clear()
local box = game.player.gui.center.add{type='frame',name='mybox',direction='vertical'}
for i=1,10 do
local btn = box.add{type='radiobutton',caption='Button '..i,state = false}
if i == 1 then
btn.state = true --[[one radio button active by default]]
end
end
script.on_event(defines.events.on_gui_checked_state_changed,function(e)
if e.element.parent.name == 'mybox' then
local elm = e.element
if elm.state == false then --[[can't deactivate already active radio button]]
elm.state = true
else
for _,child in pairs(elm.parent.children) do
child.state = false
end
elm.state = true --[[turn it back on]]
end
end
end)
Re: Radiobuttons - Handling on_gui_checked_state_changed
Thank you both. My GUI has some other elements that are not radiobuttons, so I settled on this, based on eradicator's code. So I guess that's another credit I owe you.
Code for radiobuttons