Jacob Singer
Jacob Singer

Reputation: 1

Python (RenPy): Textbuttons executing a function they don't explicitly call every time they're pressed

In the game I'm working on, I use an array to track the current stats of the player's company, and the following function to edit the array.

init python:

#The following store item objects, which include an array of their own stats
#Stores currently owned equipment
   Equipment = []
#Stores items available to buy
   Items = []
#Stores currently equipped equipment
   Equipped = []
#The company's current stats   
   SecArray = [0, 0, 0, 0, 0, 0]

#Called whenever moving something in or out of the currently equipped items.
#Pass the current item's stat array, the stat array, and a + or - symbol
   def arrayedit(array, SecArray, symbol):
        Notify("The stats have changed")
        if symbol == "+":
            SecArray[0] += array[0]
            SecArray[1] += array[1]
            SecArray[2] += array[2]
            SecArray[3] += array[3]
            SecArray[4] += array[4]
            SecArray[5] += array[5]
        if symbol == "-":
            SecArray[0] -= array[0]
            SecArray[1] -= array[1]
            SecArray[2] -= array[2]
            SecArray[3] -= array[3]
            SecArray[4] -= array[4]
            SecArray[5] -= array[5]
        return()

If any items are in the "Equipment" array, however, their stats are added to the current stats every time a textbutton is clicked (so, for example, if an item has 3 in a stat, the player's current stats will increase by 3 every time any button is clicked, counting infinitely upward). Similarly, if any items are in the "Equipped" array, their current stats are subtracted from the player's current stats every time a textbutton is clicked. Items in the "Items" array do not have any effect.

The following code is for windows to shop and equip/dequip equipment.

screen shopping1():

    frame:
        xpos (config.screen_width*25/64) ypos (config.screen_height*11/64)
        ysize (config.screen_height*31/64)
        xsize (config.screen_width*36/64)

        has side "c r b"

        viewport:
            yadjustment tutorials_adjustment
            mousewheel True

            vbox:
              xpos (config.screen_width*2/5) ypos (config.screen_height*3/16)
              ysize (config.screen_height/2)
              xsize (config.screen_width/2)
              for i in Items:
                    if i.kind == "Item":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(Equipment, i), RemoveFromSet(Items, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

              for i in Policies:
                    if i.kind == "Policy":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(OwnedPolicies, i), RemoveFromSet(Policies, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)                           

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

              for i in Trainings:
                    if i.kind == "Training":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(OwnedTrainings, i), RemoveFromSet(Trainings, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

        bar adjustment tutorials_adjustment style "vscrollbar"

        textbutton _("Return"):
            xfill True
            action [Hide("shopping1")]
            top_margin 10

screen equipmentedit():

    frame:
        xpos (config.screen_width*5/128)  ypos (config.screen_height*2/64)
        ysize (config.screen_height*47/64)
        xsize (config.screen_width*19/64)

        has side "c r b"

        viewport:
            yadjustment tutorials_adjustment
            mousewheel True

            vbox:
              null height 10
              text "Unequipped Items" alt ""
              null height 5
              for i in Equipment:

                    if i.kind == "Item":

                        textbutton "[i.title]   $[i.cost]":
                            action [arrayedit(i.stats, SecArray, "+"), AddToSet(Equipped, i), RemoveFromSet(Equipment, i), Hide("equipmentedit"), Return(i.cost)]
                            left_padding 20
                            xfill True

                    else:

                        null height 10
                        text i.title alt ""
                        null height 5
              null height 10
              text "Equipped Items" alt ""
              null height 5
              for i in Equipped:

                    if i.kind == "Item":

                        textbutton "[i.title]   $[i.cost]":
                            action [arrayedit(i.stats, SecArray, "-"), AddToSet(Equipment, i), RemoveFromSet(Equipped, i), Hide("equipmentedit"), Return(i.cost)]
                            left_padding 20
                            xfill True

                    else:

                        null height 10
                        text i.title alt ""
                        null height 5




        bar adjustment tutorials_adjustment style "vscrollbar"

        textbutton _("Return"):
            xfill True
            action [Hide("equipmentedit")]
            top_margin 10

Outside of this, the arrays and functions used are not called or referenced elsewhere in the program. I believe the "arrayedit" function is being called for items in the equipped and equpiment arrays every time a button is clicked, including the return buttons, but I'm unsure of why. Any insight would be greatly appreciated!

Upvotes: 0

Views: 3033

Answers (1)

Edward
Edward

Reputation: 235

I had the same problem and I believe you're you're falling victim to Renpy's prediction, as demonstrated by this thread on GitHub: https://github.com/renpy/renpy/issues/3718.

Renpy runs through all the code in a screen when it's shown (and multiple other times) including searching any function calls in order to pre-load assets. Usually this shouldn't cause a problem because most of the Renpy calls utilise renpy.restart_interaction() which reverts any changes to variables. Unfortunately it can struggle when it comes to directly calling Python objects.

The solution I used for this was to have the screen element call an intermediate function which included the renpy.restart_interaction() call (for some reason putting the restart directly into my Python object's function was causing other problems).

Something like this:

init python:
    def intermediateFunc(funcToCall, obj, arg):
        funcToCall(obj, arg)
        renpy.restart_interaction()

screen myScreen:
    imagebutton:
        # ...
        action Function(intermediateFunc, MyObj.DoSomething, obj, "an argument")

Upvotes: 0

Related Questions