[SOLVED] Flags and If Statement Issues

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.
Message
Author
User avatar
Ramunai
Regular
Posts: 26
Joined: Fri Jun 07, 2013 1:19 am
Projects: Love Target!
Location: Wonderland
Contact:

[SOLVED] Flags and If Statement Issues

#1 Post by Ramunai »

I've just started using Ren'Py about a week ago and I'm currently having some problems with the If statement. I'm not really good with technology and stuff so it's really hurting my head trying to figure out what's wrong so help (and easy explanations) would much be appreciated (Q___Q)

When I first used the flag ($) for the first menu I typed and then the 'If' statement for whichever chosen option everything went well, but when it came to the following branches from the first 'If' statement, I kept getting errors like a '____' not being defined although I've already defined it in this way:

Code: Select all

$ ____ = "True"
This is the error that I keep getting:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 542, in script
  File "game/script.rpy", line 542, in python
NameError: name 'mettoushi' is not defined

A Ren'Py Game 0.0
I followed tutorials and all so I really don't know what I'm doing wrong here...please help?

PS: I can send you the whole WIP script in note format if you'd like (Yes, I'm that desperate to know what I've done wrong and to set things right OTL)
Last edited by Ramunai on Tue Jun 25, 2013 11:34 pm, edited 1 time in total.

User avatar
Suwamoto
Regular
Posts: 66
Joined: Wed Sep 05, 2012 7:36 am
Contact:

Re: [Help!] Flags and If Statement Issues

#2 Post by Suwamoto »

Try to either put them under an init block like this

Code: Select all

init:
    $ variable = True
Or declare them at the beginning of label start o.o

User avatar
Ramunai
Regular
Posts: 26
Joined: Fri Jun 07, 2013 1:19 am
Projects: Love Target!
Location: Wonderland
Contact:

Re: [Help!] Flags and If Statement Issues

#3 Post by Ramunai »

Suwamoto wrote:Try to either put them under an init block like this

Code: Select all

init:
    $ variable = True
Or declare them at the beginning of label start o.o
OMG thank you! It worked! I'm not getting that error trace back anymore~ But, I now have another issue...after I put them under an init block, what about the 'If' Statement? I ran the game but it showed all the following scenes no matter what option initially picked at the previous menu...OTL

EDIT: Also, if there a way just to use the normal flag and 'If' statement for the whole script?
Ramunai [ラムない]
WIP: Love Target!

RangerDanger
Regular
Posts: 45
Joined: Wed Jun 27, 2012 11:36 pm
Contact:

Re: [Help!] Flags and If Statement Issues

#4 Post by RangerDanger »

Ramunai wrote:
Suwamoto wrote:Try to either put them under an init block like this

Code: Select all

init:
    $ variable = True
Or declare them at the beginning of label start o.o
OMG thank you! It worked! I'm not getting that error trace back anymore~ But, I now have another issue...after I put them under an init block, what about the 'If' Statement? I ran the game but it showed all the following scenes no matter what option initially picked at the previous menu...OTL
Are you using the values True and False or "True" and "False"?

If the latter, then you should be using the former.

User avatar
Ramunai
Regular
Posts: 26
Joined: Fri Jun 07, 2013 1:19 am
Projects: Love Target!
Location: Wonderland
Contact:

Re: [Help!] Flags and If Statement Issues

#5 Post by Ramunai »

RangerDanger wrote: Are you using the values True and False or "True" and "False"?

If the latter, then you should be using the former.
When I use the normal flag ($ variable = True) and the 'If' statement, I've tried with and without the quote marks but the same error of the variable not being defined just pops up...
And when I use the init block, it showed all the following scenes no matter what option initially picked at the previous menu.
Could I perhaps just send you part of the script in note format so you could check? I have a feeling that I'll be troubling you and other users more if I continue trying to solve this by myself, but it's alright if you're busy...! I've already taken enough people's time with this issues of mine.
Ramunai [ラムない]
WIP: Love Target!

User avatar
Suwamoto
Regular
Posts: 66
Joined: Wed Sep 05, 2012 7:36 am
Contact:

Re: [Help!] Flags and If Statement Issues

#6 Post by Suwamoto »

Just post the script on here o.o That would make it easier XD

User avatar
Ramunai
Regular
Posts: 26
Joined: Fri Jun 07, 2013 1:19 am
Projects: Love Target!
Location: Wonderland
Contact:

Re: [Help!] Flags and If Statement Issues

