Selecting Imagebuttons and other problems

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
Empish
Veteran
Posts: 221
Joined: Thu Jan 14, 2016 9:52 pm
Projects: Efemural Hearts, It Ends With Graduation
itch: empish
Contact:

Selecting Imagebuttons and other problems

#1 Post by Empish »

So I'm making a time-management minigame, and I'm trying to make it so that the ingredients imagebuttons toggle to a different image when it's clicked on. I'm using code like this:

Code: Select all

        imagebutton auto "barley_%s.png" action [Function(Toggle_Selected,"barley", 0), SelectedIf(ingredients_flags[0])]
for all of them, just with different names. The image files I have so far are barley_idle.png, barley_hover.png (this works!), and barley_selected_idle.png

This is my Toggle_Selected function:

Code: Select all

    def Toggle_Selected(c, i):
        global selected
        if c in selected:
            selected.remove(c)
            ingredients_flags[i] = True
        else:
            selected.append(c)
            ingredients_flags[i] = False
I know the function is getting called because the game is otherwise working properly (on this matter, anyway), it just doesn't seem to be putting the imagebutton into a selected state, which makes it hard to remember what's selected and what's not. It's probably something pretty obvious but I can't seem to figure it out.

I have a couple other problems with the game too, but I'll wait on those until this gets fixed.

Edit: Oh my lord. *facepalm* I forgot to put global ingredients_flags

>_> I feel stupid now. Although it appears to not always be working still. I wonder if there's another problem with it?

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Selecting Imagebuttons and other problems

#2 Post by namastaii »

I'm not a pro, but I know that one way that would work is having your imagebutton set a variable when it's clicked and your screen changes the imagebutton depending on the value of the variable. That's kind of how I've been setting things up lately and it works seemingly well. I imagine having a function like that would be less code and better but I haven't a clue how to do it that way (fully working).

User avatar
Empish
Veteran
Posts: 221
Joined: Thu Jan 14, 2016 9:52 pm
Projects: Efemural Hearts, It Ends With Graduation
itch: empish
Contact:

Re: Selecting Imagebuttons and other problems

#3 Post by Empish »

Well, okay, I do it that way and it works. It's kinda a pain though. ._.

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Selecting Imagebuttons and other problems

#4 Post by namastaii »

Yeah...I mean, realistically, if it works - it works. But no harm in still trying to figure it out on an external project in your spare time and if you solve the problem you can just move it over to your real project. But then again, maybe someone who knows this better can comment lol

User avatar
Quin
Regular
Posts: 117
Joined: Sun Nov 20, 2005 4:29 am
Location: Maine
Contact:

Re: Selecting Imagebuttons and other problems

#5 Post by Quin »

This sounds like what I'm trying to solve in my current project, so I'm very interested in any further discussion or suggestions on this.

What I'm trying to do is present the player with a set of imagebuttons that toggle between selected and unselected, where the player may select none, some or all of the buttons (and the selected buttons will be highlighted as selections are made), then click a Confirm button.

So if the screen was for selecting pizza toppings, there might be imagebuttons for pepperoni, mushrooms, olives and peppers. The player could, for example, click mushrooms, olives, and peppers (and each click would highlight the imagebutton and alter a variable), change their mind and remove peppers (and the click would un-highlight the button and reset the variable), then click Confirm.

I'm very rusty on both Ren'Py and Python, so any insight is much appreciated!

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Selecting Imagebuttons and other problems

#6 Post by namastaii »

You'd have a variable for each topping. Let's say for the meat topping, 1 would be pepperoni, 2 would be ham, and 3 would be sausage. The button would be like SetVariable("meat_topping", 1) and then 2 and 3 for the other buttons. Your selected idle version of those buttons or imagemap would be only highlighting the current value of each variable. Sorry I'm on a phone right now so it's hard to get into too much detail. I could possibly make a quick real example that I can test out when I get the chance.

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Selecting Imagebuttons and other problems

#7 Post by namastaii »

So this is a super basic version of the pizza thing (not assuming you're actually doing a pizza screen but here is the concept)

So these are the default values of the variables:

Code: Select all

    default pizza_size = 1
    default meat_topping = 0
    default extra_topping = 0
I made the pizza size 1, because we want one of the sizes to be selected already on the screen, it's a mandatory option. The other ones are at 0 because they are optional.

Then here's a really basic screen but the buttons do exactly what they should (the current value stands out):

Code: Select all

screen make_a_pizza():
    frame:
        xalign 0.5
        yalign 0.2
        vbox:
            text "Pick a size!"
            hbox:
                textbutton "Small" action SetVariable("pizza_size",1)
                textbutton "Medium" action SetVariable("pizza_size",2)
                textbutton "Large" action SetVariable("pizza_size",3)
            text "Pick a meat topping!"
            hbox:
                textbutton "Pepperoni" action SetVariable("meat_topping", 1)
                textbutton "Ham" action SetVariable("meat_topping", 2)
                textbutton "Sausage" action SetVariable("meat_topping", 3)
            text "Extra topping" 
            hbox:
                textbutton "Onions" action SetVariable("extra_topping", 1)
                textbutton "Peppers" action SetVariable("extra_topping", 2)
                textbutton "Olives" action SetVariable("extra_topping", 3)
I forgot to add an option to have no toppings but you'd just add a SetVariable("extra_topping", 0) on there.
You could take a concept like this and make it tons better. Use imagebuttons or an imagemap etc. Maybe even have a live pizza showing on the screen. Depending on what your plan is, I might already have an idea of how to achieve it.

Now for them selecting more than one thing on one variable..it would then have to turn into a list probably and have to deal with some screen variables and if statements. (I think) which, if you don't want to deal with that, each topping could be it's own variable. pepperoni = true, etc. So whenever you click on each button, it will light up even if other toppings are selected too. But you'd also have to create an if statement or something to have a second button take over if pepperoni is true, so that you can now have a button that turns it false, instead of you being forced to stick with your choice. If that makes sense

User avatar
Quin
Regular
Posts: 117
Joined: Sun Nov 20, 2005 4:29 am
Location: Maine
Contact:

Re: Selecting Imagebuttons and other problems

#8 Post by Quin »

Thanks for the response, Namastaii!

Of course I'm not actually doing a pizza thing, but it works similarly to the real topic. And the pizza size part is very relevant, since the first choice the player will make is of the "pick exactly one of these four" variety.

It gets a bit trickier with the other choices, since the player can pick none, some or all of the options. Following your example as written, the choice for meat is pepperoni OR ham OR sausage. But I need to allow for no meat, any one meat, any combination of two meats, or all three meats. I could either use a boolean for each topping, or have the choices as a Python set like ['pepperoni','sausage'] -- just as long as I can eventually compare the choices made to the "correct" choices.

And your last paragraph makes a lot of sense, and that's what I need to work with. After seeing this, I have a clearer understanding of what I need to do to solve this problem. Now to dive into the documentation and write some test code. Thanks!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]