About changing variables in imagebuttons

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
Adrian_DVL
Regular
Posts: 114
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

About changing variables in imagebuttons

#1 Post by Adrian_DVL » Thu Jan 07, 2021 10:10 am

Hi!

Let's see, this is going to be long, so I apologize in advance. I have a complex screen that acts like the end screen for missions in which the player gathers items with different values for food, power and money, which then are distributed to the tastes of the player to increase his own food, power and money. For that, the player is supposed to select one button per item, having the chance to change his selection for each item and, when he's done, he has to hit the finish button.
This is a concept made in photoshop; it's not the final screen in-game (neither the icons of the items nor the icons of the stats are the right ones):
Image

So I have two problems and one question. I have this so far:

Code: Select all

screen endexp():
    zorder 2
    modal True
    $ distend = False
    $ fourity = 276
    $ threeity = 333
    $ twoity = 390
    $ oneity = 447
    $ firstfood = 0
    if len(inventory.items) == 4:
        $ itema = inventory.items[0]
        $ itemb = inventory.items[1]
        $ itemc = inventory.items[2]
        $ itemd = inventory.items[3]
    elif len(inventory.items) == 3:
        $ itema = inventory.items[0]
        $ itemb = inventory.items[1]
        $ itemc = inventory.items[2]
    elif len(inventory.items) == 2:
        $ itema = inventory.items[0]
        $ itemb = inventory.items[1]
    elif len(inventory.items) == 1:
        $ itema = inventory.items[0]
    else:
        pass
    $ foodtemp = 0
    $ enetemp = 0
    $ moneytemp = 0
    $ itemaf = False
    $ itemae = False
    $ itemam = False
    $ itembf = False
    $ itembe = False
    $ itembm = False
    $ itemcf = False
    $ itemce = False
    $ itemcm = False
    $ itemdf = False
    $ itemde = False
    $ itemdm = False

    $ foodtemp = food
    $ enetemp = ene
    $ moneytemp = money

    add "sys/resexp.png"

    if distend:
        imagebutton:
            idle "sys/opfin.png"
            hover "sys/opfinh.png"
            pos (735, 721)
            action [SetVariable("food", food+foodtemp), SetVariable("ene", ene+enetemp), SetVariable("money", money+moneytemp), Jump("hub")]

    else:
        imagebutton:
            idle "sys/opfinno.png"
            hover "sys/opfinno.png"
            pos (735, 721)
            action NullAction()



