Inventory system with arrays

WARNING: LONG POST

Greetings,
To practice for more serious projects, I’ve decided to go to one of my older games and try to improve the inventory system I previously made in it.
It’s quite faulty, and suffers from several problems: Can’t arrange the items properly, doesn’t save between scenes, VERY unoptimal and messy code, etc (This was back when I was still a GDevelop amateur). But for now, I’d like to focus on trying to make the inventory arrange the items properly.

I tried using the built in inventory system, but the way my inventory is planned to function, it doesn’t work too well for me (Or at least not the way I’ve been using it). So I would like to use an array to sort my inventory (Or a structure variable in this case). It works how you’d expect: There’s slot 0, slot 1, slot 2, and so on.

So, what I would like to accomplish is to make it so that if I collect an item, it’s automatically put in slot 0. Once I collect another item, it will be placed in the next empty slot (In this case, slot 1). After collecting a third item, put it in slot 2, and so on.
If a slot is already occupied by another item, move it over to the nearest empty slot.

For reference, each inventory slot has animations according to what item it’s holding (Animation 0 = empty, animation 1 = item #1, etc.).
So far, this is the best I was able to do at the time. When the item is collected, add it to an inventory, and if the item exists in the inventory, put it in a random slot.

First screenshot: When the character collides with an item (The item changes based on it’s variable. There are more than 100 items btw), put the corresponding item in the inventory “MyStuff.”

Second screenshot: If the item exists in “MyStuff,” place it in a random slot (Notice my previous failed attempts to have the system globally loaded across scenes above the red comment).

So this begs the question: Can I make my inventory using a structure variable instead of the built in system? If this is already possible with the built in system, then how do I do that?

Also, unless necessary, please don’t just link me to the inventory example. That inventory has it’s slots mixed up, and I don’t need the extra code to drag, delete, or have multiple items (In my game, you can only hold one of each item at a time, and you can’t drop items once they’re picked up or drag them around).

Thanks in advance

Sure, why not?
But don’t bother reinventing the wheel unless you have to. :slightly_smiling_face:
Using the inventory functions will probably make things nicer to read and troubleshoot too.

Can you be more specific about the “this” and the “that”? :grin:
Here are the available actions:
image
And the conditions:
image
Are you missing anything?

I don’t think I’m missing anything.
After all, this IS one of my scrapped projects. To practice for later projects, I came back to it to try and fix all the problems that made me scrap it in the first place, starting with the crappy inventory.

Or maybe it’s because my brain is the size of a walnut and I couldn’t wrap my head around the existing inventory example. Perhaps I should take some time to practice recreating the example instead.

As you said, I probably shouldn’t bother reinventing the wheel unless I have to. And in this case, I most likely don’t have to.

I’ll tinker around with the inventory example a bit more to fit my own desires. Thanks mate ^^

I never checked the inventory example, but if you improve it, feel free to share the updated version with everyone. :wink:

Okay, I’ll see what I can do.

Hello,
I have the same doubts.

What I want is:

  • pass inventory between scenes;
  • save it for future plays;

I wasted a lot of time but I haven’t found the solution.
I have tried a thousand variations of the command

But it often works or doesn’t work or it works only the first time (it seems like something remains appended).
I believe that inventory is the only way to manage an arrays without declaring theyr length but I am unable to exploit it.

If you find a solution, please help us.

Hello,
I found a solution to maintain the Inventory from/to a scene.



it works !!!

Now I’d like to loop each element of the inventory but I don’t find a command.

Something like
Repeat (number of element in Inventory) times
do something with each element

Have you got any idea ?
Thanks,
J

If you use a variable, use the VariableChildCount() expression.