[SOLVED] Consumable and Inventory

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
Elrena
Newbie
Posts: 15
Joined: Sat Nov 12, 2022 12:38 pm
Contact:

[SOLVED] Consumable and Inventory

#1 Post by Elrena »

Hi it's me again, few days without problem but it's back with a vengeance.

So as my complex development attempt didnt go as smoothly last time i decide to use a template from the cookbook for a inventory system this time (viewtopic.php?f=51&t=65379 bt HiddenLife) which worked fine on a surface level but i now ran to several small issue.

The aim is to create consummable that will affect my stat system, but the right way to do it elude me, i try to follow and adapt the one from Elaine (https://www.youtube.com/watch?v=leEiOzuVpTc) but was'nt able to as it's lost into a more complex system.

So once more i'm humbly asking for help, moreso as this thing is python heavy and i still really struggle to see incoherences in it.

So much so in fact that the following code i was planned to submit for this topic has a error i cant fix, so i'll still post it but beware that it has an error.

But for what i need help with is this :

1: the "backpack" reset at each game reboot and does not update between save (ex i have one book, i load a save prior having it and it's still here) so i need a fix for that
(writing a default backpack = () cause a error as it's already define with another line, see code provided), i'm sure it's once more a easy fix i just cant seem to find it.

2: i want that the item in the screen ''backpack" to be interactable and be remove after use (there is a rem_item propmt already in the script but i dont know how to use it)

i'm not too attached to this script so it can be heavy edited and simplifyed if needed, i'll just need it to be compatible with a shop screen down the line but i dont think that will be a issue.

anyway there is what should be the standalone script for testing purpose, but yeah with an error :(

Code: Select all


##inventory test

image intro = Solid("#6c77a8")


# Inventory script
init -5 python:

    class Item(object):

        def __init__(self, name):

            self.name = name
            
    class InvItem(object):
        def __init__(self, item, amount):
            self.item = item
            self.amount = amount

    class Container(object):

        def __init__(self):

            self.inventory = []


        def finditem(self, item):

            return(self.inventory[[i.item for i in self.inventory].index(item)])


        def has_item(self, item, amount=1):

            if item in [i.item for i in self.inventory]:
                if self.finditem(item).amount >= amount:
                    return(self.finditem(item).amount)
                else:
                    return(False)
            else:
                return(False)


        def add_item(self, item, amount=1):

            if item in [i.item for i in self.inventory]:
                self.finditem(item).amount += amount
            else:
                self.inventory.append(InvItem(item, amount))

            return('success')


        def rem_item(self, item, amount=1):
            if self.has_item(item):
                if self.finditem(item).amount > amount:
                    self.finditem(item).amount -= amount
                    return('more left')
                elif self.finditem(item).amount == amount:
                    self.finditem(item).amount -= amount
                    self.inventory.pop(self.inventory.index(self.finditem(item)))
                    return('gone')
                elif self.finditem(item).amount < amount:
                    return('not enough')
            else:
                return('item not found')


#default value

init 0:

    $ backpack = Container()
    $ money = False

    default academics = 15 #value to affect 

    default book = 0
    
#item

$ book = Item("book", 1)

#test loop

label start:
    scene intro

    "test consummable and inventory. 
    Objective: working inventory screen and consummable effect"

    call screen test


screen test:
    textbutton _("book") action Jump ("book") xpos 0.5 ypos 0.5
    textbutton _("inventory") action Show("backpack") xpos 0.5 ypos 0.55

 

label book:

    $ backpack.add_item("book", 1)

    call screen test



#Inventory Screen

screen backpack:

    tag inventory
    zorder 1000
    modal True
    hbox:
        xalign 0.5
        yalign 0.05
        spacing 10


    side "tl":
        area (465, 109, 1, 1)
        grid 10 100:
            spacing 1
            allow_underfull True
            for i in backpack.inventory:
                vbox:
                    hbox:
                        text str(i.amount) size 20
                    text i.item.name size 15


    textbutton _("back"):
        action [Hide("backpack"), Show("test")]



As Always, thanks in advance for any help and pointer. it's fortunatly my last script heavy stuff.
Last edited by Elrena on Fri Nov 25, 2022 8:00 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Consumable and Inventory

#2 Post by Ocelot »

Any variable defined in init block or using define statement will be assumed as immutable by RenPy (aside from cases when you rebind that variable later, but that is not important here). Because of that they (a) will not be included in saves; (b) will not be reinitialized when game is soft-restarted. After all, you promised that value will not change, so there is no need.

In short, this part

Code: Select all

init 0:

    $ backpack = Container()
    $ money = False

    default academics = 15 #value to affect 

    default book = 0
should be changed to

Code: Select all

# No init. Raw init statements (not init python) are mostly unnessesary
# Because there usually are statements explicitely run in init time
default backpack = Container()
default  money = False
default academics = 15  
default book = 0
I didn't check 2nd problem yet, since I do not have much time right now.
< < insert Rick Cook quote here > >

Elrena
Newbie
Posts: 15
Joined: Sat Nov 12, 2022 12:38 pm
Contact:

Re: Consumable and Inventory

#3 Post by Elrena »

there is no rush, fortunaly as it's relativly independant i can focus on other stuff. I'll make the change you already pointed still :D

Elrena
Newbie
Posts: 15
Joined: Sat Nov 12, 2022 12:38 pm
Contact:

Re: Consumable and Inventory

#4 Post by Elrena »

little bump on my topic as ill need it soon :)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Consumable and Inventory

#5 Post by Ocelot »

An example of using nearrect displayable to add menus on click:

Code: Select all

screen backpack:
    tag inventory
    zorder 5
    modal True
    vbox:
        textbutton _("back") action [Hide("backpack"), Show("test")]
        grid 10 100:
            allow_underfull True
            for i in backpack.inventory:
                button:
                    action [CaptureFocus("chosen_item"), Show("backpack_actions", item=i)]
                    has vbox
                    hbox:
                        text str(i.amount) size 20
                    text i.item size 15


screen backpack_actions(item):
    zorder 100
    dismiss:
        action [ClearFocus("chosen_item"), Hide()]
    nearrect:
        focus "chosen_item"
        frame:
            background "#333"
            has vbox
            textbutton "Use"     action [Function(backpack.rem_item, item.item),              ClearFocus("chosen_item"), Hide()]
            textbutton "Discard" action [Function(backpack.rem_item, item.item, item.amount), ClearFocus("chosen_item"), Hide()]
< < insert Rick Cook quote here > >

Elrena
Newbie
Posts: 15
Joined: Sat Nov 12, 2022 12:38 pm
Contact:

Re: Consumable and Inventory

#6 Post by Elrena »

Thanks a lot, it's precisely what i had in mind. Once more i'm very grateful for your help Ocelot :)
'Solved' tag incoming

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]