[Done] Request explanation on inventory system

When starting a new project you can select a template and there inventory system template.
The problem is, I don’t understand the event like where items.Varstring(name) came from. Where the var name store animation name without the item_.

How the animation are in order.
When I do
Trigger → set animation of inventoryslot to banana
All inventory slot animation become banana( in my version, not this template)
Which I wonder how the template do it.

I’ve been looking at all the for four days but still can’t figure out.

2 Likes

In the template we have a scene variable called “item_picked” which is store the name of the item we picked and each slot has an object variable called “nameofitem” which is store the name of the item stored in the slot.
When you collect an item, you assign the name of the item to the variable “item_picked”.
Then, we check if the value of “item_picked” is NOT = “” (nothing) then it means we have picked an item and then we look for a free slot that hold no item so the value of “nameofitem” of the slot is = “” (nothing).
If we find one, we set the animation of the slot to be the same as the value of “item_picked”, also add item to the inventory data and finally set the value of “item_picked” to “” (nothing) to indicate we have nothing picked any more and we no longer going to look for a slot and add the item to one slot only.
We also set the value “nameofitem” of the slot to the same as the value of “item_picked” as a sign, the slot already hold an item and what item and this is how we find a slot that hold the same item already that we picked by comparing the value of “nameofitem” and “item_picked”. If it the same, then it means the item we picked already stored in that slot.

How about animation ? How to make sure only one slot turn animation to nameofitem ?
Why don’t you give me simple example of having 3 text but they are the same object , and when you press q 3 times one by one will change text to something (you can use animation example)
I’m trying to make quite an easy inventory menu.

As I mentioned, there is a scene variable called “item_picked”. By default this value is empty, has no value just an empty string “”.
When you collect an item, you assign the name of the item to this variable. If you collected an “apple” you assign the value “apple” to the variable “item_picked”.

If the value of “item_picked” is NOT = an empty string “”, ONLY THEN, we look for an empty slot.
If/When we find an empty slot, we set the animation of the slot to be the value of “item_picked”. If the value of item_picked = “apple” then set the animation of the slot to “apple”.

The important bit, After, we set the animation of the slot and do everything else, we set the value of “item_picked” back to an empty string “” to indicate we no longer have an item picked because we just added to the inventory so we no longer have it, we no longer looking for a slot.
Because we no longer have the item, we no longer looking for a slot and so we are not going to change the animation of any other slot simply because we no longer looking for one.

I’m afraid I can’t explain it any better. If you are not familiar with variables and object variables and expressions and loops, I recommend to start with the basics and try simple things first.

You can find an example here at the bottom called “display-inventory-data”, the example uses nothing but text to display inventory and inventory events:

Thanks. I guess I need to take more time to understand it.

Sorry I could not help. Maybe it is that you are over complicate it. It is not difficult at all. The game is being refreshed 30-60 times a second which means every single event is executed that many times. So when we set the value of “item_picked” to an empty string, at that point we break the execution of the event and the actions to find free slot and change animation of slot will no longer execute.

If item_picked is = “apple” THEN
Is this slot free? No
Is this slot free? No
Is this slot free? No
…repeated 60 times a second until
Is this slot free? Yes

At this moment, when the answer is Yes meaning we did actually find a free slot, we set the value of item_picked to be an empty string “” and no longer be apple. Because of this the execution stop and we no longer looking for a free slot and we are not changing the animation of any more slot because we no longer have the apple.

I did make this more dynamic so it is going to fire for any item not only apple but this is how it works in a nutshell.

Oh. So it checks each one
If item_picked is = “apple” THEN
Is this slot free? Yes

I think my event might check all slot is free at once then all turn animation

Yes, you need to use to For each object event to force it to check the conditions and execute the actions for each individual slot.
But even then, you do need a way to stop it and this is what the “item_picked variable is used for”

But indeed the inventory example is way too overcomplicated because I have redone it 3 times.
Originally it was using nothing but sprites, then I decided to use structures to store the data and finally I have redone when the inventory events was added to GD. So nobody really understand it.

So I have made a more simple version that using nothing but sprites like it was in the original:
You can download the example from here (simple-inventory-system):

Hope it helps.
Good luck :+1:

1 Like

need a solution for this

Capture1
I dunno what var should i add. those slot u see have obj var call “hasitem” = 0
i use “for” for individual check. but as u say, need something to stop it(or second condition)
The sword is like a global item with in inv system for now
Any idea?