Page 1 of 1

[SOLVED] if/else in imagebuttons

Posted: Sun Apr 21, 2024 10:58 am
by PinkePuchs
I would like to write a series of buttons that must be pressed in a certain order, but the second and subsequent buttons do not always accept the variable changed when the first button is clicked, although the clicks are processed. How could this be fixed or implemented in a different form?

Code: Select all

define mwrcount = 0
define mwround = 0
define m1 = False
define m2 = False

label mground1:
    hide screen M1lvl
    $ m1 = False
    $ m2 = False
    show screen M1lvl
    "ROUND 1"

label mgokay:
    "[mwrcount], [m1], [m2]"

screen M1lvl():
     vbox:
        xalign 0.29 yalign 0.448
        imagebutton:
            auto "button_%s.png"
            action [SetVariable("m1", True), Jump("mgokay")]
            activate_sound click
     vbox:
        xalign 0.41 yalign 0.56
        imagebutton:
            auto "button_%s.png"
            if m1:
                action [SetVariable("m2", True), Jump("mgokay")]
                activate_sound click
            else:
                action [IncrementVariable("mwrcount", 1), Jump("mgwrong1")]
     vbox:
        xalign 0.755 yalign 0.24
        imagebutton:
            auto "button_%s.png"
            if m1:
                if m2:
                    action Jump("next_round")
                    activate_sound click
                else:
                    action [IncrementVariable("mwrcount", 1), Jump("mgwrong1")]
            else:
                action [IncrementVariable("mwrcount", 1), Jump("mgwrong1")]

label mgwrong1:
    "Wrong answer"
    jump mground1

Re: if/else in imagebuttons

Posted: Sun Apr 21, 2024 11:48 am
by m_from_space
PinkePuchs wrote: Sun Apr 21, 2024 10:58 am I would like to write a series of buttons that must be pressed in a certain order, but the second and subsequent buttons do not always accept the variable changed when the first button is clicked, although the clicks are processed. How could this be fixed or implemented in a different form?
There are a couple of issues with your code in general.

1. You are using "define" for creating variables, which is wrong. It is used for constants. You have to use "default" instead! The "variables" you created might change during runtime, but Renpy won't save any changes and it also can have unforseen consequences. Maybe this fixes your problems?

2. Your labels don't jump anywhere when the "end" is reached. That means the following labels will be executed (or other code that's in between and not part of the init process).

By the way, the function IncrementVariable() by default increments by 1, so you do not have to include it inside the function argument

Re: if/else in imagebuttons

Posted: Sun Apr 21, 2024 12:30 pm
by PinkePuchs
m_from_space wrote: Sun Apr 21, 2024 11:48 am
PinkePuchs wrote: Sun Apr 21, 2024 10:58 am I would like to write a series of buttons that must be pressed in a certain order, but the second and subsequent buttons do not always accept the variable changed when the first button is clicked, although the clicks are processed. How could this be fixed or implemented in a different form?
There are a couple of issues with your code in general.

1. You are using "define" for creating variables, which is wrong. It is used for constants. You have to use "default" instead! The "variables" you created might change during runtime, but Renpy won't save any changes and it also can have unforseen consequences. Maybe this fixes your problems?

2. Your labels don't jump anywhere when the "end" is reached. That means the following labels will be executed (or other code that's in between and not part of the init process).

By the way, the function IncrementVariable() by default increments by 1, so you do not have to include it inside the function argument
Changing to "default" helped!
Thanks a lot!!!