Problem with IF sentences and menus.

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
User avatar
Avantharis
Regular
Posts: 43
Joined: Mon Feb 23, 2015 6:40 pm
Tumblr: avantharis
Deviantart: Avantharis
Contact:

Problem with IF sentences and menus.

#1 Post by Avantharis » Sun May 03, 2015 11:12 pm

Hello,

I have made a small character creation (just to train) where at one point the player must choose his skills, with the following rules

- The default number of skills is 4, each skill cost one point
- Skills are limited by class
- The player can´t choose the same skills twice.

In order to avoid the player to choose same skills twice, the game first check the status for the current skills (per exemple, the $ sk for swords), which initialy is set false. If the variable is set as false it shows on the menu if also the class match. Once the skills have been selected, it´s status change to true, therefore no longer showing on the menu.

Overall the whole thing works, except if the player choose any classe beside fighter, because in this case, its possible to choose the same skill twice and I can´t exactly figure why. I believe is because I am using on the menu, a If sentence with lots of "or" s.

Here is the part of the code where skills are selected.

Code: Select all

e "choose your Skills"

$ sk = 4 
$ sknormal = 4
$ skill =[]
$ sw = False
$ spe = False
$ ax = False
$ bw = False
$ mc = False
$ st = False
$ dg = False

if class1 == "Fighter":
    $ sk +=1
    $ sknormal +=1
if class1 == "Cleric":
    $ sk -=2
    $ sknormal -=2
if class1 == "Thief":
    $ sk -=1
    $ sknormal -=1

while skill >= sknormal:

    e "current skill points left=[sk] and current skills [skill]"

    menu:
        "Sword" if sw == False and class1 == "Fighter" or class1 == "Barbarian":
            $sk -=1
            $skill.append("Sword")
            $ sw = True
        "Spear" if spe == False and class1 == "Fighter" or class1 == "Barbarian":
            $sk -=1
            $skill.append("Spear")
            $ spe = True
        "Bow" if bw == False and class1 == "Fighter" or class1 == "Barbarian" or class1 == "Thief":
            $sk -=1
            $skill.append("Bow")
            $ bw = True
        "Axe" if ax == False and class1 == "Fighter" or class1 == "Barbarian":
            $sk -=1
            $skill.append("Axe")
            $ ax = True
        "Mace" if mc == False and class1 == "Fighter" or class1 == "Barbarian" or class1 == "Cleric" or class1 == "Thief":
            $sk -=1
            $skill.append("Mace")
            $ mc = True
        "Staff" if st == False :
            $sk -=1
            $skill.append("Staff")
            $ st = True
        "Dagger" if dg == False :
            $sk -=1
            $skill.append("Dagger")
            $ dg = True            
    if sk == 0:
        e "current skill points left=[sk] and current skills [skill]"
        jump Inventory1
Last edited by Avantharis on Sun May 03, 2015 11:27 pm, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Problem with IF sentences and menus.

#2 Post by philat » Sun May 03, 2015 11:24 pm

I'm assuming that the indentation problems are from copy pasting.

The problem looks like the order of the evaluations. if a and b or c in python is evaluated left to right, so it's effectively if (a and b) or c, as opposed to if a and (b or c).

User avatar
Avantharis
Regular
Posts: 43
Joined: Mon Feb 23, 2015 6:40 pm
Tumblr: avantharis
Deviantart: Avantharis
Contact:

Re: Problem with IF sentences and menus.

#3 Post by Avantharis » Sun May 03, 2015 11:33 pm

philat wrote:I'm assuming that the indentation problems are from copy pasting.

The problem looks like the order of the evaluations. if a and b or c in python is evaluated left to right, so it's effectively if (a and b) or c, as opposed to if a and (b or c).
Yep the indention just looked like that because of that, only now I see the "code" tag...

Thanks!

So if I got right

Code: Select all

"Bow" if bw == False and class1 == "Fighter" or class1 == "Barbarian" or class1 == "Thief":
That mean the issue is that the while does work for Fighter (since is the first after the true/false) for the rest it won´t work because is checking from left to right, therefore, first to see if fighter, then barbarian and then thief each time.

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: Problem with IF sentences and menus.

#4 Post by Onishion » Mon May 04, 2015 2:55 am

I think the easiest way to do what you want is to invert the problem.

for bow you want it to be available to fighter, barbarian and thief, but not to. . . mage or cleric, right? So why not recode it as:

Code: Select all

"Bow" if bw == False and class1 != "Mage" and class1 != "Cleric":
that way, it's all "and" conditions. You just list the things that it should not be. If it is a Thief, then it's not a Mage or a Cleric.

Like the basic problem, Philat was explaining in the way you had it written was, if you had bw= False AND Class1 was Fighter, then it would ignore everything else and move on. If that wasn't true, then it would check whether class was Barbarian, and completely ignore the "Bw" part of it. If the class wasn't Barbarian, it would check if class was Thief, and if so, again ignore anything that's come before.

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: Problem with IF sentences and menus.

#5 Post by trooper6 » Mon May 04, 2015 4:55 am

I can see two simpler options.

1) Adding parentheses:

Code: Select all

"Sword" if sw == False and (class1 == "Fighter" or class1 == "Barbarian"):
2) making your options a list:

Code: Select all

"Sword" if sw == False and class1 in ["Fighter", "Barbarian"]:
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
Avantharis
Regular
Posts: 43
Joined: Mon Feb 23, 2015 6:40 pm
Tumblr: avantharis
Deviantart: Avantharis
Contact:

Re: Problem with IF sentences and menus.

#6 Post by Avantharis » Mon May 04, 2015 9:16 am

trooper6 wrote:I can see two simpler options.

1) Adding parentheses:

Code: Select all

"Sword" if sw == False and (class1 == "Fighter" or class1 == "Barbarian"):
2) making your options a list:

Code: Select all

"Sword" if sw == False and class1 in ["Fighter", "Barbarian"]:
Yeah, now I am figuring out that there is easiers way to do it...like have different menus for each class using different labels and jumps.

Many thanks, using the list I was able to solve the problem!

Post Reply

Who is online

Users browsing this forum: Alex, Google [Bot], nyeowmi