I can't use if statement to perform an action (Jump label)

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
starlingmaximilian
Newbie
Posts: 15
Joined: Mon Oct 14, 2019 4:07 pm
Contact:

I can't use if statement to perform an action (Jump label)

#1 Post by starlingmaximilian »

HI, I'm new at coding in general (4 days old at renpy).
I'm trying to make a navigation HUD (suing screens) that uses arrows to move to different parts of a room. I wan't to have just one screen with multiple if arguments, so pressing the same arrow of the HUD directs the player to different locations based on where he is.
To make it short:
1_ If the player is in the bathroom and clicks on the left arrow, I wan't him to go to th eliving.
2_ If the player is in the exit and clicks on the left arrow, I wan't him to go to the bathroom.

I tried using if statements to accomplish this but I'm failing (I read the documentation in the wiki by the way, but I still don't know what I'm doing wrong). Here's the code.

Code: Select all

define y = ("Yukio")
label start:
define location = 1
    scene bathroom

    y "Where am I?"
    y "What happened?"

screen movement():
    frame:
        xalign 1.0 ypos 400
        imagebutton:
            idle "arrowPasive.png"
            if location = 1:
                action Jump("exit")

    frame:
        xalign 0.0 ypos 400
        imagebutton:
            idle "arrowPasive.png"
            if location = 1:
                action Jump("living")


call screen movement

label living:
    scene living
    y "It is night already..."

label exit:
    scene exit
    y "I need to find a way to open this door"
I could make a screen per each label, but I don't think that's efficient coding-wise, and I want to learn good practices.
Please help me make this If statement work guys.

User avatar
Enviel
Newbie
Posts: 11
Joined: Sat Apr 12, 2014 2:55 pm
Contact:

Re: I can't use if statement to perform an action (Jump label)

#2 Post by Enviel »

When comparing values, you need to use == instead of =. A single = means that you set value of something, while == means testing for equality. Also, you'll want to use keyword default instead of define for the variable location. Define is for values that don't change, saving and loading will cause problems if you do change them.

Code: Select all

default location = 1

Code: Select all

if location == 1:
Another approach would adding parameters to the screen like this:

Code: Select all

define y = ("Yukio")

screen movement(leftLocation, rightLocation):
    frame:
        xalign 1.0 ypos 400
        textbutton "[rightLocation]" action Jump(rightLocation) #Using textbutton just for simplicity
        # "[variableName]" inserts the variable to the text, in this case the button text

    frame:
        xalign 0.0 ypos 400
        textbutton "[leftLocation]" action Jump(leftLocation)


label start:
    scene start
    y "Where am I?"
    y "What happened?"
    call screen movement("living", "exit") # Names of the labels you can go to from this room

label living:
    scene living
    y "It is night already..."
    call screen movement("exit", "start")

label exit:
    scene exit
    y "I need to find a way to open this door"
    call screen movement("start", "living")
One advantage of this way is that you don't have to add a lot of ifs to the screen, which makes it a lot cleaner. Choosing what method to use comes down to preference though, anything that works is good.

starlingmaximilian
Newbie
Posts: 15
Joined: Mon Oct 14, 2019 4:07 pm
Contact:

Re: I can't use if statement to perform an action (Jump label)

#3 Post by starlingmaximilian »

Hey! Thank you so much for your help! I changed = to == and it worked!! ALso, thank you for the advice on define and default!
I didn't know I could add parameters to screens, it is much better than what I was trying to do, and it is in fact what I want to learn, good practices.
I'm having another problem now. The code is as follows:

Code: Select all

define y = ("Yukio")

screen movement(leftLocation, rightLocation):
        frame:
            xalign 0.0 ypos 400
            imagebutton:
                idle "arrowPasive.png"
                action Jump("leftLocation")
        frame:
            xalign 1.0 ypos 400
            imagebutton:
                idle "arrowPasive.png"
                action Jump("rightLocation")

label start:
scene bathroom
y "Where am I?"
y "What happened?"
call screen movement("living","exit") // THIS IS THE PROBLEM LINE

label living:
scene living
y "It is night already..."
call screen movement("kitchen","start")

label exit:
scene exit
y "I need to find a way to open this door"
call screen movement("start","kitchen")

label kitchen:
scene kitchen
call screen movement("exit","living")
y "This is a mess"
y "The rests on the plates are recent"
Now I can't get out from the first label. When I click on the left arrow renpy gives me this error:

Code: Select all

