Radiobuttons - Handling on_gui_checked_state_changed

Place to get help with not working mods / modding interface.
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Radiobuttons - Handling on_gui_checked_state_changed

Post 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?
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Radiobuttons - Handling on_gui_checked_state_changed

Post 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 1019 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
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Radiobuttons - Handling on_gui_checked_state_changed

Post 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)
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Radiobuttons - Handling on_gui_checked_state_changed

Post 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
Post Reply

Return to “Modding help”