How do I make an inventory system where I can have multiple inventories? What I mean by this is: a chest and a player. I tried tinkering with the example but it seems too complicated for me. Please help by either sending code or telling me what each event does in the example.
I did comment every single event in the example. Not sure if I could do any better.
Is there anything specific that you don’t understand or you just don’t get it the whole thing as-is?
Yeah the id counter, I dont get how to assign an item to a slot’s id.
The “id_counter” variable is used only to generate an ID value for each inventory slot so we can identify them.
So instead of go and manually add an ID value to each slot what I did was
id_counter = 1
Repeat for each slot
Do = id_counter to variable ID of slot
Do + 1 to id_counter
The reason I did this way is to make it flexible and be able to add and use any number of slots.
In case you don’t understand what I just wrote, you need to learn the fundamentals first before trying to make an inventory and I’m afraid I can’t explain it to you right here, right now.
The ‘ID’ variable of the slot is used exclusively when saving and loading the inventory, specifically to identify the slot when we load the data. So for example if we load the data of Slot with the ID = 3, then we go and find the slot with ID = 3 and apply the data on that slot and move on to the next. That’s it.
Again, if you don’t understand, you want to learn the fundamentals first.
So the id_counter and ID variables got nothing to do with assigning an item to a slot.
To assign an item to a slot is done in a very simple way. Each slot object got multiple animations, basically all possible items included as an animation for each slot. So when you wan to add an item, all you do is find a slot with empty animation and change the animation of the slot to display the item you want to add to the slot. The animation names and item names are the same. So if you want to add an ‘apple’, you switch the animation to apple, it is that simple.
The actual inventory data is stored in the inventory system provided by GDevelop. It is works in a very simple way, you can add and remove items, check quantity of items and set maximum quantity of items. It is that simple and you can use this without a GUI just to easily keep track of items in the background in your game.
I use the inventory system mainly to keep track of how many items I have in the inventory and I use that to display the quantity in the slot. The way it works is very straight forward, if a slot display ‘apple’, I go and check how many ‘apple’ item I have in the inventory system and display that value using a text object at the slot.
Text objects are also created for each slot at the beginning using a for each loop similar to ID values and I use object linking to link text objects to slots and this way I can find the text object of any slot. So if I want to update the text object displaying the number of apples, I go to the slot displaying apple, go and find the text object linked to this slot, and then go and get the value from the inventory system provided by GDevelop.
Again, the reason I do it this “complicated” way is to be able to use any number of slots. I was trying to make the inventory in the example as flexible as possible.
But I have explained all this in the example and I don’t think I can explain it any better I’m afraid, I’m almost certain what I just wrote makes no sense to you. It is not that simple, you want to learn the following topics in order to understand it better:
-sprite objects and animations
-storage events to store and load data
The fact is, the inventory system is not even necessary actually and originally I was using object variables to store the number of items for each slot. Basically each slot did store all the information about the item it was stored. But after we had this “new” Inventory system feature added to GDevelop I felt I need to update my example and I replaced the object variables with the Inventory system added to GD to store the data. Just in case if you or anyone wondering why I’m not using object variables for this. I was originally, and to be honest I liked that version better but as always I received no feedback if the one with the object variables or the one with the inventory system was better, so I just kept the one with the inventory system.
So, what i’m trying to do is make two inventories. The player’s and a chest. I want the chest to automatically spawn with an apple in the beginning. Everything I try though doesn’t work. I had been studying the saving and loading and noticed that I can’t make a specific slot by id. What I was wondering if there is a way to call a specific id. Or if you could help me with what I’m trying to do, that would be great…
I’m going to put you in the credits to my game if I ever get it finished. Thanks for the help.
If you haven’t already, you need to create a separated copy of the inventory for the chest. I did not make the example with more than 1 inventory in mind, so you need to do some editing, I don’t have time to show you how to do that right now. Basically you need to create a copy of the slot object, the slot_text the inventory with a different name, something other than “player_inventory” and also change the names everywhere in the events for the chest.
Then you want to add this events to the very bottom of the Initialization part of the inventory to add an apple to a selected slot and selected inventory.
Replace “player_inventory”, “inventory_slot” and “slot_text” with the ones used for the chest inventory
It is going to add an apple to the inventory and pick the slot with ID = 1 and make it display an apple and also update the text object to display the quantity which is 1. If you want, you can add more apples to the inventory, the event for the slot is the same and if you want to pick an other slot just pick a different ID value. If you have 6 slots you can choose ID in range 1-6 for example.
There is probably a better way to do the whole thing, maybe I’ll look in to it when I got some time.
Thank you so much for your help.