Check for the first empty slot in an inventory

Greetings,

In my game, I previously used global variable checks to see if the player has certain upgrades. This time, I want to rework the system to checking if the item exists in an inventory instead (And show them in an inventory screen). So I got to work on some iterations.

However, I’ve been having unsatisfactory results so far…

(If it helps, all of these iterations are nested under a “Repeat for each Upgrade” block)

MK 2: Function call

Here, the game DOES add the item to the inventory, but it doesn’t display on my inventory screen…

This one is peculiar because I modified this from a previous example I made where this DOES work. Did something break? Or did I set it up wrong?

MK 3: Extract the function call to the events

This is roughly the same as the function call, but modified so that it works outside of it. This one, however, doesn’t work at all. The item is not added to the inventory, nor does it show up on my inventory screen

MK 4: Default inventory example

This is basically a recreation of the default inventory example bundled into GDevelop, retrofitted to fit my game’s mechanics. The results are the same as previously, the item is not recognized as being added, nor does it show up in my menu…

I’ve had the best results with MK 2 so far (The function call), and I’d personally like to fix that one in particular to keep my code small and reusable (If possible ofc).

I’m totally stumped as to why nothing is working. Can anyone see something I missed? I’ll appreciate any help I can get, thanks

UPDATE:

I decided to try other options while waiting for a reply. So in a last ditch effort, I tried to reinvent the wheel and use an array/structure system for my inventory

MK 5: The old fashioned way

The function to update the inventory:


(I’m aware of the error text btw, functions don’t play nicely with global or scene variables)

So far, it’s not working right, but it’s functional. I’ve run into a few issues though

Issue 1: The upgrade’s quantity isn’t increasing

Issue 2: The upgrade went into the incorrect slot (Each slot has an ID, ordered left to right, up to down). However, it’s only going into the first placed instance of the slot, rather than the one with the smallest ID

Issue 3: When I reload the scene, the inventory’s slot animation does not persist (I have the function run at the beginning of the scene)

Issue 4: When I collect the upgrade, it doesn’t show up in the inventory. However, if I reload the scene and collect the upgrade again, it now shows up (This only happens if the game is in fullscreen)

Is a global structure the right way to go here, or is there still hope for the other designs I made? I feel like I’m onto something, but I can’t quite get it to work right…

If anyone could help me iron out the MK 2 or this design, I’d highly appreciate it

Thanks

UPDATE: I’ve tinkered some more, and I’ve noticed a difference between the default inventory example, and my version

The default inventory example uses separate objects in a group, wheras my game uses one single object with different animations

Default example:

My game:

Could this be the issue I was having? Do I need to extract all of my upgrades out of a single objects and just make them a group instead?

Ofc, I’d like to clump this into a function call later, but I’ll cross that bridge when I get there. Right now, I just want to make this work

Am I on the right track? Anyone have any pointers for me?


EDIT: It turns out, I got the expressions wrong
I was using (Inventory::Count(String,String)), the expression used for the built in inventory. However, the Simple Inventories extension instead uses (Inventories::ItemCount(String,String)) instead!

I was referring back to an old inventory example where I used the built in inventory, and I totally missed this change, oops

Anyways, I made the necessary changes, and now it works!!

We still have a small problem though
The upgrade is still going into the wrong slot

Again, all of my slots are ID-ed from 0-23, from left to right, up to down. I would like my items to enter the inventory in that order, basically new items enter the slot with the smallest ID

I COULD just order the slot instances accordingly, but I would like to have the engine do that automatically

Is there anything I can do to make the inventory order the items right?

Oh yeah, I’m currently using the MK 2 design if it helps (The function call)

Ignore the disabled code, I have code elsewhere that changes the quantity text:

UPDATE: I still don’t got it working, but I might be onto something?

Here, I have a local variable called “i” that I want to check each slot’s ID, starting at 0, and working it’s way up

If the slot is currently full, skip it and add 1 to “i”

If the slot is empty, then add the item there

However, I can’t get it to work. It still prioritizes the first placed instance of the slot. Is there something else I can do to check for empty slots in order of ID?

EDIT:
I’m close now, I can feel it!

I changed the code to do this:

Good news: The upgrades are going into the leftmost topmost slot (ID 0), just like I wanted them to!

Bad news: This only works for the first upgrade. Every different upgrade I collect after that doesn’t show up in the inventory at all…


I’m so close to cracking this. My theory is that “i” isn’t skipping over filled slots?

Is there anything else I’m missing?

EDIT:
I think I finally did it. I just added this code:

This code checks to see if the item you picked up is NOT the same as the slot being checked, and NOT empty, then increments until it finds an empty slot. If you already have the chosen item in your inventory, then simply add it to the existing one

I’m 99% sure I got this to finally work, but if I have any more questions, I’ll come back here

I never did inventory so do not expect anything more from me than what i write below

But you took wrong approach in my eyes
IDK how it works but sometimes you just need to take most stupid approach

There is condition Pick Nearest Object
And you specify which object and nearest to what position
So if your inventory is always on screen like top left part of it
Then how about
Condition
Slot is not occupied (however you check if slot is free)
Pick nearest Slot to position CameraBordrLeft() CameraBorderTop()

And that would pick closest empty slot to top left corner of your viewport