Want to add text to a (image)button

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
MagicBuns
Newbie
Posts: 9
Joined: Mon Dec 18, 2023 5:26 pm
Contact:

Want to add text to a (image)button

#1 Post by MagicBuns »

Hiya, technically this question has been answered twice through these two links:

viewtopic.php?p=466433

viewtopic.php?t=40730

I am trying to add the cost of clothing to my shop, alongside an image. I've tried the fixed and button methods shown in both links, but nothing happens. no images, buttons, or text are displayed, and neither is a bug popping up.

Here is the code I'm trying to insert my price tags into (storepage 3 is for testing the code):

Code: Select all

        hbox:
            xalign 0.65
            yalign 0.5
            grid 4 2:
                style_prefix "window"
                spacing 20
                if threadz.storepage == 0:
                    for key in wardrobe.top_lock:
                        frame:
                            imagebutton idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
                elif threadz.storepage == 1:
                    for key in wardrobe.pant_lock:
                        frame:
                            imagebutton idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
                elif threadz.storepage == 2:
                    for key in wardrobe.ling_lock:
                        frame:
                            imagebutton idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
                elif threadz.storepage == 3:
                    for key in wardrobe.top_unlock:
                        frame:
                            button idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
I am using dictionaries to store the data, and here is an example:

Code: Select all

default wardrobe = Wardrobe(
    top_unlock = {"turtleneck":{"Description": "A boring cardigan."}},
    top_lock = {"longsleeve":{"Description": "A green long-sleeved top", "cost": 50}},
I want to display the "cost" of the clothing alongside the image. Please don't just chuck me the Ren'py documentation. If that was enough, I wouldn't be asking for help. Thank you for your time.

jeffster
Veteran
Posts: 436
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Want to add text to a (image)button

#2 Post by jeffster »

MagicBuns wrote: Wed May 08, 2024 7:39 am Hiya, technically this question has been answered twice through these two links:

viewtopic.php?p=466433

viewtopic.php?t=40730

I am trying to add the cost of clothing to my shop, alongside an image. I've tried the fixed and button methods shown in both links, but nothing happens. no images, buttons, or text are displayed, and neither is a bug popping up.

Here is the code I'm trying to insert my price tags into (storepage 3 is for testing the code):

Code: Select all

        hbox:
            xalign 0.65
            yalign 0.5
            grid 4 2:
                style_prefix "window"
                spacing 20
                if threadz.storepage == 0:
                    for key in wardrobe.top_lock:
                        frame:
                            imagebutton idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
                elif threadz.storepage == 1:
                    for key in wardrobe.pant_lock:
                        frame:
                            imagebutton idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
                elif threadz.storepage == 2:
                    for key in wardrobe.ling_lock:
                        frame:
                            imagebutton idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
                elif threadz.storepage == 3:
                    for key in wardrobe.top_unlock:
                        frame:
                            button idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
I am using dictionaries to store the data, and here is an example:

Code: Select all

default wardrobe = Wardrobe(
    top_unlock = {"turtleneck":{"Description": "A boring cardigan."}},
    top_lock = {"longsleeve":{"Description": "A green long-sleeved top", "cost": 50}},
I want to display the "cost" of the clothing alongside the image. Please don't just chuck me the Ren'py documentation. If that was enough, I wouldn't be asking for help. Thank you for your time.
When you do

Code: Select all

                    for key in wardrobe.pant_lock:
                        frame:
                            imagebutton idle key:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
"for" means you iterate through wardrobe.pant_lock, taking each element of it, one by one, in a loop.
If it would be an image (or a path to an image file, like "images/pants01.png"), then

Code: Select all

imagebutton idle key
would mean "idle" image for this imagebutton is {substituted value from wardrobe.pant_lock}, like

Code: Select all

imagebutton idle "images/pants01.png"
And of course to add text there you would need to insert that text into the frame.

Hence the correct construction (for example) would be

(1) for the dictionaries, something like

Code: Select all

default top_unlock = {"turtleneck":{"Description": "A boring cardigan.", "Pic": "turtleneck.png", "Cost": 50}}
Note that top_unlock["turtleneck"] would contain not only the description but also the cost and the picture.

Looping "top_unlock" with "for" will give you keys of that dictionary one by one: "turtleneck", "grey sweater" and the like.

(2) Therefore in the screen use something like

Code: Select all

                    for key in top_unlock:
                        frame:
                            imagebutton idle top_unlock[key]["Pic"]:
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
                            text top_unlock[key]["Cost"]
PS. Oh I see it's a duplicate topic of viewtopic.php?t=68304
viewtopic.php?t=68304
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

Post Reply

Who is online

Users browsing this forum: No registered users