• To improve security, we will soon start forcing password resets for any account that uses a weak password on the next login. If you have a weak password or a defunct email, please update it now to prevent future disruption.

Ren'Py Variables points on imagebuttons ?

shitcrap1

Newbie
Game Developer
Aug 15, 2023
79
374
So i've been having trouble with add variable points where I need to click certain imagebuttons tu get enough points to unlock another imagebutton
This is my code:

init python:
points = 0
unlock_button2_points = 3

screen store():
add "images/mapscreens/sto.png"
if button_visible2 and points < unlock_button2_points:
imagebutton:
focus_mask True
auto "images/Mapscreens/stot_%s.png" action [ToggleScreen("store"), Jump("cashier_1"), SetVariable("points", points + 1)]


screen gasrestroom():
add "sscene5"
if not unlock_button2_points:
imagebutton:
focus_mask True
auto "images/mapscreens/gasrestroom_%s.png" action [ToggleScreen("gasrestroom"), Jump("restroomin"), if points >= 3: SetVariable("unlock_button2_points", True)]

label_whatever

if unlock_button2_points:
"You've unlocked the second button!"

If put
if points >= 3: SetVariable("unlock_button2_points", True)]
the game crashes
and if I delete it
the game just keeps the variable frozen
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,202
14,939
Python:
            auto "images/mapscreens/gasrestroom_%s.png" action [ToggleScreen("gasrestroom"), Jump("restroomin"), if points >= 3: SetVariable("unlock_button2_points", True)]
[...] the game crashes [...]
And with reason.

From where come that idea to put an if whatever: something in the action property ? Especially when it is explicitly expressed as a list of values ?

Ren'Py already have a limitation, not understanding one line blocks expressed in one line:
Python:
label whatever:
    if day == "Monday": $ week += 1
wouldn't works, while it's a legit syntax in Python. Instead you need to explicitly mark the blocks:
Python:
label whatever:
    if day == "Monday": 
        $ week += 1
But if in top of that you put code in the middle of a list of values, it's not surprising that Ren'Py starts to insult the world. Anyway, even if it wasn't, properties expect data, at most assessable conditions (X == Y), not code.

What you are looking for is the screen action:
Python:
        imagebutton:
            auto "images/mapscreens/gasrestroom_%s.png" 
            action [ ToggleScreen("gasrestroom"), Jump("restroomin"), If( points >= 3, SetVariable("unlock_button2_points", True), NullAction() ) ]
Alternatively, if there's a lot of variation depending on the value, it's possible to put the if outside of the action property:
Python:
        imagebutton:
            auto "images/mapscreens/gasrestroom_%s.png" 
            if points >= 3:
                action [ ToggleScreen("gasrestroom"), Jump("restroomin"), SetVariable("unlock_button2_points" ) ]
            else:
                action [ ToggleScreen("gasrestroom"), Jump("restroomin") ]

Now, this being said, there's another flaw in your code, the position of the Jump() screen action.

There's few screen actions that act as branching points. The most frequent ones are Call(), Jump() and Return(), but it's also the case for RestartStatement, as well as RollForward() and the other screen actions related to the rollback feature.
As "branching point", they direct Ren'Py somewhere else in the code, what mean that Ren'Py will never reach whatever is following them in the list of actions.

Therefore, your code MUST be:
Python:
        imagebutton:
            auto "images/mapscreens/gasrestroom_%s.png" 
            action [ ToggleScreen("gasrestroom"), If( points >= 3, SetVariable("unlock_button2_points", True), NullAction() ), Jump("restroomin") ]
 

peterppp

Member
Mar 5, 2020
469
879
init python:
points = 0
unlock_button2_points = 3

if not unlock_button2_points:

SetVariable("unlock_button2_points", True)

if unlock_button2_points:
in the above code, setting unlock_button2_points to True won't do any difference because a value of 3 will already be considered true in a comparison like that. a value of 0 will be false.
 
  • Like
Reactions: anne O'nymous

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,202
14,939
in the above code, setting unlock_button2_points to True won't do any difference because a value of 3 will already be considered true in a comparison like that. a value of 0 will be false.
Good catch, I totally missed this part.
 
  • Like
Reactions: peterppp