Help with Imagebuttons and variables

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
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Help with Imagebuttons and variables

#1 Post by GeeSeki »

I'm have 3 variables that pull a random gift from a list. For example:

Code: Select all

giftoftheday_1 = "gift001"
giftoftheday_2 = "gift002"
giftoftheday_3 = "gift003"
I'm trying to get the imagebutton to display based on which gift is chosen but renpy can't read the button.

Code: Select all

screen scr_test():
    imagebutton auto "btn [giftoftheday_1]_%s" xpos 1260 ypos 120 focus_mask True action Call("lbl_buy_[giftoftheday_1]")
    imagebutton auto "btn [giftoftheday_2]_%s" xpos 1260 ypos 340 focus_mask True action Call("lbl_buy_[giftoftheday_2]")
    imagebutton auto "btn [giftoftheday_3]_%s" xpos 1260 ypos 560 focus_mask True action Call("lbl_buy_[giftoftheday_3]")
I won't get the value inside the btn name. It'll always say it can't find "btn [giftoftheday_1]_idle" rather than reading it as "btn gift001_idle"

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

Re: Help with Imagebuttons and variables

#2 Post by Ocelot »

String substitution does not work in filenames. You need to construct string manually:

Code: Select all

screen scr_test():
    python:
        gotd_file1 = "btn " + giftoftheday_1 + "_%%s" #Use double percentage sign to avoid treating it as a substitution marker
        gotd_file2 = "btn " + giftoftheday_2 + "_%%s"
        gotd_file3 = "btn " + giftoftheday_3 + "_%%s"
    imagebutton auto gotd_file1  xpos 1260 ypos 120 focus_mask True
IIRC text suibstitution won't work on label names either, so you will propably have to do same thing for Call actions.
< < insert Rick Cook quote here > >

User avatar
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Re: Help with Imagebuttons and variables

#3 Post by GeeSeki »

Ocelot wrote: Thu Apr 15, 2021 6:36 am String substitution does not work in filenames. You need to construct string manually:

Code: Select all

screen scr_test():
    python:
        gotd_file1 = "btn " + giftoftheday_1 + "_%%s" #Use double percentage sign to avoid treating it as a substitution marker
        gotd_file2 = "btn " + giftoftheday_2 + "_%%s"
        gotd_file3 = "btn " + giftoftheday_3 + "_%%s"
    imagebutton auto gotd_file1  xpos 1260 ypos 120 focus_mask True
IIRC text suibstitution won't work on label names either, so you will propably have to do same thing for Call actions.
Thank you for that! That worked but I also needed to have the buttons resized so I used this instead:

Code: Select all

screen scr_test():
    imagebutton auto "btn giftoftheday_1_%s" xpos 1260 ypos 120 focus_mask True action [SetVariable("giftclick",1), Call("lbl_buy_gift")]
    imagebutton auto "btn giftoftheday_2_%s" xpos 1260 ypos 340 focus_mask True action [SetVariable("giftclick",2), Call("lbl_buy_gift")]
    imagebutton auto "btn giftoftheday_3_%s" xpos 1260 ypos 560 focus_mask True action [SetVariable("giftclick",3), Call("lbl_buy_gift")]
    
    image btn giftoftheday_1_hover:
        "//images/gifts/btn_giftitem_[giftoftheday_1]_hover.png"
        zoom 0.5
    image btn giftoftheday_1_idle:
        "//images/gifts/btn_giftitem_[giftoftheday_1]_idle.png"
        zoom 0.5
    image btn giftoftheday_2_hover:
        "//images/gifts/btn_giftitem_[giftoftheday_2]_hover.png"
        zoom 0.5
    image btn giftoftheday_2_idle:
        "//images/gifts/btn_giftitem_[giftoftheday_2]_idle.png"
        zoom 0.5
    image btn giftoftheday_3_hover:
        "//images/gifts/btn_giftitem_[giftoftheday_3]_hover.png"
        zoom 0.5
    image btn giftoftheday_3_idle:
        "//images/gifts/btn_giftitem_[giftoftheday_3]_idle.png"
        zoom 0.5
        
label lbl_buy_gift:
    if giftclick == 1:
        $ gift_inventory.append(giftoftheday_1)
        $ money -= giftprice_1
    elif giftclick == 2:
        $ gift_inventory.append(giftoftheday_2)
        $ money -= giftprice_2
    else:
        $ gift_inventory.append(giftoftheday_3)
        $ money -= giftprice_3

    call screen scr_market_sequence

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]