Saving and loading an inventory (SOLVED)

Greetings,

I’m working on an inventory example, and I want to make it as reusable as possible.

The logic for collecting an item is in a function call, and I want to have the function change the animation of the slot as well (There’s no logic to tell it which slot to put it in for now, so it should change all slots to the right animation for right now).

What I tried here didn’t work. The slots remain empty.

Basically, I want to make something kind of similar to this inventory logic (Baldi’s Basics Classic btw). When the item is collected, change the animation of the slot based on the items ID within the function itself (Then update the name using a nested function call):

Does anybody know what I’m doing wrong and how I can fix it? Or is this not at all possible with GDevelop’s functions, would daisy chaining external events do the trick instead?

Thanks

What makes you think that this wouldn’t be possible?
Does the inventory count get updated?
Are you sure about your ToString(quantity)? Repeat should expect a number. :thinking:
Try to apply other actions to invCell, like changing tint, or adding an object variable, and check if that works any better than the animation action. :person_shrugging:

2 Likes

Thanks for your response, the ToString() WAS indeed the issue, and now items are displaying like they should. With that out of the way, I moved on to saving and loading the inventory.

However, now I’ve run into a new problem. When I save the inventory and load it back, the items are all put in the wrong spots, items I didn’t collect are placed in the inventory, and the numbers just keep increasing, even for empty slots.

The debugger isn’t pinpointing the issue either. Here’s the saving code (External event):

And here’s the loading code (Also external):

I tried to change some of these values to not include ToString(), but they insist that they NEED toString() to work, despite being integers.

I can’t seem to pinpoint the issue here, can you see what’s wrong with the code?

Thanks

And in case you’re curious, here’s the aftermath of what happens when I load:

========================================================================
EDIT: I fixed a little bit of it, I changed the saving code up top from “Read a Text” to “Read a Value,” thus allowing me to remove the ToString()

The slots still fill wrong and the numbers still increase, but the empty slots don’t increment anymore

Any help is appreciated, but I’ll keep tinkering around in the meantime

If you need to see my project file, I’d be glad to share it

Okey, so I struggled and brute forced with it a bit more, and after a headache of a time, I FINALLY found a near solution!

Turns out, by simply using the function that I built earlier (With your help ofc), I was able to get this inventory working almost perfectly! It even makes the code look cleaner!

Although, notice how I said ALMOST perfectly. There’s still one last tiny problem…

It’s a little hard to explain, so how bout I show you right here (Sorry for the crap quality, I had to compress it quite a bit):

So basically, you can see me collecting the items, and they’re going into the inventory. I then save the inventory (The green status text above indicates the save)

Afterwards, I restart the scene. I made it so that the game runs the load external event at the beginning of the scene so the saved inventory automatically loads in

However, you’ll notice that the quantities of each item in the inventory is DOUBLED for some reason. Once I manually load the inventory (Also indicated by purple text above), then the inventory is restored to what the values are supposed to be

I’m not sure what’s doubling everything, but it’s definitely not right. Can you please help me on this? I’ll share more code if you need it

Looks like you made good progress :+1:

Regarding that new issue, hard to say…
I suggest you try collecting, saving, and spending items before restarting/loading.
If you get double like before, it means the loading is somehow doubled. If not, I guess it means the loading is added to the existing count…maybe. :sweat_smile:
Fiddle and fiddle some more, and you’ll find. :wink:
If not, I’ll need to see more events indeed.

It’s looking good. Are you clearing the inventory before you reload it or adding it to the existing inventory?

Edit: I watched the gif again. Do the values double and then return to the correct values?

I’m not sure about the trigger once without any other condition. [Trigger once] in loops doesn’t usually work as expected especially without any other conditions.

Thank you for your response

I tinkered with it some more, but I just can’t pinpoint the issue. I’ll share my code right here:


The scene events (Gonna move loading at beginning of scene to the game manager external event in the future for more convenient use)


The game manager, specifically, the saving and loading part (The option to delete the save is down below, and it runs the load game event to instantly update the inventory to empty)


The finished function from earlier


The external event for saving the game


The external event for loading the game

That’s roughly all the saving code I have, anything stand out to you?

Also, thankuu

The values are doubled when a saved inventory is loaded after restarting the scene, but once I manually load the game, they’re back to normal. I’d like for them to be normal automatically

