Ren'Py I was testing screens alongside inventory and location classes. How would I reduce the code to a dynamic current location inventory screen?

Ying Ko

Member
Jun 16, 2018
457
899
300
This is what I'm currently using, but it would require creating a new screen for every location.

Code:
############              The "middleman" screen, I'd rather get rid of this               ############
screen locitemsDisplay():
    if location == "Kitchen":
        use loc0itemsDisplay
    if location == "DiningRoom":
        use loc1itemsDisplay            


##I'd rather have one instance of this that dynamically relays the inventory of the current location##
##    Creating an identical inventory screen for every location works, but it is seems inefficient    ##
screen loc0itemsDisplay():
    frame:
        area (1440, 0, 480, 1080)
        add Solid ("#121212")
        viewport:
            draggable True
            mousewheel True
            vbox:
                for item in Kitchen.items: ## Loop location inventory ##
                    button:                ## Inventory items represented by buttons##
                        action [item.action]
                        frame:
                            background Solid("#121212")
                            foreground None
                            xsize 480
                            ysize 108
                            text [item.itemName]:
                                align (0.5,0.5)
                                size 25
                                hover_color "#9d4620"
Code:
##           What my test classes look like        ##
init python:

    class Inventory():
        def __init__(self, items):
            self.items = items
       
        def add_item(self, item):
            self.items.append(item)

        def remove_item(self, item):
            self.items.remove(item)
   
    class InventoryItem():
        def __init__(self, itemName, action):
            self.itemName = itemName
            self.action = action

    ###$ Kitchen.add_item(lemons) ####
    ##$ Kitchen.remove_item(lemons) ##

    class LOCATION():
        def __init__(self, locName):
            self.locName = locName      

    Locations = []
Code:
#########        Instance examples              ############################
default location = "Kitchen"
default Kitchen = Inventory([])
default DiningRoom = Inventory([])

define lemons = InventoryItem("Lemons", NullAction())
define watch = InventoryItem("Watch", NullAction())
define phone = InventoryItem("Phone", NullAction())
define shoes = InventoryItem("Shoes", NullAction())

label InitialiseVariables:
    $ Locations.append(LOCATION("Kitchen"))#0
    $ Locations.append(LOCATION("DiningRoom"))#1
#################################################################
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
12,959
21,571
1,026
This is what I'm currently using, but it would require creating a new screen for every location.
Well, just rely on the arguments passed to the screen:
Python:
screen locitemsDisplay( where ):

    frame:
        [...]
                for item in where.items:
                    [...]


label kitchen:
#  whatever one you need
#    show screen locitemsDisplay( kitchen )
    call screen locitemsDisplay( kitchen )