[Solved] Inventory system for Multiple Characters

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
RicharDann
Regular
Posts: 58
Joined: Thu Aug 31, 2017 11:47 am
Location: Planet Earth
Contact:

[Solved] Inventory system for Multiple Characters

#1 Post by RicharDann » Wed Oct 11, 2017 1:58 pm

So I have different POV characters in my story and I want to have each of them manage their own
set of items and clothes. And at some points I want them to be able to exchange items. So I figured classes where a good way to represent each character and their inventory. However, I want each inventory to hold an x amount of the same item, something like what you see in some RPGs like Suikoden, where you have for example up to Medicinex6 using one space, and if you add another Medicine, it becomes Medicinex1 in a different space and so on.

However I'm really stuck on how to apply this idea on actual code. I thought of using lists to store each player's amount of each item, however using a group of lists inside another list seems awfully unelegant and I have a feeling things will get messy and unnecesarily complicated when I start adding methods like remove or exchange item.

This is what I have so far:

Code: Select all

init python:

    class Item(object):

        def __init__(self, name= "", desc= ""):
            self.name = name
            self.desc = desc

    class Player(object):

        def __init__(self, name= "", inv = []):
            self.name = name
            self.inv = inv

        def add_item(self, item, amount): #This method probably won't work properly
            for l in self.inv:
                if item not in l: 
                    self.inv.append([item, amount])

default dave = Player('Dave')
default white_flower = Item('Flower', 'A pure white flower.')
What is the best way to handle this?
Last edited by RicharDann on Thu Oct 12, 2017 8:50 am, edited 1 time in total.

User avatar
RicharDann
Regular
Posts: 58
Joined: Thu Aug 31, 2017 11:47 am
Location: Planet Earth
Contact:

Re: Inventory system for Multiple Characters

#2 Post by RicharDann » Thu Oct 12, 2017 8:48 am

Nevermind I used dictionaries in the end and it seems to be working, dunno why I didn't think of that earlier since I recently worked with them. Also I found a tutorial in the cookbook basically on this so I'll probably end up relying on it if anything unexpected happens.

Thanks anyway.

Post Reply

Who is online

Users browsing this forum: No registered users