Imagebuttons & Conditional Statements [Solved... sort of]

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
MonsoonMushroom
Regular
Posts: 62
Joined: Thu Jan 21, 2016 2:19 pm
Projects: Fate of the Squire
Organization: MonsoonMushroom
Contact:

Imagebuttons & Conditional Statements [Solved... sort of]

#1 Post by MonsoonMushroom »

Hello, I'm trying to make my imagebuttons do different actions depending on if/else statements. Specifically, I have a very simple money system and day planner type system, and when the player tries to pick an activity they don't have the money for, I want the game to tell them they can't afford it.

Right now, I have monkeypatched this solution:

Code: Select all

screen home_morn():
    tag menu1
    imagebutton auto "gui/iconarm_%s.png" xalign 0.6 yalign 0.15 focus_mask True hovered tt.Action("One of Somalaire's blacksmiths is prepared to teach me about armouring. Cost: 200c") action If ( (Money >= 200), true = [SetVariable("morning", "Arm"), Return()], false = Jump ("needmoney"))
    #the Return () statement takes them back to the code in script/rpy, which then checks the variables and runs the corresponding event
    
    #and then later on...
    
    label needmoney:
    Le "I can't afford this..."
    jump day #this brings back the home_morn screen
    
But there are a few problems with this:
1) when it jumps to the needmoney label, the home_morn screen menu disappears. I want to stay on the home_morn menu AND have the dialogue appear at the bottom of the screen
2) I'm sure there are more elegant ways of doing this than jumping around a bunch of labels. What I basically want to do is this...

Code: Select all

    imagebutton auto "gui/iconarm_%s.png" xalign 0.6 yalign 0.15 focus_mask True hovered tt.Action("One of Somalaire's blacksmiths is prepared to teach me about armouring. Cost: 200c")
  	  if Money >= 200:
        	action [SetVariable("morning", "Arm"), Return()]
   	 else:
        	Le "I can't afford this..."
        	action NullAction
But obviously that doesn't work, and I'm not good enough at coding to know the right way to do it. I've looked up other threads but none of them are quite what I'm after, and now I'm stumped. Can anyone explain how I make my imagebutton work with conditional statements?
Last edited by MonsoonMushroom on Sat Aug 26, 2017 2:35 pm, edited 1 time in total.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Imagebuttons & Conditional Statements

#2 Post by Remix »

imagebutton auto blah blah action If("Money >= 200", SetVariable("morning", "Arm"), NullAction) etc etc

If(expression, true=None, false=None)
This returns true if expression is true, and false otherwise. Use this to select an action based on an expression. Note that the default, None, can be used as an action that causes a button to be disabled.
Frameworks & Scriptlets:

MonsoonMushroom
Regular
Posts: 62
Joined: Thu Jan 21, 2016 2:19 pm
Projects: Fate of the Squire
Organization: MonsoonMushroom
Contact:

Re: Imagebuttons & Conditional Statements

#3 Post by MonsoonMushroom »

Remix wrote: Mon Aug 14, 2017 1:55 pm imagebutton auto blah blah action If("Money >= 200", SetVariable("morning", "Arm"), NullAction) etc etc

If(expression, true=None, false=None)
This returns true if expression is true, and false otherwise. Use this to select an action based on an expression. Note that the default, None, can be used as an action that causes a button to be disabled.

Heey, that cleans my code up a bit, thank you :D It looks like this now and works fine:

Code: Select all

imagebutton auto "gui/iconarm_%s.png" xalign 0.6 yalign 0.15 focus_mask True hovered tt.Action("One of Somalaire's blacksmiths is prepared to teach me about armouring. Cost: 200c") action If ( Money>=200, [SetVariable("morning", "Arm"), Return()], NullAction () )
I use NullAction () instead of None because my tooltips don't appear when the button is inactive. However, as well as disabling the button, I also need to bring up a little "I can't afford this..." message. I'm not sure how to do that without using Jump! Is there any way I can trigger a dialogue response to pressing the button without jumping to a new scene, then jumping back?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Imagebuttons & Conditional Statements

#4 Post by Remix »

[ Function(renpy.say, narrator, "I can't afford this...", interact=False), NullAction() ] *might* work or maybe just use Function to wrap a different worded tt.Action tooltip
Frameworks & Scriptlets:

MonsoonMushroom
Regular
Posts: 62
Joined: Thu Jan 21, 2016 2:19 pm
Projects: Fate of the Squire
Organization: MonsoonMushroom
Contact:

Re: Imagebuttons & Conditional Statements

#5 Post by MonsoonMushroom »

In the end I went for changing the tooltip text and button function with if statements. I think it's probably better this way, if players can see what they can/can't afford at a glance. My code looks like this:

Code: Select all

    if Money >= 200: 
        imagebutton: 
            auto "gui/tasks/iconarm_%s.png" xalign 0.6 yalign 0.15 focus_mask True hovered tt.Action("One of Somalaire's blacksmiths is prepared to teach me about armouring. Cost: 200c") 
            action [SetVariable("Money", Money - 200), SetVariable("morning", "Arm"), Return()]
    else:
        imagebutton idle "gui/tasks/iconarm_inactive.png" xalign 0.6 yalign 0.15 focus_mask True hovered tt.Action("I need at least 200c if I want the blacksmith to teach me.") action NullAction()
It seems to work fine! Thank you for the help Remix :)

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], piinkpuddiin, Semrush [Bot], snotwurm