#7 Post by Ramunai »

Suwamoto wrote:Just post the script on here o.o That would make it easier XD
Alright! Though it'll be quite a long one since I don't where I went wrong. Sorry for troubling all of you (Q____Q)

Here's the script:

Code: Select all

 EDIT: This huge block of script has been removed due to Ramunai's paranoia x'D 
I've been cracking my head with this for days before I gave up and came here to seek for help OTL
Last edited by Ramunai on Tue Jun 25, 2013 11:36 pm, edited 1 time in total.
Ramunai [ラムない]
WIP: Love Target!

pwisaguacate
Veteran
Posts: 356
Joined: Mon Mar 11, 2013 11:03 pm
Contact:

Re: [Help!] Flags and If Statement Issues

#8 Post by pwisaguacate »

Ramunai wrote:
Suwamoto wrote:Try to either put them under an init block like this

Code: Select all

init:
    $ variable = True
Or declare them at the beginning of label start o.o
OMG thank you! It worked! I'm not getting that error trace back anymore~ But, I now have another issue...after I put them under an init block, what about the 'If' Statement? I ran the game but it showed all the following scenes no matter what option initially picked at the previous menu...OTL

EDIT: Also, if there a way just to use the normal flag and 'If' statement for the whole script?
I'm hoping you didn't literally initialize all of them to True, since that would make the condition checks return True thus enabling the scenes.

Code: Select all

init:
    $ myVariable1 = False # For booleans that stay False until toggled True
    $ myVariable2 = "" # For strings that will be given a value later

User avatar
Suwamoto
Regular
Posts: 66
Joined: Wed Sep 05, 2012 7:36 am
Contact:

Re: [Help!] Flags and If Statement Issues

#9 Post by Suwamoto »

The way you've got it right now is in short this:

Code: Select all

label start:
    menu what_to_do:
        "Do Homework":
            $ homework = "done"
        "Don't do Homework":
            $ homework = "not done"
    
    # In this case, the variable "$ homework" exists for sure, because you had to make a choice before this,
    # and no matter what your choice was, a variable with the name "homework" was created
    
    if homework == "done":
        # Here you create the variable "$ mettoushi" for the first time, but only elements of this block will know it
        # This block can only be reached if you have done your homework
    
        $ mettoushi = True
    else:
        # Same as with "$ mettoushi"
        
        $ methiro = True
    
    # Everything under here is in a different block than the if statements above
    # So if you did your homework, you created the variable "$ mettoushi = True" but never created the variable "$ methiro = True"
    # Since in both cases, a variable is missing, Ren'Py will give you an error
    
    if mettoushi == True:
        # I know mettoushi but Idk  methiro so I will give an error after this
    
    if methiro == True:
        # I know methiro but Idk mettoushi so I will give an error before this
I tried to explain XD' *fails at it*
To fix it, simply declare all variables before you start the game. Like this:

Code: Select all

# Set them to None and False so the stuffs don't happen if they are True
init:
    $ homework = None
    $ methiro = False
    $ mettoushi = False
    
label start:
    menu what_to_do:
        "Do Homework":
            $ homework = "done"
        "Don't do Homework":
            $ homework = "not done"
    
    # And the whole rest.... etc.
I think I wrote to much XD' *shot*
Hope it kinda helped you understand that better though.

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: [Help!] Flags and If Statement Issues

#10 Post by Anima »

You shouldn't declare variables in the init block, the changes won't be saved.
Declare them right under the start label.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

pwisaguacate
Veteran
Posts: 356
Joined: Mon Mar 11, 2013 11:03 pm
Contact:

Re: [Help!] Flags and If Statement Issues

#11 Post by pwisaguacate »

Anima wrote:[...]
[Removed]

EDIT: Ah. appending to lists... I'm still a confused failure, so I'm just going to drop it for awhile.
Last edited by pwisaguacate on Tue Jun 25, 2013 5:28 pm, edited 2 times in total.

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: [Help!] Flags and If Statement Issues

#12 Post by Anima »

They are not saved if the changes are in place.
If you have something like:
$ flag = True
it will be saved, since it's pretty much a new declaration.
But if you have a list for example and append things to it, the changes won't survive restarting the game.

So it's a good idea to keep all variable declaration (constants are a different matter) out of init blocks.
And yes I tested it just now with the current version, so it hasn't changed.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

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: [Help!] Flags and If Statement Issues

#13 Post by trooper6 »

