[SOLVED] if/else in imagebuttons

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
PinkePuchs
Newbie
Posts: 2
Joined: Sun Apr 21, 2024 10:48 am
Contact:

[SOLVED] if/else in imagebuttons

#1 Post 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
Last edited by PinkePuchs on Sun Apr 21, 2024 12:32 pm, edited 1 time in total.

User avatar
m_from_space
Miko-Class Veteran
Posts: 985
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: if/else in imagebuttons

#2 Post 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

PinkePuchs
Newbie
Posts: 2
Joined: Sun Apr 21, 2024 10:48 am
Contact:

Re: if/else in imagebuttons

#3 Post 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!!!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]