Page 1 of 1

[Solved] Help with an inventory sistem using screen language

Posted: Tue Apr 19, 2022 11:27 pm
by Arcaico976
Hi guys.
I am quite new to thiss.
What I am trying to do is a screen wich portrays the items the player has. I saw some models with python but i find too complex for what i am doing. Instead I tried to use a vbox with an if statement and a add image for each item. Works fine but the problem is that they are aligned vertically and i actually want them to be in a grid (4 for infinite). This is the test:

Code: Select all

screen phone_menu:
    modal True
    add bg_phone
    imagebutton auto "phone_icon_end_but_%s":
        focus_mask True
        hovered SetVariable("screen_tooltip","phone_icon_end")
        unhovered SetVariable("screen_tooltip", "")
        action Hide ("phone_menu")

    hbox:
        align(0.44, 0.22) 

        if aple == True:
            add "aple_inventory"
        if banana == True:
            add "banana_inventory"
        if anana == True:
            add "anana_inventory"
So I tried a grid but the issue is that I need to display them in a particular order, wich may not be how the player gets them, or loose them. If the player gets the banana first he will get an empty square and then a banana. With an inventory who could have 20 or 30 items it will look wierd.
This is just a test with three items.

Code: Select all

screen phone_menu2:
    modal True
    add bg_phone
    imagebutton auto "phone_icon_end_but_%s":
        focus_mask True
        hovered SetVariable("screen_tooltip","phone_icon_end")
        unhovered SetVariable("screen_tooltip", "")
        action Hide ("phone_menu2")
    side "c r":
        area (740, 195, 445, 750)

        viewport id "vp":
            draggable True
            has vbox

            vbox:
                grid 2 2:
                    if aple == True:
                        add "aple_inventory"
                    else:
                        null
                    if banana == True:
                        add "banana_inventory"
                    else:
                        null


                    if anana == True:
                        add "anana_inventory"
                    else:
                        null
                    null
Do anyone knows a solution to do what i am triyng to do ?
Thnks.

Re: Help with an inventory sistem using screen language

Posted: Wed Apr 20, 2022 12:25 am
by Xiael
If you have a fixed number of items and all the item hardcoded into each box, I suggest you could just turn it into a list of booleans that would show another image if that box is empty and the image of the item if True. Then, you could put an hbox containing these items of 4 in a row into a vbox. So that it would seem as if they are a grid.

Code: Select all

#declare items like these
default items = [False, False, False, False, False, ...]
# Just remember which ones' which
#you put these inside screen statement
vbox:
   hbox:
    if items[0] == False:
      image "empty.png"
    else:
      image "banana.png"
    if items[1] == False:
      image "empty.png"
    else:
      image "egg.png"
    if items[2]==False:
      image "empty.png"
    else:
      image "avocado.png"
    if items[3] == False:
      image "empty.png"
    else:
      image "orange.png"
   hbox:
     if items[4] == False:
        image "empty.png"
     else:
        image "apple.png"
     #etc
Apologies if that was not what you're trying to achieve, also I am not on pc right now so my code could have some errors esp on the spacing

Re: Help with an inventory sistem using screen language

Posted: Wed Apr 20, 2022 1:00 am
by Arcaico976
Thanks for the answer. I am not sure if I follow you, I am really new with this, but I dont want empty spaces. I used the else condition for the grid just so the code could work. The hbox or the vbox aligns the images perfetly, if you dont have an item it just show you the next without empty spaces. The only problem is that only have one axis, horizontal or vertical, and i want both of them. If I am not wrong, with your code i would need to put some items in each line (the hbox), so if the player gets first, lets say item number6, it will be on the first spot of the second line, while the first will be still empty, and i would like to see it in the first spot or the first line.
I tried your code, not sure how items[0] works. I should clarify that i didn't define items as a class, i am trying to avoid python language couse i dont understand it yet.

Re: Help with an inventory sistem using screen language

Posted: Wed Apr 20, 2022 11:44 am
by Alex
Arcaico976 wrote:
Tue Apr 19, 2022 11:27 pm
...Works fine but the problem is that they are aligned vertically and i actually want them to be in a grid (4 for infinite). ...
Hope this will help - viewtopic.php?f=8&t=52548#p499157

Re: Help with an inventory sistem using screen language

Posted: Wed Apr 20, 2022 1:08 pm
by Arcaico976
Thnks, it do help a lot. I readed the full page and play a little with the code and after some changes it can be exatly what i am looking for.
There is still one issue: how do I use the "$ inventory.append(orange)" as an action for an imagebutton. Guess i could use a label but if there is a simplier way it would help a lot.

Re: Help with an inventory sistem using screen language

Posted: Wed Apr 20, 2022 2:31 pm
by Arcaico976
Ok, I find the solution and works perfetely fine. Thanks a lot Alex, you are a life saver.
If someone has the same problem and its looking this page, the solution to my previous question is:

action AddToSet(inventory, banana)
and
action RemoveFromSet(inventory, banana)

Re: Help with an inventory sistem using screen language

Posted: Wed Apr 20, 2022 3:37 pm
by Alex
Arcaico976 wrote:
Wed Apr 20, 2022 2:31 pm
Ok, I find the solution and works perfetely fine. Thanks a lot Alex, you are a life saver.
If someone has the same problem and its looking this page, the solution to my previous question is:

action AddToSet(inventory, banana)
and
action RemoveFromSet(inventory, banana)
Well, for quite simple inventory system you could try to make it like this

Code: Select all

default shop_list = [apple, apple, orange, cake, orange]

init python:
    def add_item(item_to_add, list_to_add_to):
        list_to_add_to.append(item_to_add)
        
    def remove_item(item_to_remove, list_to_remove_from):
        if item_to_remove in list_to_remove_from:
            list_to_remove_from.remove(item_to_remove)
        else:
            renpy.notify("Item not in list")
            
screen test_shop_scr():
    on "show" action Function(renpy.retain_after_load)
    
    default description = ""
    if description:
        text "[description]" align(0.05, 0.45)
        
    frame:
        align(0.05, 0.05)
        vbox:
            text "Inventory:"
            hbox:
                for item in inventory:
                    button:
                        add item["img"]
                        hovered SetScreenVariable("description", item["dscrptn"])
                        unhovered SetScreenVariable("description", "")
                        action [Function(remove_item, item, inventory), Function(add_item, item, shop_list)]
            null
            textbutton "Done" action Return()
                    
    frame:
        align(0.05, 0.55)
        vbox:
            text "Shop:"
            hbox:
                for item in shop_list:
                    button:
                        add item["img"]
                        hovered SetScreenVariable("description", item["dscrptn"])
                        unhovered SetScreenVariable("description", "")
                        action [Function(remove_item, item, shop_list), Function(add_item, item, inventory)]
                        # this action will add only one item of a kind to inventory
                        # action AddToSet(inventory, item)
                    
                    
    


# The game starts here.
label start:
    "..."
    call screen test_shop_scr
    "... ..."
    "and again"
    call screen test_shop_scr
    "..."
    "?!"
https://www.renpy.org/doc/html/screen_a ... l#Function
https://www.renpy.org/doc/html/save_loa ... after-load