I was looking through the documentation about saving variable data during game saves and rollbacks and the like, and I think I understand the difference...but I'd love to have you veterans confirm:

Code: Select all

init:
    $ cookies = 2
    $ cakes = 1
    $ poisons = 10

label start:
    
    "Mary Goody-TwoShoes looked in her picnic basket...she found..."
So, if I understand correctly, in this instance, no variables are saved on the saved game and rollback data. Though...a question I have is...wouldn't you still have the initial values from the init block when the game is started up?

Code: Select all

init:
    $ cookies = 2
    $ cakes = 1
    $ poisons = 10

label start:
    $ cookies = 1
    "Mary Goody-TwoShoes looked in her picnic basket...she found..."
    $ cookies = 0
    $ poison = 21
So in this instance if you save right before the "Mary Goody-TwoShoes" line the only data you save is cookies = 1, if you save at the end of the block, your saved variables are cookies = 0 and poison = 21. Right? But when you load the game, would the cake still be at default of 1?

My last question on this matter is, with this code:

Code: Select all

label start:
    $ cookies = 2
    $ cakes = 1
    $ poisons = 10

    "Mary Goody-TwoShoes looked in her picnic basket...she found..."

label grandma:

    if cookies == 0:
        m "Grandma, have some poison...um...cake."
Will that work? In other words, if you declare a variable in a start label block, can you refer to it in a different label block? Or, if you declare it in the start block, can you only refer to it in the start block? Because if so, it would make sense to declare in an init block so you can access the data in all blocks.

Lists seem to be the big red flag since appending seems to work differently than declaring...so none of that would be changed in saves. But generally, can you access the data declared under one label in a different label if the item wasn't declared in an init block?

I'd try it out myself, but I'm at work and away from Ren'Py!

Veterans, any insight in the details on this?
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

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: [Help!] Flags and If Statement Issues

#14 Post by Anima »

The init blocks are executed every time you start the program, so yes for 1.
So the same goes for 2.
3 will work as well. You can access them from any label. Just pay attention to the order, since you can't use a variable that's declared in a label that has not run yet.

There is also a different reason for not declaring in the init section.

Code: Select all

init:
    $ hitpoints = 100

label start:
    "A bug attacks! You loose 90 hitpoints!"
    $ hitpoints -= 90
    if hitpoints > 0:
        "Run for your life!"
    else:
        "Don't start a new game without restarting the program."
You can probably guess what happens. Bottom line, don't declare variables in init blocks, it probably will come back to bit you in various body parts.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

pwisaguacate
Veteran
Posts: 356
Joined: Mon Mar 11, 2013 11:03 pm
Contact:

Re: [Help!] Flags and If Statement Issues

#15 Post by pwisaguacate »

I'm sorry for causing such a hullabaloo. I'm not what anyone should call a "veteran", ha.

No matter what I did, I couldn't get hitpoints from the above post to accidentally become -80 or reset to 100.

I wrote the script below, to test variables placed in an init block, and observed. myString behaved as expected. Saving retains its value, and saves are independent from each other. Starting a new game, whether or not Ren'Py was restarted, will reset the value to "default".

However, Anima was right about lists. myList acted bizarrely! It did not respect saves, i.e. loading from anywhere will display the same list of values. When a new game is started without restarting Ren'Py, the same list still shows. When Ren'Py is restarted, it resets to ["default"].

(Ugh, I didn't include numbers.)

Anyways, placing all non-constants after the start label does look better to me now.

Code: Select all

init:
    myString = "default"
    myList = ["default"]

label start:
    "DEMO: testing the saving of variables declared within in init block"

    menu:
        "Pick which variable to deal with."

        "myString":
            jump test_string

        "myList":
            jump test_list

label test_string:
    menu:
        "Set value of myString":
            $ myString = renpy.input("Enter value for myString:")

        "Pass (do nothing)":
            pass

    "myString = \"[myString]\""

    jump end

label test_list:
    menu:
        "Append value to myList":
            $ myList.append(renpy.input("Enter value append to myList:"))

        "Remove value from myList":
            $ remover = renpy.input("Enter value to remove from myList:")

            if remover in myList:
                $ myList.remove(remover)

            else:
                "ValueError: list.remove(x): x not in list"

        "Pass (do nothing)":
            pass

    "myList = [myList]"

    jump end

label end:
    menu:
        "Jump back to start":
            jump start

        "Return to main menu":
            return

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Milkymalk