Ren'Py Radiobuttons

Playstorepers

Member
May 24, 2020
160
79
Hello everyone,

According to the Ren'Py documentation, Ren'Py allows the use of radiobuttons.
Now I tried to google it, but the only threads, that show up are workarounds and no direct use of radiobuttons via Ren'Py.
With grouping of the Radiobuttons, toggling them on and off etc.

So the question, that I have is quite simple:
Does Ren'Py have a Radiobutton feature and if so, does anyone have a good link, which can explain, that to me?

And if RenPy does not have that feature, does anyone have a good link to explain, how I can emulate the radiobutton?
I think, I can understand it myself, if I look up toggle buttons, but you know... maybe there's an more elegant solution than toggle on/off textbuttons?

Thanks in advance
 

GNVE

Active Member
Jul 20, 2018
724
1,184
Yes you can see it in the preferences screen. That's where I stole the code from :p

Python:
style_prefix "radio"
label _("Display")
textbutton _("Window") action Preference("display", "window")
textbutton _("Fullscreen") action Preference("display", "fullscreen")
 
  • Like
Reactions: Playstorepers

Playstorepers

Member
May 24, 2020
160
79
That's very similar to the workarounds, that I read online.

So I tried my hand at this, but now I have another problem:

Python:
frame:
    xpos 170
    ypos 280
    vbox:
        spacing 22
        text "Test:":
            size 40
        hbox:
            spacing 40
            textbutton "Test1" action SetVariable("test", "0")
            textbutton "Test2" action SetVariable("test", "1")
            textbutton "Test3" action SetVariable("test", "2")
When I call the screen for the first time, no button is highlighted.
If I choose something, leave the screen and come back later, the selection is remembered.

I tried all kinds of workarounds to let the button hover, during the first call of the screen, but I am certain, that I'm missing something:

Workarounds, that I tried:
Python:
$SetVariable("test", "0")
$ test = 0
before and after calling the screen and inside the screen in and of itself.

test is:
default test = 0

Anyone got an idea, how I can make one of the text buttons light-up for the first call?

It's not that bad, but it's a small mistake, that I would like to fix.

EDIT:
Okay, so I decided to fuck around a bit to pinpoint the problem and it's kinda weird.
If I add to the screen above:
$SetVariable("test", "0")

then everytime, the screen gets called test must be set to 0 -> Test1 right?
But when I leave the screen, after I set it on Test2 for example, it will still highlight Test2, afterwards, which indicates, that highlighting of buttons is stored somewhere else than the variables.

Now more interestingly the music room in the Renpy Tutorial/documentation also uses the mechanic of highlighted buttons.
But there, it's working fine, during the first call.

And I don't know why.

Code for music room:
Code:
screen music_room:
    textbutton "Songname 1" action mr.Play("locationsong1")
    textbutton "Songname 2" action mr.Play("locationsong2")

    # Start the music playing on entry to the music room.
        on "replace" action mr.Play("location song2")
    
#with mr:

# Step 1. Create a MusicRoom instance.
    mr = MusicRoom(fadeout=0.0)

    # Step 2. Add music files.
    mr.add("location song 1", always_unlocked=True)
    mr.add("location song 2", always_unlocked=True)
That's very confusing...
Why does it work for the music room and the preference screen, but not for a custom screen with setvariables?
Why does it store the highlight, even if the value changed?
 
Last edited:

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,607
2,256
Gotta say, I'm curious about this one too.

I can see it does work in the preferences... I just don't see WHY it works.

There doesn't seem to be anything that connects the state of the buttons to the values. Yes, there are actions. But I don't seem to be able to make a connection to why the value affects which button/checkbox/radiobutton is activated next time the screen is shown.
 
  • Like
Reactions: Playstorepers

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,957
16,192
Anyone got an idea, how I can make one of the text buttons light-up for the first call?
Just give to the variable the value for this button.
You want "Test2" to be highlighted when the screen is called for the first time, just set test at "2".


If I add to the screen above:
$SetVariable("test", "0")

then everytime, the screen gets called test must be set to 0 -> Test1 right?
Wrong.

It just create a SetField (yes, I know, but SetVariable is in fact a function) object, nothing more.
The value will change only if this object is then called, what isn't the case here.


And I don't know why.
The reason is in plain light in the screen :
Code:
    # Start the music playing on entry to the music room.
        on "replace" action mr.Play("location song2")

Why does it work for the music room and the preference screen, but not for a custom screen with setvariables?
Because, unlike the music room, that start to play a song when the screen is opened, your custom screen do not give a value to the test variable.


Why does it store the highlight, even if the value changed?
It do not store the highlight, it's the value that haven't changed.



I can see it does work in the preferences... I just don't see WHY it works.
The answer is, partly, in recreation answer.

It's based on the fact that each button is, or not, selected, and that by default this selection depend of its value.

If by example you have :
Code:
default myBool = False

screen whatever():
    vbox:
        textbutton "On" action SetVariable( "myBool", True )
        textbutton "Off" action SetVariable( "myBool", False )
What button is selected will depend of the value of myBool. If the value is True, it's "On" that will be shown as selected. And it will be "Off" if the value is False.


But I don't seem to be able to make a connection to why the value affects which button/checkbox/radiobutton is activated next time the screen is shown.
Sometimes there's no need to search too far, the answer is : Because Ren'py works like that, dot.

Each screen action have a get_selected method, that will tell to Ren'py if the button is selected or not. For the SetVariable above, the code of this method is "variable == value" ; so if the variable already have the value it will be set to, then it's selected.
Why is it working like that ? Well, probably to offer this possibility to have radio buttons.

If you have more than one screen action, a more complex computation to tell if the button is selected or not, if the selection is totally unrelated to the value set, or if there's no value set for this button, then you can use either , as said by Recreation, or use the property.

But, as implied above, since all this is based on the fact that a button is, or not, selected, for the effect to be visible, you need to have buttons.
 
Last edited:

Playstorepers

Member
May 24, 2020
160
79
Thanks for the tips, you guys.
It's very interesting to go into the details.

And fun fact:
Even though, what you all said, is right,

MY METHOD STILL WORKS

I just fooled around a bit more and found my mistake.

My real mistake was:
test was declared as integer.

$SetVariable("test", "0")
doesn't work to highlight at first call

$SetVariable("test", 0)
works.

So what you all said, might be right, but your answers aren't the solution to my problem.
Well, not directly, at least

on "replace" action mr.Play("location song2")
^
I tested this one, before I edited my initial answer and that's not the reason, why the highlight doesn't work.

It's simpy the fact, that I'm retarded and used setvariable with a string instead of an integer.

Nonetheless, I am incredibly thankful for all your answers and insight.
I learned a lot by reading your answers.

So thanks again :D

EDIT:
SetVariable(test, "1") works really weird, when you used

declare test = 0

That's incredibly weird...
I fooled around a bit more and I don't seem to fully grasp it, but I'm happy, that it works.

EDIT 2:

My bad:
I didn't cite the right string/integer problem
I meant:

Python:
textbutton "Test1" action SetVariable("test", "0")
textbutton "Test2" action SetVariable("test", "1")
textbutton "Test3" action SetVariable("test", "2")
is in string and doesn't hightlight, because "declare test = 0" is in integer.

Python:
textbutton "Test1" action SetVariable("test", 0)
textbutton "Test2" action SetVariable("test", 1)
textbutton "Test3" action SetVariable("test", 2)
works perfectly, even with $SetVariable("test", 1) or $test = 1 before the screen call.
But you're right, when you tell me, that setvariable in the screen in and of itself doesn't work.
 
Last edited: