Variable not interpolating in imagebutton string

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
pastblasters
Newbie
Posts: 8
Joined: Fri Mar 16, 2018 3:36 am
Contact:

Variable not interpolating in imagebutton string

#1 Post by pastblasters »

Fairly experienced coder, Renpy has been somewhat confusing so far but am starting to get a pretty good handle on how it interacts with actual Python code (which has made me far more comfortable). I encountered my first error tonight that I can't seem to figure out.

I am building out an inventory screen. The InventoryManager is a defined class with an inventory dictionary as a property and some methods for adding InventoryItems to and taking them away from the dictionary. The inventory screen contains a grid, and I iterate through the inventory dictionary keys (which are the item names) to obtain all of the items and populate the grid with the associated images (each InventoryItem has a file string field). I then populate the empty grid spaces with empty strings (so as not to get the unfilled grid error):

Code: Select all

grid 5 2 xpos 137 ypos 335:
        $ total = len(inventory.inventory.keys())
        for key in inventory.inventory.keys():
            $ img = str(inventory.inventory[key].image)
            imagebutton auto "[img]_%s.png" action Null
        $ rem = 10 - total
        for i in range(0, rem):
            text "{size=+10}{font=architect.ttf}{/font}{/size}"
However, when I run the program, I get an "Imagebutton does not have a idle image. (auto=u"[img]_%s")" error. It is apparently interpreting the "[img]" bracket interpolation as simply being the name of the file. I have tested the interpolation with a standard image, and it works fine. I'm fairly sure that the issue stems from the combination of the renpy bracket interpolation and the string substitution (%s), but I'm not sure how to get around it. Any help would be appreciated.

Thank you!

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: Variable not interpolating in imagebutton string

#2 Post by Remix »

Try:

Code: Select all

$ img = "{0}_%s.png".format( inventory.inventory[key].image )
imagebutton auto img action Null
Frameworks & Scriptlets:

pastblasters
Newbie
Posts: 8
Joined: Fri Mar 16, 2018 3:36 am
Contact:

Re: Variable not interpolating in imagebutton string

#3 Post by pastblasters »

Works perfectly, thank you.

Post Reply

Who is online

Users browsing this forum: Google [Bot], haitai