How to change an Image variable in the middle of 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
Yuvan raj
Regular
Posts: 38
Joined: Wed Feb 17, 2021 11:24 am
Contact:

How to change an Image variable in the middle of label?

#1 Post by Yuvan raj »

Hello, i'm trying to create a game where an image is shown first on the screen, then the player has to select the arrow keys to change the image colour that is assigned for each arrow keys. But I don't know how to declare the image variable. I somehow made i work yesterday but now, I'ts not working. And the circle image is Red colour from the start instead of being yellow. I would be very thankful if you could tell me the various methods to declare an image (like 'default', $ , 'image') and whey my circle is red colour from the get go. And also the colour of the circle is not changing when I click the arrow keys. What am I doing wrong?

This is my script code:

Code: Select all

# The script of the game goes in this file.

# Declare characters used by this game. The color argument colorizes the
# name of the character.

define e = Character("Eileen")

default Red = False

default Blue = False

image arrow_down = "down_arrow.png"

image arrow_up = "up_arrow.png"

image circle = "yellow_circle"

# The game starts here.

label start:

    # Show a background. This uses a placeholder by default, but you can
    # add a file (named either "bg room.png" or "bg room.jpg") to the
    # images directory to show it.

    scene bg room

    # This shows a character sprite. A placeholder is used, but you can
    # replace it by adding a file named "eileen happy.png" to the images
    # directory.

    show eileen happy


    # These display lines of dialogue.

    e "You've created a new Ren'Py game."

    e "Once you add a story, pictures, and music, you can release it to the world!"

    hide eileen

    show circle at truecenter

    "This is to test the key statement"

    "choose"
    window hide

    show screen keyscreen
    show screen arrowscreen
    pause


    if Blue == True:
        image circle = "blue_circle"
    elif Red == True:
        image circle = "red_circle"


    window show

    "The game ends"
    pause

    # This ends the game.

    return
This is my screen code:

Code: Select all

screen keyscreen():
    key "K_UP" action (SetVariable("Blue", True), Hide('keyscreen'), Hide('arrowscreen'))

    key "K_DOWN" action (SetVariable("Red", True), Hide('keyscreen'), Hide('arrowscreen'))


screen arrowscreen():

    vbox:
        xalign 0.8
        yalign 0.5
        spacing 50
        imagebutton idle "up_arrow" action NullAction()
        imagebutton idle "down_arrow" action NullAction()
This is what the game shows(the circle is red colour instead of yellow)

Image

Then the screen is shown
Image

The colour should have changed here(the colour should have been yellow from the start but even if it was red, it should have changed into blue if I had clicked the up arrow but it didn't)
Image

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

Re: How to change an Image variable in the middle of label?

#2 Post by Alex »

The circle is red 'cause image declaration code runs before the game starts and it doesn't matter where you put this code. The last line in your code is

Code: Select all

image circle = "red_circle"
Try to use ConditionSwitch to make image change depend of variable's value.
https://www.renpy.org/doc/html/displaya ... tionSwitch
viewtopic.php?f=51&t=19063#p246889

viewtopic.php?f=51&t=39572#p422964

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
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 an Image variable in the middle of label?

#3 Post by Remix »

With your key actions, both variables can be True, which seems suspect.

I would suggest a dynamic image that responds to a single variable...

Code: Select all

default circle_color = "yellow"

image circle = "[circle_color]_circle" ## the [] part makes this respond dynamically to changes in the variable value

label start:
    show circle at truecenter
    pause
    $ circle_color = "red"
    pause
    $ circle_color = "blue"
    pause
    $ circle_color = "yellow"
Frameworks & Scriptlets:

Yuvan raj
Regular
Posts: 38
Joined: Wed Feb 17, 2021 11:24 am
Contact:

Re: How to change an Image variable in the middle of label?

#4 Post by Yuvan raj »

Alex wrote: Sun May 02, 2021 4:19 am The circle is red 'cause image declaration code runs before the game starts and it doesn't matter where you put this code. The last line in your code is

Code: Select all

image circle = "red_circle"
Try to use ConditionSwitch to make image change depend of variable's value.
https://www.renpy.org/doc/html/displaya ... tionSwitch
viewtopic.php?f=51&t=19063#p246889

viewtopic.php?f=51&t=39572#p422964
Remix wrote: Sun May 02, 2021 5:30 am With your key actions, both variables can be True, which seems suspect.

I would suggest a dynamic image that responds to a single variable...

Code: Select all

default circle_color = "yellow"

image circle = "[circle_color]_circle" ## the [] part makes this respond dynamically to changes in the variable value

label start:
    show circle at truecenter
    pause
    $ circle_color = "red"
    pause
    $ circle_color = "blue"
    pause
    $ circle_color = "yellow"

Thank you very much for the reply! Both of your code works perfectly. The condition switch method instantly changes the colour once I press the arrow keys while the single variable method changes after a click. Think the key hides the screen then the click goes to the conditions. I added the Call statement to the keys and and created a new label with only the conditions which changes the colour instantly. Again thank you both for taking your time and answering me. Now I know two methods to do this thing. Again Thank you!

Code: Select all

screen keyscreen():

    modal True

    key "K_UP" action (SetVariable("Blue", True), Hide('keyscreen'), Hide('arrowscreen'), Call("condition"))

    key "K_DOWN" action (SetVariable("Red", True), Hide('keyscreen'), Hide('arrowscreen'), Call("condition"))

################################# new label##################################################
label condition:
    if Blue == True:
        $ circle_color = "blue"

    elif Red == True:
        $ circle_color = "red"

    return
################################# new label##################################################

screen arrowscreen():

    vbox:
        xalign 0.8
        yalign 0.5
        spacing 50
        imagebutton idle "up_arrow" action NullAction()
        imagebutton idle "down_arrow" action NullAction()

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]