ConditionSwitch and Imagebutton

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
terrordoll
Regular
Posts: 46
Joined: Tue Apr 11, 2017 11:57 am
Contact:

ConditionSwitch and Imagebutton

#1 Post by terrordoll »

I'm having trouble getting a condition switch to work with an image button and I can't seem to find the answer in any of the searches I've done. I have a feeling the real issue here is a misunderstanding on how Renpy and Python reference variables. Functionally everything seems set up ok, I think there is just a slight syntax error that is halting me. If anyone has a second to look I'd appreciate it.

Here is the script code:

Code: Select all

init:
    default mixer_left = "none"
    image mixer_left_idle_image = ConditionSwitch("mixer_left == none", "images/mixer_flask_idle.png", 
        "mixer_left == purple_chemical", "images/mixer_flask_purple.png",
        "mixer_left == green_chemical", "images/mixer_flask_green.png",
        "mixer_left == orange_chemical", "images/mixer_flask_orange.png")
my screens code:

Code: Select all

screen mixing_screen():    
    imagebutton idle mixer_left_idle_image hover mixer_left_idle_image action SetVariable(mixer_left = helditem) sensitive mixer_left == "none" xpos 400 ypos 0
I'm fairly certain the problem is with how i'm referencing variables. Sometimes its just the variable name, sometimes it wants "" or ''. Coming from C# this is just confusing me.

The error I'm getting now is that mixer_left_idle_image is not defined. I'm not sure if the problem is the image declaration or the reference to it in the image button/ I'm also fairly certain the set variable syntax is wrong.

If someone has a second to either explain why the variable reference syntax is different and why that is I'd be really appreciative. Teach a man to fish and all that.

terrordoll
Regular
Posts: 46
Joined: Tue Apr 11, 2017 11:57 am
Contact:

Re: ConditionSwitch and Imagebutton

#2 Post by terrordoll »

Oh good lord I think I figured it out, at least partially. I didn't get that that ' and " are essentially the same thing. I'm still not sure why variables are accessible by their reference name and have to be a string some times but I can deal with that.

So my first mistake was not putting double quotes around the value in my ConditionSwitch. Its should have been "mixer_left == 'none'" instead of just "mixer_left == none".

That then I noticed that SetVariable doesn't take a conditional statement. So this SetVariable(mixer_left = helditem) should have been SetVariable(mixer_left, helditem).



*EDIT: Looks like SetVariable also expects strings SetVariable("mixer_left", "helditem") instead of a direct variable reference. Still not sure I understand why it won't take a variable directly.

*EDIT 2: Well I was partially right. Using SetVariable("mixer_left", "helditem") this way looks up the variable by string name but takes the value literally.
I had to do this: SetVariable("mixer_left", helditem) to get it to work, otherwise it would assign my variable to the the string "helditem" instead of the value which is "purple_chemical". I'm sure there is a great reason why it does this, but it would have been nice to throw in two vars and have it just assign the values.
Last edited by terrordoll on Wed May 24, 2017 2:18 pm, edited 1 time in total.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: ConditionSwitch and Imagebutton

#3 Post by trooper6 »

Here is the code that does what you are looking for:

Code: Select all

default mixer = "none"

image redSquare = Solid("#ff0000",xsize=100,ysize=100)
image greenSquare = Solid("#00ff00",xsize=100,ysize=100)
image blueSquare = Solid("#0000ff",xsize=100,ysize=100)
image switchSquare = ConditionSwitch(
    "mixer == 'red'", "redSquare",
    "mixer == 'green'", "greenSquare",
    "True", "blueSquare")
    
screen tester():
    imagebutton:
        idle "switchSquare" 
        hover "switchSquare"
        action NullAction()

label start:
    "Just testing."
    show screen tester()
    "Starting blue"
    $mixer = "red"
    "Now Red"
    $mixer = "green"
    "Now Green"
    return
So comparing your code and my code. From top to bottom.

1) Don't put your defaults and image declarations inside an init block. Renpy does all of this.
2) For your ConditionSwitch, make sure one condition is always true. For your button, I suspect it will be when mixer_left is equal to "none"--so put that last in your ConditionSwitch and have the condition just be True instead.
3) You need to put single quotes around things you want to be strings inside that ConditionSwith string, otherwise it thinks you are talking about a variable.
So this:

Code: Select all

"mixer_left == purple_chemical"
translates to: if the variable named mixer_left is equal to the variable named purple_chemical. But you don't have a variable named purple_chemical. You want to talk about strings.
This (which is what you want):

Code: Select all

"mixer_left == 'purple_chemical'"
translates to: if the variable named mixer_left is equal to the string "purple_chemical"

4) in your screen, when you declare your idle and hover images, they need to be in quotation marks because the Imagebutton object expects a string after those properties, you can imply this by looking at the documentation for Imagebutton under the first property "auto" where it reads:
Used to automatically define the images used by this button. This should be a string that contains %s in it.
idle and hover don't need the %s, they'll just have idle and hover explicitly stated, but the property still expects a string.
So put your mixer_left_idle_image in quotations.
5) That is not how you do a SetVariable action. (https://www.renpy.org/doc/html/screen_a ... etVariable)
Following the documentation it should look like so:

Code: Select all

SetVariable("mixer_left", "helditem")
But note, if you are adding to a numerical value say you have a:
default mixer_num = 0

and you want your SetVariable action to add to that number, that would look like this:

Code: Select all

action SetVariable("mixer_num", mixer_num+1)
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: ConditionSwitch and Imagebutton

#4 Post by trooper6 »

Well, you seemed to have figured most of it out while I was coding and testing for this answer. Ah well! I'll just keep it here though in case it is useful for someone else.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

terrordoll
Regular
Posts: 46
Joined: Tue Apr 11, 2017 11:57 am
Contact:

Re: ConditionSwitch and Imagebutton

#5 Post by terrordoll »

Trooper6, once again, thank you. I know I kinda blundered my way through to the solution by I really appreciate that you took the time to write out a detail answer with code. Can't tell you how awesome that is.

Hats off to you my friend!

*EDIT:The Arrangement of the ContitionSwitch and the Init block comments are both great and stuff I wasn't aware of, so thank you.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]