How to change scenes through Variables and Screens?

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
Tritonials
Newbie
Posts: 16
Joined: Fri Sep 21, 2018 6:32 pm
Completed: None. Still learning how to code.
Contact:

How to change scenes through Variables and Screens?

#1 Post by Tritonials » Sun Jul 14, 2019 1:19 am

Hello, I need to change the background using screens and variables.

Code: Select all

screen jx:
    add "images/date.png" xalign 0.5 yalign 0.0
    if sun == True:
        $ classroom2 = 'images/classroomday.jpg'
        add 'images/sun_icon.jpg' xalign 1.0 yalign 0.0
    else:
        $ classroom2 = 'images/clasroomnight.jpg'
        add 'images/moon_icon.jpg' xalign 1.0 yalign 0.0

    text "Monday" xalign 0.5 ypos 20

$ sun = True
$ classroom2 = 'images/classroomday.jpg'
image classroom = '[classroom2]'

label start:
    show screen jx
    scene classroom2
    e "bla bla bla"
    e "bla bla bla"
    $ sun = False # background is now supposed to turn to night, but it doesn't.
    e "It's nighttime."

return
So what I was trying to say was I need the background to also change to night when I set the variable "sun" to False and
that activates the background and my sun icon to change. However, The background doesn't change but the sun icon does. I tried everything I learned from Ren'Py and python and honestly this is my last choice so please any help would be kindly appreciated. :(

User avatar
Per K Grok
Miko-Class Veteran
Posts: 674
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1
Projects: Defenders of Adacan Part 2
itch: per-k-grok
Location: Sverige
Contact:

Re: How to change scenes through Variables and Screens?

#2 Post by Per K Grok » Sun Jul 14, 2019 2:06 am

Tritonials wrote:
Sun Jul 14, 2019 1:19 am
Hello, I need to change the background using screens and variables.

Code: Select all

screen jx:
    add "images/date.png" xalign 0.5 yalign 0.0
    if sun == True:
        $ classroom2 = 'images/classroomday.jpg'
        add 'images/sun_icon.jpg' xalign 1.0 yalign 0.0
    else:
        $ classroom2 = 'images/clasroomnight.jpg'
        add 'images/moon_icon.jpg' xalign 1.0 yalign 0.0

    text "Monday" xalign 0.5 ypos 20

$ sun = True
$ classroom2 = 'images/classroomday.jpg'
image classroom = '[classroom2]'

label start:
    show screen jx
    scene classroom2
    e "bla bla bla"
    e "bla bla bla"
    $ sun = False # background is now supposed to turn to night, but it doesn't.
    e "It's nighttime."

return
So what I was trying to say was I need the background to also change to night when I set the variable "sun" to False and
that activates the background and my sun icon to change. However, The background doesn't change but the sun icon does. I tried everything I learned from Ren'Py and python and honestly this is my last choice so please any help would be kindly appreciated. :(
Try this

Code: Select all


screen jx:
    add "images/date.png" xalign 0.5 yalign 0.0
    if sun == True:
        add 'images/sun_icon.jpg' xalign 1.0 yalign 0.0
    else:
        add 'images/moon_icon.jpg' xalign 1.0 yalign 0.0

    text "Monday" xalign 0.5 ypos 20

default sun = True
image classroom2 = 'images/classroomday.jpg'
image classroom = 'images/clasroomnight.jpg'

label start:
    show screen jx
    scene classroom2
    e "bla bla bla"
    e "bla bla bla2"
    $ sun = False 
    scene classroom
    e "It's nighttime."

return



User avatar
IrinaLazareva
Veteran
Posts: 374
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: IKG
Location: St.Petersburg, Russia
Contact:

Re: How to change scenes through Variables and Screens?

#3 Post by IrinaLazareva » Sun Jul 14, 2019 3:41 am

Code: Select all

default  sun = True

layeredimage classroom:
    if sun:
        'classroomday.jpg'
    else:
        'clasroomnight.jpg'
        
screen jx:
    add "date.png" xalign 0.5 yalign 0.0
    if sun:
        add 'sun_icon.jpg' xalign 1.0 yalign 0.0
    else:
        add 'moon_icon.jpg' xalign 1.0 yalign 0.0
    text "Monday" xalign 0.5 ypos 20

label start:
    show screen jx
    scene classroom
    e "bla bla bla"
    e "bla bla bla"
    $ sun = False # background is now supposed to turn to night.
    e "It's nighttime."
    return
https://renpy.org/doc/html/layeredimage ... red-images
https://renpy.org/doc/html/layeredimage.html#if
There is a light at the end of the tunnel, just pray it's not a train.
___
TODO: unresolved/нерешённое

User avatar
Remix
Eileen-Class Veteran
Posts: 1410
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to change scenes through Variables and Screens?

#4 Post by Remix » Sun Jul 14, 2019 4:33 am

In my experience, the easiest approach is to use a string value in your variable and interpolate it in the image (thus automatically making it dynamic)

Code: Select all

default time_of_day = "day" # or "night" or whatever

image classroom = "images/clasroom[time_of_day].jpg"
image sun_moon_icon = "images/[time_of_day]_icon.jpg"

label start:

    scene classroom

    "..."

    $ time_of_day = "night"

    "..."

Crazy Li
Regular
Posts: 112
Joined: Fri Jan 03, 2014 3:35 pm
Contact:

Re: How to change scenes through Variables and Screens?

#5 Post by Crazy Li » Mon Jul 15, 2019 3:35 pm

I also do what Remix said as much as possible, making my variable values match something in my file names. Fitted specifically to the OP example, I think it would be fashioned something like...

Code: Select all

screen jx:
    add "images/date.png" xalign 0.5 yalign 0.0
    add "images/[time]_icon.jpg" xalign 1.0 yalign 0.0 # you will need your icons to be day_icon.jpg and night_icon.jpg instead for this to work
    text "Monday" xalign 0.5 ypos 20

$ time = "day"
image classroom = "images/classroom[time].jpg"

label start:
    show screen jx
    scene classroom
    e "bla bla bla"
    e "bla bla bla"
    $ time = "night"
    e "It's nighttime."

return

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], hell_oh_world