So, did you try what I suggested earlier? Spending items to check if there’s any difference?
I don’t notice anything wrong, except for what Keith suggested, which I’ve been told is a bad combo.
Try disabling some events too, see where that leads you.

Alright, I’ve deleted all of the standalone trigger onces, and found some redundant code to disable too

Unfortunately, it didn’t change anything. Everything still doubles regardless

I’ve disabled all of the events that I thought were the culprit, but to no avail. Nothing I tried worked

So I decided to try testing your theory on spending items to remove them from the inventory. However, yet another problem arose (Note: That Trigger Once in the events is no longer there):

As you can see. for now, I just have the code removing any item you click, regardless of what it is. The code on the way bottom, just above the external event makes it so that if there’s nothing in that slot at all, change the animation back to empty, and set the quantity to zero

Unfortunately, when I boot the game up, EVERY slot is empty. When I try to load, the items saved from before show up for a splint frame, then vanish again. So apparently, the game it TRYING to load everything back in, but this code isn’t changing the ID variables back and deleting everything again

I tried to see if the values were increasing correctly in the debugger, but now a third problem arose. The debugger doesn’t work anymore for some reason, so using the debugger is out of the question:

First, I need to fix that issue with the code that sets the slots to empty when there’s nothing in them, but I don’t know how to fix it

(Also, disabling this event and clicking on items DOES remove them, but the animation never switches back to empty. Also, the load problem still isn’t fixed either)

=====================================================

EDIT: After looking through the pre-made inventory example bundled into GDevelop, as well as another inventory example I made a while back (I’m starting from scratch with an improved version if that makes sense), I’ve noticed in both that they and I both used strings to track items, as well as using animation NAMES instead of NUMBERS to track data.

Truth be told, I have an older inventory example similar to this one I’m working on now, I wanted to remake it from the ground up to smooth over the flaws, enhance it with new features, and just give it a facelift (I even tried the function in the old version, which doesn’t work right. Getting the function working in this version might work in the old version if I make the changes).

The example I’m working on right now, I’m trying to switch to integers instead of strings because I figured numbers would be easier to work with.

Both the bundled example and my old version work just fine and don’t have the issues I have right now, and since the expression:
Inventory::Count(string, string)
requires two strings, I feel like trying to convert them to numbers might be more trouble than I thought.

I added names for the animations of the items and inventory cells just in case. Would you recommend I use primarily STRINGS for an inventory over INTEGERS?

Well I would use strings because it would be easier for me to remember what “player foods, cake” is than what “892054, 95473” is. I have been pouring over your problem but just can’t see where the error lies.

:smile: You seem to be drowning in issues, take a break!

The problem Keith mentioned regarding the Trigger once was about using it inside a repeat loop, I believe. Just saying because I see one in your screenshot.

I just remembered there used to be a bug about external events being triggered twice when the preview was started from certain menus. I wonder if it could be connected…

On a side note, if the debugger doesn’t work, this is what you should fix first. :sweat_smile:
Are you on Windows? Running the latest version of GDevelop?

1 Like

Ahh, how frustrating… I just restarted GDevelop, and now the debugger works just fine

So, I ran the debugger, and it’s not giving me info on the item’s quantity

This is after collecting items and saving (Cake indeed has an ItemID of 3, in the first cell, ID 0)

And this is after restarting the scene, where the values are doubled (Until a manual load ofc)

I just realized that there’s no variable on InvCell that stores the quantity, which is weird since my old example and the bundled GDevelop example work just fine without it…

Would you recommend that I stuff another variable into the mix to store quantities in an object variable, or should I convert the integers back into strings?

Or perhaps both?

Okey, so I backed up the project just in case, and replaced all of the integers with strings instead where it was needed. In the end, the code functions exactly the same as it did before, it’s just easier to work with now.

Although, I think I found a solution to the double inventory for now, and I’m embarrassed by how easy it was:

All I had to do was repeat the load code twice, then it resets everything to the right value instantly. No more double quantity issue, hooray!

Additionally, the issue with the inventory showing up as empty all the time was fixed too. I just forgot to change some integers back into strings, oops

Now the inventory is working really well, a huge upgrade of my old one

Thanks so much for all your help. I’m going to keep upgrading this, so if I get stumped again, I’ll let you know

2 Likes

I have been pouring over your problem but just can’t see where the error lies.