...



	if len(inventory.items) == 4:
            imagebutton:
                idle "sys/tofood.png"
                hover "sys/tofoodh.png"
                selected "sys/tofoodh.png"
                pos (585, 298)
                action [ If(itemae == True, true = [SetVariable("enetemp", enetemp-itema.ene)], false = NullAction()), If(itemam == True, true = [SetVariable("moneytemp", moneytemp-itema.mon)], false = NullAction()), SetVariable("itemae", False), SetVariable("itemam", False), SetVariable("itemaf", True), SetVariable("foodtemp", foodtemp+itema.foo) ]
            imagebutton:
                idle "sys/toene.png"
                hover "sys/toeneh.png"
                selected "sys/toeneh.png"
                pos (585, 323)
                action [ If(itemaf == True, true = [SetVariable("foodtemp", foodtemp-itema.foo)], false = NullAction()), If(itemam == True, true = [SetVariable("moneytemp", moneytemp-itema.mon)], false = NullAction()), SetVariable("itemaf", False), SetVariable("itemam", False), SetVariable("itemae", True), SetVariable("enetemp", enetemp+itema.ene) ]
            imagebutton:
                idle "sys/tomoney.png"
                hover "sys/tomoneyh.png"
                selected "sys/tomoneyh.png"
                pos (585, 348)
                action [ If(itemae == True, true = [SetVariable("enetemp", enetemp-itema.ene)], false = NullAction()), If(itemaf == True, true = [SetVariable("foodtemp", foodtemp-itema.foo)], false = NullAction()), SetVariable("itemae", False), SetVariable("itemaf", False), SetVariable("itemam", True), SetVariable("moneytemp", moneytemp+itema.mon) ]


            imagebutton:
                idle "sys/tofood.png"
                hover "sys/tofoodh.png"
                selected "sys/tofoodh.png"
                pos (585, 398)
                action [ If(itembe == True, true = [SetVariable("enetemp", enetemp-itemb.ene)], false = NullAction()), If(itembm == True, true = [SetVariable("moneytemp", moneytemp-itemb.mon)], false = NullAction()), SetVariable("itembe", False), SetVariable("itembm", False), SetVariable("itembf", True), SetVariable("foodtemp", foodtemp+itemb.foo) ]
            imagebutton:
                idle "sys/toene.png"
                hover "sys/toeneh.png"
                selected "sys/toeneh.png"
                pos (585, 423)
                action [ If(itembf == True, true = [SetVariable("foodtemp", foodtemp-itemb.foo)], false = NullAction()), If(itembm == True, true = [SetVariable("moneytemp", moneytemp-itemb.mon)], false = NullAction()), SetVariable("itembf", False), SetVariable("itembm", False), SetVariable("itembe", True), SetVariable("enetemp", enetemp+itemb.ene) ]
            imagebutton:
                idle "sys/tomoney.png"
                hover "sys/tomoneyh.png"
                selected "sys/tomoneyh.png"
                pos (585, 448)
                action [ If(itembe == True, true = [SetVariable("enetemp", enetemp-itemb.ene)], false = NullAction()), If(itembf == True, true = [SetVariable("foodtemp", foodtemp-itemb.foo)], false = NullAction()), SetVariable("itembe", False), SetVariable("itembf", False), SetVariable("itembm", True), SetVariable("moneytemp", moneytemp+itemb.mon) ]


            imagebutton:
                idle "sys/tofood.png"
                hover "sys/tofoodh.png"
                selected "sys/tofoodh.png"
                pos (585, 498)
                action [ If(itemce == True, true = [SetVariable("enetemp", enetemp-itemc.ene)], false = NullAction()), If(itemcm == True, true = [SetVariable("moneytemp", moneytemp-itemc.mon)], false = NullAction()), SetVariable("itemce", False), SetVariable("itemcm", False), SetVariable("itemcf", True), SetVariable("foodtemp", foodtemp+itemc.foo) ]
            imagebutton:
                idle "sys/toene.png"
                hover "sys/toeneh.png"
                selected "sys/toeneh.png"
                pos (585, 523)
                action [ If(itemcf == True, true = [SetVariable("foodtemp", foodtemp-itemc.foo)], false = NullAction()), If(itemcm == True, true = [SetVariable("moneytemp", moneytemp-itemc.mon)], false = NullAction()), SetVariable("itemcf", False), SetVariable("itemcm", False), SetVariable("itemce", True), SetVariable("enetemp", enetemp+itemc.ene) ]
            imagebutton:
                idle "sys/tomoney.png"
                hover "sys/tomoneyh.png"
                selected "sys/tomoneyh.png"
                pos (585, 548)
                action [ If(itemce == True, true = [SetVariable("enetemp", enetemp-itemc.ene)], false = NullAction()), If(itemcf == True, true = [SetVariable("foodtemp", foodtemp-itemc.foo)], false = NullAction()), SetVariable("itemce", False), SetVariable("itemcf", False), SetVariable("itemcm", True), SetVariable("moneytemp", moneytemp+itemc.mon) ]


            imagebutton:
                idle "sys/tofood.png"
                hover "sys/tofoodh.png"
                selected "sys/tofoodh.png"
                pos (585, 598)
                action [ If(itemde == True, true = [SetVariable("enetemp", enetemp-itemd.ene)], false = NullAction()), If(itemdm == True, true = [SetVariable("moneytemp", moneytemp-itemd.mon)], false = NullAction()), SetVariable("itemde", False), SetVariable("itemdm", False), SetVariable("itemdf", True), SetVariable("foodtemp", foodtemp+itemd.foo) ]
            imagebutton:
                idle "sys/toene.png"
                hover "sys/toeneh.png"
                selected "sys/toeneh.png"
                pos (585, 623)
                action [ If(itemdf == True, true = [SetVariable("foodtemp", foodtemp-itemd.foo)], false = NullAction()), If(itemdm == True, true = [SetVariable("moneytemp", moneytemp-itemd.mon)], false = NullAction()), SetVariable("itemdf", False), SetVariable("itemdm", False), SetVariable("itemde", True), SetVariable("enetemp", enetemp+itemd.ene) ]
            imagebutton:
                idle "sys/tomoney.png"
                hover "sys/tomoneyh.png"
                selected "sys/tomoneyh.png"
                pos (585, 648)
                action [ If(itemde == True, true = [SetVariable("enetemp", enetemp-itemd.ene)], false = NullAction()), If(itemdf == True, true = [SetVariable("foodtemp", foodtemp-itemd.foo)], false = NullAction()), SetVariable("itemde", False), SetVariable("itemdf", False), SetVariable("itemdm", True), SetVariable("moneytemp", moneytemp+itemd.mon) ]



