Using a variable as an image name

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
SoupyTheCat
Newbie
Posts: 1
Joined: Fri Apr 29, 2022 8:34 pm
Location: A very comfy chair
Discord: SoupyTheCat#6604
Contact:

Using a variable as an image name

#1 Post by SoupyTheCat » Sat Apr 30, 2022 9:15 pm

How would I use a variable as part of an image name when using the show command? For example, setting the variable score to 20 and using that to show the image score_20. Technically I could use a bunch of else/if statements, but that would get cluttered easily.
~ Soupy

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: Using a variable as an image name

#2 Post by Imperf3kt » Sat Apr 30, 2022 11:27 pm

Theres a few ways to do this.
One way you can achieve this is via ConditionSwitch.
https://www.renpy.org/doc/html/displaya ... tionSwitch

Code: Select all

image score = ConditionSwitch(
    "score == 0", "score_00.png",
    "score == 1", "score_01.png",
    "score == 2", "score_02.png")
    ##etcetera

label start:
    show score
    $score += 1
    "You have [score] point"
    $score += 1
    "You have [score] points"
    
    return
    
Or you can use python and screen language.

Code: Select all

default score = 0

screen score_screen():
    imagebuton:
        idle "images/score_{}%s.png" .format(score)
        action NullAction()

label start:
    show screen score_screen
    $score += 1
    "You have [score] point"
    $score += 1
    "You have [score] points"
    
    return
There are probably more ways, but I find either of these more than sufficient for my needs.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Using a variable as an image name

#3 Post by Ocelot » Sun May 01, 2022 3:21 am

Also: DynamicImage:

Code: Select all

image score_img = DynamicImage("score_[score].png")
< < insert Rick Cook quote here > >

Post Reply

Who is online

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