[Closed] How to keep radiobuttons.state
Hi everybody, I’m using radiobuttons in material, and I assume it to work like Clone dialog, which the radiobuttons will remember the last state I have used. But everytime I navigate to other material slot then back, the radiobuttons won’t keep the previous state I selected. My code (extracted) is something like this:
rollout params "Metal"
(
group "Metal Parameter"
(
radiobuttons metal_type columns:3 labels:#("S/S", "Chrome", "Gold", "Copper", "Brass", "Champagne", "Bronze", "Custom")
) -- end group
-- BUTTON ACTION
-------------------------------------------------------------------------------------
fn updateColor = (
case metal_type.state of
(
3: () -- gold
4: () -- copper
5: () -- brass
6: () -- champagne
7: () -- bronze
8: () -- custom
default:()
)
if metal_type.state != 8 do
(
)
if metal_type.state == 2 then
(
) else if metal_type.state == 7 or metal_type.state == 3 then (
) else (
)
) -- end fn updateColor
on metal_type changed state do updateColor()
Anything wrong with my code? I have read the maxscript help and look into some example script using radiobuttons but still no idea.
Thanks in advanced for the help!
If the rollout is part of a scripted material’s UI which from your description seems to be the case, you would have to connect the radio buttons control to a parameter in a parameter block for the state value to be stored and stay sticky between sessions.
If the rollout is part of some scripted tool, you will have to store the value of the radio button state somewhere yourself, either in a global variable to reuse it within the current Max session, or in an INI file on disk to make it sticky between sessions.
Hi Bobo,
It works! Thank you so much!
Another question: I take your 2nd suggestion, then I define the global variable and assign .state like below (red part), but it cannot work. Error message ‘expected name’ was produced. It seems like I can’t use the .state in the body of rollout?
I also tried putting the ‘global a = 1’ (green part) into updateColor() then in the case [ <expr> ] of I re-assigned ‘a’ value, but it is not working as well. And I also realized that value assigned in the case [ <expr> ] of seems has no effect at all (that’s why I have those if…else to do the re-assign work).
To find the answer, I have read rollout(), variable, etc in the help file but I can’t really understand why I can’t use .state there. Can you/anybody suggest me some idea or point me to any article/help file topic which talk about this issue?
Thank you very much.
rollout params "Metal"
(
group "Metal Parameter"
(
radiobuttons metal_type columns:3 labels:#("S/S", "Chrome", "Gold", "Copper", "Brass", "Champagne", "Bronze", "Custom")
) -- end group
-- BUTTON ACTION
-------------------------------------------------------------------------------------
global a = 1
metal_type.state = a
fn updateColor = (
global a = 1
metal_type.state = a
case metal_type.state of
(
3: (a=3) -- gold
4: (a=4) -- copper
5: (a=5) -- brass
6: (a=6) -- champagne
7: (a=7) -- bronze
8: (a=8) -- custom
default:()
)
if metal_type.state != 8 do
(
)
if metal_type.state == 2 then
(
) else if metal_type.state == 7 or metal_type.state == 3 then (
) else (
)
a = metal_type.state
) -- end fn updateColor
on metal_type changed state do updateColor()
The main problem is that you can’t define a global variable within the rollout definition scope (i.e. where you define the buttons, radiobuttons, etc.). You could define it in the radiobutton state change event, but it seems to me like you’re using it to (temporarily) store the value of the radiobuttons so that when the interface opens, it will use the previous value again for the radiobuttons. In that case, it’s better to treat it as a variable that should be define on initialization – for example, in the ‘on <rollout> open’ event.
rollout params "Metal" (
group "Metal Parameter"
(
radiobuttons metal_type columns:3 labels:#("S/S", "Chrome", "Gold", "Copper", "Brass", "Champagne", "Bronze", "Custom")
) -- end group
-- BUTTON ACTION
-------------------------------------------------------------------------------------
fn updateColor = (
case metal_type.state of
(
3: (a=3) -- gold
4: (a=4) -- copper
5: (a=5) -- brass
6: (a=6) -- champagne
7: (a=7) -- bronze
8: (a=8) -- custom
default:()
)
if metal_type.state != 8 do
(
)
if metal_type.state == 2 then
(
) else if metal_type.state == 7 or metal_type.state == 3 then (
) else (
)
a = metal_type.state
) -- end fn updateColor
on metal_type changed state do updateColor()
on params open do (
global a
if (a != undefined) then ( metal_type.state = a )
)
)
createDialog params 400 80 -- open the rollout as a dialog
Basically, when the rollout opens, the variable ‘a’ is first declared as global.
Then it checks whether the value of variable ‘a’ is undefined. If it isn’t, it changes the state of the radiobuttons to the value of variable ‘a’, otherwise it doesn’t do anything – you could do something here to set a default value in case you use the variable ‘a’ elsewhere.
I’ve removed the ‘a = 1’ line as that kept resetting the radiobutton state value, making it impossible to pick any option
If you now change the radiobutton state, the value of variable ‘a’ is changed. Close the dialog, and re-open it, and it should now automatically re-use the value of variable ‘a’ for the radiobutton state.
Note that the value of variable ‘a’ is not saved with the scene, for that you would have to use custom attributes, user properties, appdata, a persistent global variable, or… etc.