...
Sorry for the extension of the code, but, as you can see, I have defined several things:
  • itema, itemb, itemc and itemd are the four items that the player can have in the inventory at the end of each mission, so they're the variables I'm going to work with.
  • foodtemp, enetemp and moneytemp are temporal variables that, starting in 0, are going to increase or decrease each time the player selects a button for each item, and they're supposed to go adding up to the previous add. For instance, if the player clicks on the food button of the first item and that item adds 2 of food, and then he clicks on the food button of the second item and that item adds 3, then the value of foodtemp will be 5. However, if then he clicks on the money button of the second item, the foodtemp value will be 2, since only the food button of the first item would be selected.
  • itemaf, itemae, itemam and their equivalents for the second, third and fourth items are variables that are set to True when the player selects an specific button for an item: the ended in f for food, the ended in e for power and the ended in m for money.
So I'm going to try to explain the imagebuttons action from my point of view. I know the code of the actions are a bit redundant, but that's because I've been trying things.

The thing is that every time that, for instance, the food button is clicked for, for instance, the first item, the variable associated to that button for that item (itemaf) is set to True, while the other two variables, the ones for power and money for the first item (itemae and itemam) are set to False. BUT before those two are set to False, the value of the item for power is substracted to powertemp IF the variable associated to the power button was True, i.e. if the player had selected the power button before switching to the food button, and the sames goes for the money button.

I don't know if I was clear enough, but I hope you understand. Now, from this I have several problems. First of all: the boolean variables itemaf, itemae, itemam, etc, are working well and each time I click a button, its variable is set to True and the other two are set to False. The problem is that even when the conditions are apparently well written, once the value of food, power or money of an item is added to foodtemp, powertemp or moneytemp, it isn't substracted back when I select another button for that item. Why is that? Is it because it's not well written? Or could it be because all the actions in the set of actions are executed at the same time so Renpy doesn't take into accound the variables set to True?

Another problem is that if I for instance click the food button for the first item and then the food button for the other items, only the value for food of the first item is added to foodtemp. The value of food for the other items are ignored and they're not added up to foodtemp.

And finally, I have a question on how SelectedIf works. As you can see, each imagebutton has a selected parameter, apart from the idle and hover ones, that'd show the same image as if the button was simply hovered. I've made this so that the player could know which button he had selected while he finished selecting the other ones. I guessed I could tell the game a button was selected with the SelectedIf statement, but it seems I don't understand well how that works. I've been trying but no button seems to remain selected.

Can someone help me with the If actions, the temp values and the SelectedIf thing, please? Thank you so much, especially for reading this whole damn thing!

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: About changing variables in imagebuttons

#2 Post by philat » Thu Jan 07, 2021 11:36 am

I didn't take the time to parse everything you did, but that is not how I would approach it in general. You can run the following in a new project as a set of pointers.

Code: Select all

init python:
    class Item():
        def __init__(self, food, energy, power):
            self.food = food
            self.energy = energy
            self.power = power
            self.selected = ""

    def getTotal(items, typ):
        res = 0
        for a in items:
            if a.selected==typ:
                res += getattr(a, a.selected)
        return res

screen testdistribute(items):
    vbox:
        xalign 0.2
        for i, j in enumerate(items):
            hbox:
                text ("Item "+str(i+1))
                vbox:
                    textbutton "Food":
                        action SetField(j, "selected", "food")
                    textbutton "Energy":
                        action SetField(j, "selected", "energy")
                    textbutton "Power":
                        action SetField(j, "selected", "power")
    vbox:
        xalign 0.8
        text "Food: {}".format(getTotal(items, "food"))
        text "Energy: {}".format(getTotal(items, "energy"))
        text "Power: {}".format(getTotal(items, "power"))

default item1 = Item(1, 2, 3)
default item2 = Item(2, 3, 1)

label start:
    $ tempinventory = [item1, item2]
    call screen testdistribute(tempinventory)

Post Reply

Who is online

Users browsing this forum: Google [Bot]