While running game code:
File "game/script.rpy", line 19, in script
call screen movement("living","exit")
ScriptError: could not find label 'leftLocation'.
I really don't have a clue what I'm doing wrong!

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: I can't use if statement to perform an action (Jump label)

#4 Post by hell_oh_world »

starlingmaximilian wrote: Tue Oct 15, 2019 12:50 am While running game code:
File "game/script.rpy", line 19, in script
call screen movement("living","exit")
ScriptError: could not find label 'leftLocation'.
[/code]

I really don't have a clue what I'm doing wrong!
Remove the quotes...

Code: Select all

define y = ("Yukio")

screen movement(leftLocation, rightLocation):
        frame:
            xalign 0.0 ypos 400
            imagebutton:
                idle "arrowPasive.png"
                action Jump(leftLocation) # remove quotes
        frame:
            xalign 1.0 ypos 400
            imagebutton:
                idle "arrowPasive.png"
                action Jump(rightLocation) # remove quotes

Just for you to know, having words quoted inside is different from unquoted words, if you stated in the code Jump("leftLocation") the words inside the quotes is treated as a string (a data type that is meant for words or text) so basically the parameter for the Jump() function requires a string, and since you inputted it with quotes it will find the "label leftLocation:".
On the other hand, words without quotes are usually treated as a variable name, and the leftLocation on the parameter of the screen is a local variable that stores the passed arguments when you called the screen. So technically speaking leftLocation is a string itself with its value depending on the passed parameter.
Hope that makes sense, in case only that if you're not aware of course.

starlingmaximilian
Newbie
Posts: 15
Joined: Mon Oct 14, 2019 4:07 pm
Contact:

Re: I can't use if statement to perform an action (Jump label)

#5 Post by starlingmaximilian »

It works!!! Thank you so much!
Regarding quotes. I know about that, but I was really confused because I don't quite get why sometimes quotes are used and sometimes not.
For example I totally get why they aren't used here:

Code: Select all

 screen movement(leftLocation, rightLocation): 
But I don't get why they are used here

Code: Select all

 idle "arrowPasive.png" 
Here I'm calling a file inside a folder... Why is the file name stored as a string instead of a (non string?) variable?
I guess I'll get the hang of it with time.
Thanks again for the help, you don't know how happy I am to be able to navigate my room!

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: I can't use if statement to perform an action (Jump label)

#6 Post by hell_oh_world »

starlingmaximilian wrote: Tue Oct 15, 2019 7:06 pm It works!!! Thank you so much!
Regarding quotes. I know about that, but I was really confused because I don't quite get why sometimes quotes are used and sometimes not.
For example I totally get why they aren't used here:

Code: Select all

 screen movement(leftLocation, rightLocation): 
But I don't get why they are used here

Code: Select all

 idle "arrowPasive.png" 
Here I'm calling a file inside a folder... Why is the file name stored as a string instead of a (non string?) variable?
I guess I'll get the hang of it with time.
Thanks again for the help, you don't know how happy I am to be able to navigate my room!
Well, the thing about that is youre specifying a file inside the folder images, which is a relative folder to the current game directory. Paths and directories in renpy are always stated in a string format, though if you want you can also set a string variable that you will call instead of the long name of the path to the file.

Code: Select all

screen sample:
	default aP = "arrowPassive.png"
	imagebutton:
		idle aP
or

Code: Select all

image aP = "arrowPassive"
...
screen sample:
	imagebutton:
		idle "aP"
Variables only serve as a container for a thing that you want to store. It is very useful especially if you're dealing with repetitive usage of that particular thing. So in the example, having the "arrowPassive.png" contained in the string variable aP, you can now easily use the image many times inside the screen by only calling aP instead of the specific string "arrowPassive.png". I don't know if that makes sense, but I know you'll get along with it somehow. If I can give you a piece of advice, you should try to read about some documentations in python variables so that you'll be familiarized more, because these things actually helped me get easily going with renpy.

starlingmaximilian
Newbie
Posts: 15
Joined: Mon Oct 14, 2019 4:07 pm
Contact:

Re: I can't use if statement to perform an action (Jump label)

#7 Post by starlingmaximilian »

Paths and directories in renpy are always stated in a string format
Thank you very much for the help. When things seem to be arbitrary it is really usefull to dispell the fog with information!
Starting to understand everything you write is a must, but when you start you (sadly) use "formulas" that work and don't think deeply into what is exactly what you are doing, but in order to be good (or at least decent) you can't just do that! Thanks man.

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot]