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 images, but nothing happens. no images, buttons, or text are displayed.

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.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1009
Joined: Sun Feb 21, 2021 3:36 am
Contact:

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

#2 Post by m_from_space »

MagicBuns wrote: Wed May 08, 2024 7:39 am 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 images, but nothing happens. no images, buttons, or text are displayed.
Your imagebuttons inside the grid use the contents of the loop variable "key" as the image reference. So looking at your "wardrobe" data, the key would be something like "turtleneck" or "longssleeve". Do you have images with that name in your images folder, e.g. "turtleneck.png"?

If nothing is showing up in your grid you might want to test if the grid is even inside your viewing window, so put something inside without any condition, like a text. If that works, maybe your problem is the condition named "threadz.storepage". You sure this is even met?

P.S. "key" is a reserved Renpy keyword, it's adviced to use another name!

MagicBuns
Newbie
Posts: 9
Joined: Mon Dec 18, 2023 5:26 pm
Contact:

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

#3 Post by MagicBuns »

Yeah, I've defined those images, and they work. The code shown works as intended. I haven't added the price tag, so when I try writing this, storepage 3 won't show anything, but storepage 2 can work fine:

Code: Select all

                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:
                                add key
                                text "Test"
                                action SetVariable("threadz.receipt", key), Jump("lucy_receipt")
I can use the fixed: method too, where I have an imagebutton & textbutton as children, but same issue as it is with the button method, nothing shows. No errors, no nothing.

p.s. It's a (nested) dictionary that I am iterating through, so I'm assuming key, value are both needed for such a thing? I'd be happy to go back to creating my own local variables (top, pants, ling). Again, it works!

p.p.s. The original code, though it probs can be written better, functions as it should. It shows all the outer keys in the nested dictionaries that they iterate through. The grid works fine. The storepages also work fine too. Why I shared that code, is that I was wondering whether it being in a grid, or hbox would affect the "fixed" or "button" methods. Also, with the example I've shown in this reply, I've also tried

Code: Select all

add "image.png"
, and same results - nothing.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1009
Joined: Sun Feb 21, 2021 3:36 am
Contact:

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

#4 Post by m_from_space »

MagicBuns wrote: Wed May 08, 2024 11:02 am I can use the fixed: method too, where I have an imagebutton & textbutton as children, but same issue as it is with the button method, nothing shows. No errors, no nothing.
Well, I guess there is no data / dict inside "wardrobe.top_unlock" that it can iterate over. Maybe post your class definition for objects of type "Wardrobe()"!

MagicBuns
Newbie
Posts: 9
Joined: Mon Dec 18, 2023 5:26 pm
Contact:

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

#5 Post by MagicBuns »

I've shown you the wardrobe.top_lock etc in my first post... it does have it, the imagebuttons all work fine, it's when I try the button/fixed method to include text when it shows nothing.

But it's ok, I found a compromise. Thank you anyway :)

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

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

#6 Post by Imperf3kt »

You can use hover_foreground and idle _foreground states to define a text element over an imagebutton
I use this often with a bunch of classes to input data for hundreds of objects using a simple for loop
Something like this

Code: Select all

imagebutton:
    idle "an image"
    hover_foreground Text(_(""your text or variable"), xalign=0.5, yalign=0.95, color='#000', size=30)
    idle_foreground Text(_("your text or variable" ), xalign=0.5, yalign=0.95, color='#000', size=30)
    action NullAction()
Adjust the xalign and yalign properties to change the position of the text over your image.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

giorgi1111
Newbie
Posts: 14
Joined: Sat May 04, 2024 10:40 pm
Contact:

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

#7 Post by giorgi1111 »

Imagebutton:
Xpos 10 ypos10
Idle "[key]"
Action something
Text "[key price]" xpos 10 ypos 10
Or put imagebutton and text in maby vbox and use yoffset for adjustment

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Sugar_and_rice