An inventory system help
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.
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.
An inventory system help
Heya! I'm having an issue with creating an "inventory system"
Where ...
- you can drag and place the item around the inventory slots
- Each new item will be sorted on its category and rarity
- A crafting menu where it requires particular items with the same rarity to create a new item
- It's easy to add or remove new items (Just like typing the code on the label and then the new item will appear)
- Item has their own Weight, Rarity, Amount, and Category (for example, an item for food ingredients is not consumable, snacks are consumable, and cooked meal cannot be stacked)
(If the weight in the inventory is surpassing certain amount, the player cannot add more item into their inventory)
- And you can get the item from the shop by buying it or selling the item in the shop for money
I'm really sure this is really complicated coding ...
And I can't use my logic here, not like when creating the pop-up notification one.
Massive thanks in advance. (if it's necessary, you can DM me)
Where ...
- you can drag and place the item around the inventory slots
- Each new item will be sorted on its category and rarity
- A crafting menu where it requires particular items with the same rarity to create a new item
- It's easy to add or remove new items (Just like typing the code on the label and then the new item will appear)
- Item has their own Weight, Rarity, Amount, and Category (for example, an item for food ingredients is not consumable, snacks are consumable, and cooked meal cannot be stacked)
(If the weight in the inventory is surpassing certain amount, the player cannot add more item into their inventory)
- And you can get the item from the shop by buying it or selling the item in the shop for money
I'm really sure this is really complicated coding ...
And I can't use my logic here, not like when creating the pop-up notification one.
Massive thanks in advance. (if it's necessary, you can DM me)
Re: An inventory system help
This might be a good starting point:
viewtopic.php?f=51&t=66699
I didn't do sorting there (being too lazy for that), but the cells can be highlighted according to the item category. Which is the next best thing, and doesn't change the placement created by manual sorting.
Your weight calculations (and the like) can be done the same way as stat bonuses calculations there.
viewtopic.php?f=51&t=66699
I didn't do sorting there (being too lazy for that), but the cells can be highlighted according to the item category. Which is the next best thing, and doesn't change the placement created by manual sorting.
Your weight calculations (and the like) can be done the same way as stat bonuses calculations there.
If the problem is solved, please edit the original post and add [SOLVED] to the title.
Re: An inventory system help
Oh yeah! I saw your post on the RPG-style one! I must admit it does cover some parts of what I need, but... I still do not understand how to add a new item category, make the same item category but with different rarity, or make the item "consumable"jeffster wrote: ↑Wed Jun 14, 2023 7:13 am This might be a good starting point:
viewtopic.php?f=51&t=66699
I didn't do sorting there (being too lazy for that), but the cells can be highlighted according to the item category. Which is the next best thing, and doesn't change the placement created by manual sorting.
Your weight calculations (and the like) can be done the same way as stat bonuses calculations there.
and how to make and elif statement if the player has the particular item category and rarity My head, ambatublow
Re: An inventory system help
These two can contradict each other.
Here is another RenPy inventory that you may find useful: Infinite, Stackable Inventory/Crafting/Vendor
Re: An inventory system help
Let's look at the script.
Items in the game are represented by dictionary "items":
Code: Select all
items = {
"sword": {
"idle": "images/sword.webp",
"hover": "images/sword_h.webp",
"dragin": "images/sword_d.webp",
"slots": ["hand_r"], # In which slots can be placed
"attr": "https://www.flaticon.com/authors/freepik",
"bonus": {"strength": 3},
"desc": _("Good short sword {=g}+3 STR"),
"type": "Weapon Melee",
},
"dagger": {
# ...And so on
(As the dictionary keys should be unique, if the game has several swords of the same kind, name them differently, like "sword01", "sword02", "sword03"... even if their contents would be the same).
To add an item to the inventory, there is function addItems(). Call it like this:
Code: Select all
addItems("dagger", "sword", "shield")
That's how we add items to the inventory in one simple line.
Of course you can change items' fields. For example, add weight & rarity and flags for "consumable" & "stackable":
Code: Select all
# Rarity constants:
TRASH = 0
COMMON = 1
UNCOMMON = 2
RARE = 3
EPIC = 4
items = {
"sword": {
"consumable": False,
"stackable": False,
"weight": 1.5,
"rarity": UNCOMMON,
"idle": "images/sword.webp",
"hover": "images/sword_h.webp",
"dragin": "images/sword_d.webp",
"slots": ["hand_r"], # In which slots can be placed
"attr": "https://www.flaticon.com/authors/freepik",
"bonus": {"strength": 3},
"desc": _("Good short sword {=g}+3 STR"),
"type": "Weapon Melee",
},
Code: Select all
if myItem["type"] == "Weapon Melee" and myItem["rarity"] == EPIC:
#...
Code: Select all
init python:
def checkRarity(itemName, rarity):
return (items[itemName]["rarity"] == rarity)
Code: Select all
python:
myEpic = set()
for k in owned:
if checkRarity(k, EPIC):
myEpic.add(k)
PS. To have different categories, just write whatever you want in the item "type" field:
Code: Select all
"egg": {
"consumable": True,
"stackable": True,
"weight": 0.1,
"rarity": COMMON,
"idle": "images/egg.webp",
"hover": "images/egg_h.webp",
"dragin": "images/egg_d.webp",
"desc": _("Tasty egg"),
"type": "Food Raw",
},
If the problem is solved, please edit the original post and add [SOLVED] to the title.
Re: An inventory system help
PS. My inventory doesn't have "stackables" implemented.
So to add "stackables" to your game you will need for example to add the amount to "owned" dictionary stackable items. If can look like this if you pick up an egg:
Then you will need function addItem accepting 2 parameters: item name and amount:
Something like this. Therefore, to add stackables, use addItem function that adds only 1 kind of item at a time. (Because if you want to use addItems function that can add several different items at a time, you would need to complicate parameters structure. Do we want that? Nah...).
Then stackables in your inventory might look like:
As you see all the data there would be copied from "items" except the "amount" field. We add the "amount" field there when we add that kind of item to the inventory if it didn't exist there. Otherwise we just increase the amount.
Likewise, you can write/modify functions that spend stackable items: decrease their amount or remove them at all when there are no more left.
So to add "stackables" to your game you will need for example to add the amount to "owned" dictionary stackable items. If can look like this if you pick up an egg:
Code: Select all
addItem("egg", 1)
Code: Select all
init python:
def addItem(name, amount=1):
if item[name]["stackable"]:
# check if items of those kind are in the inventory already:
if name in owned:
# If so, just increase the amount:
owned[name]["amount"] += amount
else:
# Otherwise add new item:
addItems(name)
# Check if it was successful (or the backpack full or something)
if name in owned:
owned[name] = amount
else:
# Not a stackable
for i in range(0, amount):
addItems(name)
Then stackables in your inventory might look like:
Code: Select all
"egg": {
"consumable": True,
"stackable": True,
"weight": 0.1,
"rarity": COMMON,
"idle": "images/egg.webp",
"hover": "images/egg_h.webp",
"dragin": "images/egg_d.webp",
"desc": _("Tasty egg"),
"type": "Food Raw",
"amount": 3
}
Likewise, you can write/modify functions that spend stackable items: decrease their amount or remove them at all when there are no more left.
If the problem is solved, please edit the original post and add [SOLVED] to the title.
Re: An inventory system help
Humu humu... I'll see what I can do, Massive thanks for sharing this knowledge! TwT I will try this out later!jeffster wrote: ↑Wed Jun 14, 2023 3:30 pm PS. My inventory doesn't have "stackables" implemented.
So to add "stackables" to your game you will need for example to add the amount to "owned" dictionary stackable items. If can look like this if you pick up an egg:Then you will need function addItem accepting 2 parameters: item name and amount:Code: Select all
addItem("egg", 1)
Something like this. Therefore, to add stackables, use addItem function that adds only 1 kind of item at a time. (Because if you want to use addItems function that can add several different items at a time, you would need to complicate parameters structure. Do we want that? Nah...).Code: Select all
init python: def addItem(name, amount=1): if item[name]["stackable"]: # check if items of those kind are in the inventory already: if name in owned: # If so, just increase the amount: owned[name]["amount"] += amount else: # Otherwise add new item: addItems(name) # Check if it was successful (or the backpack full or something) if name in owned: owned[name] = amount else: # Not a stackable for i in range(0, amount): addItems(name)
Then stackables in your inventory might look like:As you see all the data there would be copied from "items" except the "amount" field. We add the "amount" field there when we add that kind of item to the inventory if it didn't exist there. Otherwise we just increase the amount.Code: Select all
"egg": { "consumable": True, "stackable": True, "weight": 0.1, "rarity": COMMON, "idle": "images/egg.webp", "hover": "images/egg_h.webp", "dragin": "images/egg_d.webp", "desc": _("Tasty egg"), "type": "Food Raw", "amount": 3 }
Likewise, you can write/modify functions that spend stackable items: decrease their amount or remove them at all when there are no more left.
Re: An inventory system help
So for the "Consumable" item, how do I make it when the player "Use" the item (like eat or drink) from 2 different rarity (which give different energy increase)jeffster wrote: ↑Wed Jun 14, 2023 3:30 pm PS. My inventory doesn't have "stackables" implemented.
So to add "stackables" to your game you will need for example to add the amount to "owned" dictionary stackable items. If can look like this if you pick up an egg:Then you will need function addItem accepting 2 parameters: item name and amount:Code: Select all
addItem("egg", 1)
Something like this. Therefore, to add stackables, use addItem function that adds only 1 kind of item at a time. (Because if you want to use addItems function that can add several different items at a time, you would need to complicate parameters structure. Do we want that? Nah...).Code: Select all
init python: def addItem(name, amount=1): if item[name]["stackable"]: # check if items of those kind are in the inventory already: if name in owned: # If so, just increase the amount: owned[name]["amount"] += amount else: # Otherwise add new item: addItems(name) # Check if it was successful (or the backpack full or something) if name in owned: owned[name] = amount else: # Not a stackable for i in range(0, amount): addItems(name)
Then stackables in your inventory might look like:As you see all the data there would be copied from "items" except the "amount" field. We add the "amount" field there when we add that kind of item to the inventory if it didn't exist there. Otherwise we just increase the amount.Code: Select all
"egg": { "consumable": True, "stackable": True, "weight": 0.1, "rarity": COMMON, "idle": "images/egg.webp", "hover": "images/egg_h.webp", "dragin": "images/egg_d.webp", "desc": _("Tasty egg"), "type": "Food Raw", "amount": 3 }
Likewise, you can write/modify functions that spend stackable items: decrease their amount or remove them at all when there are no more left.
it'll call a label to do the dialogue for each item and give the player a different result?
Re: An inventory system help
We call screen "inventory" in a loop. When an interaction happens in the screen and we need to process it, the screen call returns, and we get a return value. Here it is in the script:
Code: Select all
call screen inventory(equips)
if isinstance(_return, bool):
# Clicked "Exit" button
return
python:
curDrag = None
item, place = _return # strings: what dropped, where dropped
* "item" is an item that was clicked or drag-n-dropped.
* "place" is a slot to equip the item. (If "place" returned value is None, it means the item was dropped outside slots and we just unequip it if it was equipped).
So to add items' consumption, the easiest way is to add a slot for "eating". Let's call that slot "eat". In my demo, here is the dictionary of slots:
Code: Select all
# Slots for equipped items (as drop areas)
drops = {
"hand_r": { # drag_name
"xy": (997, 186), # pos
"xys": (118, 118), # xysize
},
"hand_l": {
"xy": (1435, 466),
"xys": (118, 118),
},
}
Code: Select all
drops = {
"eat": { # drag_name
"xy": (1000, 100), # pos
"xys": (600, 800), # xysize
},
"hand_r": { # drag_name
"xy": (997, 186), # pos
"xys": (118, 118), # xysize
},
# and so on...
Code: Select all
"egg": {
"consumable": True,
"stackable": True,
"weight": 0.1,
"rarity": COMMON,
"idle": "images/egg.webp",
"hover": "images/egg_h.webp",
"dragin": "images/egg_d.webp",
"slots": ["eat"],
"attr": "https://www.psdgraphics.com/psd-icons/brown-white-and-golden-eggs-psd-icons/",
"desc": _("Tasty egg"),
"type": "Food Raw",
},
Let's see how we do that in the "inventory" screen:
* When an item in the inventory gets clicked, function "clickedIt" runs.
* When an item gets dragged and dropped, function "draggedTo" runs.
Both these functions check if the item should go to an available slot. Then they return a pair of values, item name and slot name. If there's no slot available, they return None, and that causes the screen to continue. (Screen call ends only when those functions return a non-empty value).
So what we have to do is to add a check of returned values, was that a command to consume. You see the returned values in the code above:
Code: Select all
item, place = _return # strings: what dropped, where dropped
# So we add this: Was that a consumable?
if place == "eat":
# And here we will write a code to process consumption
#...
Code: Select all
"egg": {
"consumable": True,
"effects": {"energy": 5},
Last edited by jeffster on Fri Jun 16, 2023 1:54 pm, edited 1 time in total.
If the problem is solved, please edit the original post and add [SOLVED] to the title.
Re: An inventory system help
By the way there were like 2 errors in my addItem function. Here's the corrected version:
Code: Select all
def addItem(name, amount=1):
""" Add items[name] to the backpack in amount.
"""
global owned
if items[name]["stackable"]:
# check if items of those kind are in the inventory already:
if name in owned:
# If so, just increase the amount:
owned[name]["amount"] += amount
else:
# Otherwise add new item:
addItems(name)
# Check if it was successful (or the backpack full or something)
if name in owned:
owned[name]["amount"] = amount
else:
# Not a stackable
for i in range(0, amount):
addItems(name)
If the problem is solved, please edit the original post and add [SOLVED] to the title.
Re: An inventory system help
I posted demo v.1.1. You can see there stackables and consumables at work. Eating happens without confirmation screen, but you can add it in a standard Ren'Py way.
https://renpy.org/doc/html/screen_special.html#confirm
PS. Crafting screen can be done the same way, just a different picture and set of slots.
https://renpy.org/doc/html/screen_special.html#confirm
PS. Crafting screen can be done the same way, just a different picture and set of slots.
If the problem is solved, please edit the original post and add [SOLVED] to the title.
Re: An inventory system help
I posted v.1.3.
viewtopic.php?f=51&t=66699
It has Crafting screen and so it shows the ability to combine "backpack" with different screen types: equipment slots, crafting etc. The same way, storage or shopping screens can be added.
viewtopic.php?f=51&t=66699
It has Crafting screen and so it shows the ability to combine "backpack" with different screen types: equipment slots, crafting etc. The same way, storage or shopping screens can be added.
If the problem is solved, please edit the original post and add [SOLVED] to the title.
-
- Regular
- Posts: 94
- Joined: Thu Dec 31, 2020 3:37 pm
- Contact:
Re: An inventory system help
Hi Jeffster,
I've started using this inventory system (or parts of it, anyway) in my game, but have a question. The initial items available in game are defined in the code like this:
The problem is that I obviously cannot predict in advance every single item that's going to be in my game, so I need a way of extending this list later. I'd imagined that just adding items to the list when it was defined would update my saved games but it doesn't - when I load a game, it just crashes because it doesn't recognise the items I've added to the list since the game was saved. So is there a way to add new items to the items array programmatically within my script at runtime?
Thanks
Simon
I've started using this inventory system (or parts of it, anyway) in my game, but have a question. The initial items available in game are defined in the code like this:
Code: Select all
default items = {
"sword": {
"idle": "images/sword.webp",
"hover": "images/sword_h.webp",
"dragin": "images/sword_d.webp",
"slots": ["hand_r"], # In which slots can be placed
"attr": "https://www.flaticon.com/authors/freepik",
"bonus": {"strength": 3},
"desc": _("Good short sword {=g}+3 STR"),
"type": "Weapon Melee",
},
"dagger": {
"idle": "images/dagger.webp",
"hover": "images/dagger_h.webp",
"dragin": "images/dagger_d.webp",
"slots": ["hand_r", "hand_l"],
"attr": "https://www.flaticon.com/authors/freepik",
"bonus": {"dexterity": 1},
"desc": _("Nasty knife {=g}+1 DEX"),
"type": "Weapon Melee",
},
... etc
Thanks
Simon
-
- Regular
- Posts: 94
- Joined: Thu Dec 31, 2020 3:37 pm
- Contact:
Re: An inventory system help
Well, I'm damned if I can work out how to delete my last post - I can't see a delete button anywhere - but I worked it out in the end. I'm not too familiar with python and just had to do a little research on dictionaries. I'm still working with arrays in my head.
-
- Regular
- Posts: 94
- Joined: Thu Dec 31, 2020 3:37 pm
- Contact:
Re: An inventory system help
I'm now using this inventory system fairly extensively in my game and it's worked wonders - but there's one thing I can't seem to work out so I'm hoping you can help point me in the right direction.
I have two characters in my game (actually, more than two but let's not complicate matters), and when each character is active they should have their own inventory. I had imagined that this would be as simple as doing something like this:
Everything seems to be stored in dictionaries called drops (for equipment slots), backpack (for backpack slots) and items (for list of items and where they're stored), so I thought that just swapping these dictionaries out for new ones (called itemsmarie, dropsmarie and backpackmarie) and then back again afterwards would work. To be fair, it does seem to work until marie consumes an item or anything is removed from an item slot - at which point there are suddenly two of them in the backpack and everything goes mad. This seems to be something to do with the Sack class, but classes in Python seem to be nothing like classes in other languages I use (where classes are basically objects which you don't call directly but instead create instances of - such as Sack1=Sack(), Sack2=Sack() to create two instances of the sack class). The sack class seems to be called directly by the inventory code, so short of copying and pasting the entire class into a new class and hacking and slashing about with the code to refer to the correct sack depending on the character in use at the time, I can't see how I can make this work properly.
Short of adding support for multiple characters directly into the Inventory system, how would you suggest I go about doing this correctly?
TIA,
Simon
I have two characters in my game (actually, more than two but let's not complicate matters), and when each character is active they should have their own inventory. I had imagined that this would be as simple as doing something like this:
Code: Select all
* PLAY AS CHARACTER ONE UP UNTIL THIS POINT *
$tempitems=items
$tempdrops=drops
$tempbackpack=backpack
$items=itemsmarie
$drops=dropsmarie
$backpack=backpackmarie
* NOW PLAYING AS CHARACTER TWO (MARIE) *
$items=tempitems
$drops=tempdrops
$backpack=tempbackpack
* PLAYING CHARACTER ONE AGAIN *
Short of adding support for multiple characters directly into the Inventory system, how would you suggest I go about doing this correctly?
TIA,
Simon
Who is online
Users browsing this forum: Google [Bot]