[SOLVED] If/elif always equal last branch?

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
gurophilia
Newbie
Posts: 2
Joined: Thu Jan 03, 2019 10:13 pm
Contact:

[SOLVED] If/elif always equal last branch?

#1 Post by gurophilia »

What I'm attempting to accomplish is have the player choose one of four available characters by assigning the character if the contents variable perkchoice (which is activated by clicking an imagebutton) match the assigned text (Bond, Empathy, Calm Spirit, Adrenaline).

My problem is that no matter what, character pc is always whatever is the last elif (ie. in this case, Jake). I've switched the order of the if/elif positions to make sure this the case. I've tried making individual ifs instead of an if/elif branch, and that hasn't worked. Can someone help me figure out how to fix this and why this method works incorrectly as it does?

in screens.rpy

Code: Select all

screen perkselection():
        zorder 100
        imagebutton:
            hover bondico
            idle bondico
            action SetVariable("perkchoice","Bond")
            xpos 0.5
            ypos 0.1
            focus_mask True
        imagebutton:
            hover empathyico
            idle empathyico
            action SetVariable("perkchoice","Empathy")
            xpos 0.5
            ypos 0.4
            focus_mask True
        imagebutton:
            hover calmico
            idle calmico
            action SetVariable("perkchoice","Calm Spirit")
            xpos 0.4
            ypos 0.25
            focus_mask True
        imagebutton:
            hover adrenico
            idle adrenico
            action SetVariable("perkchoice","Adrenaline")
            xpos 0.6
            ypos 0.25
            focus_mask True
in script.rpy

Code: Select all

    label lab_perksel:
        show screen perkselection

        label setchar:
            pause
            nar "Equip [perkchoice]?"
            menu:
                "Yes":
                    if perkchoice == "Bond":
                        define pc = Character("Dwight")
                        define pca = Character("Claudette")
                        define pcb = Character("Jake")
                        define pcc = Character("Meg")
                    elif perkchoice == "Empathy":
                        define pc = Character("Claudette")
                        define pca = Character("Dwight")
                        define pcb = Character("Jake")
                        define pcc = Character("Meg")
                    elif perkchoice == "Adrenaline":
                        define pc = Character("Meg")
                        define pca = Character("Dwight")
                        define pcb = Character("Jake")
                        define pcc = Character("Claudette")
                    elif perkchoice == "Calm Spirit":
                        define pc = Character("Jake")
                        define pca = Character("Dwight")
                        define pcb = Character("Claudette")
                        define pcc = Character("Meg")
                    else:
                        jump lab_perksel
                    jump scene001
                "No":
                    jump lab_perksel
Last edited by gurophilia on Tue Jan 08, 2019 12:31 pm, edited 1 time in total.

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: If/elif always equal last branch?

#2 Post by Kia »

I'm not sure what you're trying to do by looking at your code, but you can use AddToSet to make a list of choices by the player then use that list for the rest of your code.
https://www.renpy.org/doc/html/screen_a ... l#AddToSet

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: If/elif always equal last branch?

#3 Post by Alex »

It's all looks so wrong...
Do you have any default value for perkchoice variable?
All the 'default' and 'define' statements are run before main menu whereever they placed in script, so your characters will be set as if you choose 'Calm Spirit' anyway.
viewtopic.php?f=51&t=39572#p422964

If only difference is the names of characters, then you can just set names for them as variables and change them in game - viewtopic.php?f=8&t=52641&p=499608&hili ... me#p499608

Also, it might be better to call perkselection screen, so player would have to choose an option. In this case you need to add a Return() action to the screen to end interaction.

gurophilia
Newbie
Posts: 2
Joined: Thu Jan 03, 2019 10:13 pm
Contact:

Re: If/elif always equal last branch?

#4 Post by gurophilia »

Alex wrote: Fri Jan 04, 2019 4:42 pm It's all looks so wrong...
Do you have any default value for perkchoice variable?
All the 'default' and 'define' statements are run before main menu whereever they placed in script, so your characters will be set as if you choose 'Calm Spirit' anyway.
viewtopic.php?f=51&t=39572#p422964

If only difference is the names of characters, then you can just set names for them as variables and change them in game - viewtopic.php?f=8&t=52641&p=499608&hili ... me#p499608

Also, it might be better to call perkselection screen, so player would have to choose an option. In this case you need to add a Return() action to the screen to end interaction.
Thanks for the advice! I honestly didn't even think about the difference between define and assigning a new value, so thanks for pointing it out.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot]