Add if/else to Imagebutton? (SOLVED)

Scarecrow-76

Newbie
Oct 4, 2021
42
21
Here's a simple enough code for my image button. It triggers the first conversation between the mc and Nikki. But I only want to jump to this conversation once. If the mc goes back to her, I would like it to jump to another label. I'm guessing I'll be using if/else or true/false? But I can't find any tuts on this for imagebuttons. I know how to use menus with flags in conversations but not in imagebuttons. This seems to be a pretty common mechanic in renpy games, so I'm surprised I cant find it. Yes, I went through the renpy documents. But being a newbie, it might as well be in Japanese, lol.



Code:
screen Nikki_day_button():
    imagebutton:
        xpos 708
        ypos 440
        idle "images/Character_buttons/Nikki_day_idle.png"
        hover "images/Character_buttons/Nikki_day_hover.png"
        action Jump("Nikki_intro")
 

Deleted member 3831983

Member
Game Developer
Jun 20, 2021
265
991
Code:
screen Nikki_day_button():

    imagebutton:

        xpos 708

        ypos 440

        idle "images/Character_buttons/Nikki_day_idle.png"

        hover "images/Character_buttons/Nikki_day_hover.png"

        if idle_nikki == 1:
            action Jump("Nikki_intro_repeat")
        else:
            action Jump("Nikki_intro")
This is basically how I do it with the passage of time resetting the variable so it is as if you're coming to her for the first time that day. Just have it jump to two different labels. BUT I just have the if/else statement within one label and have what needs to be different there before it goes into a menu
 
  • Like
Reactions: Scarecrow-76

Deleted member 3831983

Member
Game Developer
Jun 20, 2021
265
991
I would also suggest putting focus_mask True on your imagebuttons if you want the transparent parts of the image to not be hoverable. so it isn't just a square button
 

Scarecrow-76

Newbie
Oct 4, 2021
42
21
Code:
screen Nikki_day_button():

    imagebutton:

        xpos 708

        ypos 440

        idle "images/Character_buttons/Nikki_day_idle.png"

        hover "images/Character_buttons/Nikki_day_hover.png"

        if idle_nikki == 1:
            action Jump("Nikki_intro_repeat")
        else:
            action Jump("Nikki_intro")
This is basically how I do it with the passage of time resetting the variable so it is as if you're coming to her for the first time that day. Just have it jump to two different labels. BUT I just have the if/else statement within one label and have what needs to be different there before it goes into a menu
I tried it real quick, because I have to go. But Im getting an error. First idle_nikki not defined, so I figured you meant to put Nikki_day_idle. But that's coming up not defined as well.

Code:
I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/scripts/location_jump_to/vendors/vendors.rpy", line 29, in script
    call screen Nikki_day_button
  File "renpy/common/000statements.rpy", line 569, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
  File "game/scripts/screens/user_screen.rpy", line 20, in execute
    screen Nikki_day_button():
  File "game/scripts/screens/user_screen.rpy", line 20, in execute
    screen Nikki_day_button():
  File "game/scripts/screens/user_screen.rpy", line 21, in execute
    imagebutton:
  File "game/scripts/screens/user_screen.rpy", line 21, in keywords
    imagebutton:
  File "game/scripts/screens/user_screen.rpy", line 27, in keywords
    if Nikki_day_idle == 1:
  File "game/scripts/screens/user_screen.rpy", line 27, in <module>
    if Nikki_day_idle == 1:
NameError: name 'Nikki_day_idle' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/scripts/location_jump_to/vendors/vendors.rpy", line 29, in script
    call screen Nikki_day_button
  File "renpy/ast.py", line 2015, in execute
    self.call("execute")
  File "renpy/ast.py", line 2003, in call
    return renpy.statements.call(method, parsed, *args, **kwargs)
  File "renpy/statements.py", line 278, in call
    return method(parsed, *args, **kwargs)
  File "renpy/common/000statements.rpy", line 569, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
  File "renpy/exports.py", line 3136, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "renpy/ui.py", line 298, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "renpy/display/core.py", line 3325, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, pause=pause, pause_start=pause_start, **kwargs)
  File "renpy/display/core.py", line 3737, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/core.py", line 568, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 568, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 568, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/screen.py", line 436, in visit_all
    callback(self)
  File "renpy/display/core.py", line 3737, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/screen.py", line 447, in per_interact
    self.update()
  File "renpy/display/screen.py", line 637, in update
    self.screen.function(**self.scope)
  File "game/scripts/screens/user_screen.rpy", line 20, in execute
    screen Nikki_day_button():
  File "game/scripts/screens/user_screen.rpy", line 20, in execute
    screen Nikki_day_button():
  File "game/scripts/screens/user_screen.rpy", line 21, in execute
    imagebutton:
  File "game/scripts/screens/user_screen.rpy", line 21, in keywords
    imagebutton:
  File "game/scripts/screens/user_screen.rpy", line 27, in keywords
    if Nikki_day_idle == 1:
  File "game/scripts/screens/user_screen.rpy", line 27, in <module>
    if Nikki_day_idle == 1:
NameError: name 'Nikki_day_idle' is not defined
 

Deleted member 3831983

Member
Game Developer
Jun 20, 2021
265
991
You need to basically set the variable to something whenever you start the game, 'defining' it.

So anywhere in any script, usually a place where you can keep all your variables organized, just do...
default nikki_day_idle = 0

if you ever want to change the idle to something else just do
$ nikki_day_idle = Whatever you want, like 1 for example
 
  • Like
Reactions: Scarecrow-76

Scarecrow-76

Newbie
Oct 4, 2021
42
21
It Worked!!! Thanks so much!! That tackles a huge part of the coding that I was worried about learning. Thanks!!

label Nikki_intro:
$ Nikki_day_idle += 1
scene balloon_zoom:
blur 16
show Nikki_normal at right
show mc_normal at left
with dissolve
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,978
16,234
Code:
screen Nikki_day_button():
    imagebutton:
        xpos 708
        ypos 440
        idle "images/Character_buttons/Nikki_day_idle.png"
        hover "images/Character_buttons/Nikki_day_hover.png"
        if idle_nikki == 1:
            action Jump("Nikki_intro_repeat")
        else:
            action Jump("Nikki_intro")
Alternatively it's possible to have:
Code:
screen Nikki_day_button():
    imagebutton:
        [...]
        hover "images/Character_buttons/Nikki_day_hover.png"
        action Jump("Nikki_intro_repeat" if idle_nikki == 1 else "Nikki_intro")
Or, if there will be a lot of possible condition:
Code:
init python:
    def nikkiLabels():
        if   idle_nikki == 0: return "Nikki_intro"
        elif idle_nikki == 1: return "Nikki_intro_repeat" 
        elif idle_nikki == 2: return "Nikki_is_borred_to_repeat_herself"
         

screen Nikki_day_button():
    imagebutton:
        [...]
        hover "images/Character_buttons/Nikki_day_hover.png"
        action Jump( nikkiLabels() )
Alternatively the selection can be made in a label:
Code:
screen Nikki_day_button():
    imagebutton:
        [...]
        hover "images/Character_buttons/Nikki_day_hover.png"
        action Jump( "Nikki_hub" )

label Nikki_hub:
    if   idle_nikki == 0: 
        jump Nikki_intro
    elif idle_nikki == 1: 
        jump Nikki_intro_repeat
    elif idle_nikki == 2: 
        jump Nikki_is_borred_to_repeat_herself
 
  • Like
Reactions: Scarecrow-76