Page 1 of 1

Radiobuttons - Handling on_gui_checked_state_changed

Posted: Sun Mar 11, 2018 1:09 pm
by DRY411S
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?

Re: Radiobuttons - Handling on_gui_checked_state_changed

Posted: Sun Mar 11, 2018 2:46 pm
by darkfrei
2018-03-11 15_44_44-Factorio 0.16.28.png
2018-03-11 15_44_44-Factorio 0.16.28.png (20.06 KiB) Viewed 1020 times
I'm sure that somebody can write it better, but it works.
EasyRadioButton_0.0.1.zip
For 0.16
(1.44 KiB) Downloaded 60 times

Re: Radiobuttons - Handling on_gui_checked_state_changed

Posted: Sun Mar 11, 2018 2:57 pm
by eradicator
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):

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

Posted: Sun Mar 11, 2018 9:59 pm
by DRY411S
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