How to load inventory?


I am still unable to load the inventory

Remove the trigger once

removed but still not working

How are you saving the inventory? Are you sure it’s being saved and with the JSON format?


this is the mechanic for saving and below as you can see it does shows the objects in the save variable so i guess it is saving properly
image

The inventory variable needs to be saved to storage in the JSON format. In the save to storage action you can use
ToJSON(Playerinventorysave)

https://wiki.gdevelop.io/gdevelop5/all-features/inventory/#savingloading-inventories

Hey,

Where are you saving your values ? You have to save / write before you can load / read out of storage, and you will need trigger once because in your current situation your events will be true multiple times in a row without it and you don’t need events to be running every frame, which is bad for optimization.

The first initial value of Player structure is 0 , it has nothing in it, so I just add something by pressing ’ k ', then I save that value by pressing ’ s ', and then reload the game and the value still stays cause it is in memory, if you want to clear / reset, then that’s ‘c’. I think if you try that way you will be able to save and load every value from your structure at the same time, you will have to change few things in the events to suit your needs as that is just a quick example.

Hope this helps a little bit.


i have done it already

i tried but it’s not working

to save inventory
inventory → string variable → string variable with Json text → storage

to load inventory
storage → string variable with Json text → string variable → inventory

this add 1 Gold to “bag” and saves it
it then loads the bag and adds the quantity of gold to the debugger

it is saving and loading but i am not sure how to get it to show the items when loaded

I don’t know how you’re displaying it but you can use for each child with the inventory structure variable. Either from the load variable or from the inventory.

This displays the items in the inventory. In this example the for each child sets each child structure to the structure variable named item and the item name to itemName You can use the values in the structure to create objects or change animations or whatever.

scene Variables

result
image

source (click the green [code] and [download zip] unzip and open json file.
https://github.com/doug13579/gdevelop-inventroy-example-2024

Edit: this is the same code but it displays the inventory from the inventory system instead of using storage. I used the variable bag for this part. It’s best to use different variable names in case you need to debug also, if something fails and diesn’t change the variable, the previous value of the variable will be used.

edit: I don’t know if it’s a bug or my code but the max count doesn’t seem to display the when displayed from the inventory system but it does when loaded. If it saves and loads then it should work from the inventory system.


this is how my inventory works so how should i implement the method you did into this?

I updated my project. inventory example on GitHub

This uses an object variable named ID on what I named slot. I changed the ID instance variable for each slot to 0 through 6. This could be done at runtime.
I added a scene number variable named counter
There are 3 objects: slot is a sprite and 2 text objects count and name
slot has an image of each item with matching animations; Gold, Silver and Bronze.

events (press l)

The inventory isn’t cleared before adding the items in my save events. So, it will add the items again. It uses a max Count, so it will only add up to 10 of each item. That part doesn’t need to be used.

This is how I setup and linked the text objects to the slots

result
image

I did the way you did but it just loads any random item with 0 count
and if i pick anything after that it just shows random numbers

Can you post a screenshot of the events?


here
Oh and one more thing because the code doesn’t loads the variable into inventory the other stuff i have made to work with the inventory doesn’t seems to work i even tried loading into inventory but still same result

In my example, I used an object variable named ID that’s numbered sequentially starting at zero on the slots as a way to pick them with the counter variable. It doesn’t matter how you decide to pick them in your version of my slot. It just needs to pick one at a time. Once the image sprite object is picked, the linked object would get picked from it.

I Honestly don’t know where the random number came from unless you put the number in the text object’s txt property instead of an object variable named ID

So i did that now it is not loading anything and my inventory slots are given numbers this way

I’m still looking at your events but I believe

Item.Count should be Item.count
(lower case c in count)

As it is in the inventory structure
image

Are the animations named the same as the inventory names including case? Example: if the inventory has Gold then the slot needs to have the animation named Gold.

I’m not really clear about your player setup. How many players are there and are the inventories saved separately? Also, just a heads up. IDK about inserted external layouts but the for each object cycles through the objects in the order the objects were created. I’d have to test it. I’m wondering if that applies to how the objects were created inside the layout. IDK how the text object have numbers but that would explain why the numbers aren’t in sequence. I’m wondering if you made the external layout by copying and pasting objects. That’s what I would’ve done. But that could’ve shuffled the order of the objects. I also don’t know if you have the same images IDs for the other players. That could cause problems. Or maybe all of the inventory slots just keep getting a larger number. So, if there was 10 slots then player 1 could be ID 0 to 9, player 2 could be 10 to 19.

Are you saving each players inventory to separate storage? Maybe, the inventory sprites in the layout need to be numbered manually say 0 thru whatever and if each player has their own storage then they might need a second variable for the player number.

So, each player inventory image would have an ID of 0 to whatever and a player or playerID variable that matches the player number or name.

I’m just making guesses since I’m unclear about the multiple players. As is, they could be mixing or overwriting things.