How to make a Hotbar System for a Survival Game

Hello, im trying to make a hotbar system for my top-down survival island game, i tried to make the system but when i pickup a item, the item deletes ang goes to hotbar on the first slot, ok, its deleting the object, but this is not happening, its creating in every single slot and he gets one item and not more, its weird…

Code With Hotbar “Bug”

I don’t know what to do, I’ve been trying to solve it for weeks and I’m not getting very good results.

I’d love to try to help but I don’t speak the language in the screenshot. Could you also post it in English? You’ll double your odds. Thanks.

Do you want each slot to hold a single item or do you want items of the same type to be able to stack multiple quantities in the same slot?

The way the events look, you want a single item in each slot and it will fill every slot with that item if the isFull bool is false. They’re all false so it fills them all.

I am curious to see your event for changing the text object for the quantity of each slot to see what’s going on there.

I wanted them to be stacked (without limits), and if the item does not have the same id as the item in the slot, it would go to the first empty slot, like, the wood is in slot 1, and I go to get flint, it goes to the first empty slot
btw the command where it shows how many items are in the slot is easy:

Well I can’t get the text to update the item quantity on mine yet, I didn’t pass the variable like you did.

For the other, you can try something like this:

It is just an example, you don’t have to look for the existence of a child in a structure as a condition. And in fact I don’t recommend it because special care must be taken not to create the child accidentally with other events before the user actually picks up the item . You can use another more convenient condition to check if one of your slots already has the item in it, then do that action. If none of the slots has that item in it but there are some free slots, then do that action, and if neither of those conditions are true it will do nothing because of course you didn’t tell it to do anything in that event.

Okay, I’m going to test this example you gave in the screenshot, I’ll update you when it works.

Alright I hope it goes well. I got the text working finally. I put the example and event sheet here:

Hotbar Example by lucky-j (itch.io)

I saw your example on itch.io and downloaded it to test. Well, I tried it and it works (btw good example) , but the items aren’t being stacked, I don’t quite understand how gdevelop’s child variables work (I’ve never used it), do they work as a “temporary” variable? But what matters is that I’m one step closer to having a working hotbar. :slight_smile:

image

That’s great you’re almost there. What does your event look like now? By stacking do you mean changing the quantity or actually creating duplicate objects in the same slot? I set mine up just to change quantity.

Child variables are just like normal variables but they’re inside of a structure variable or an array. You can use them like normal variables. What I meant by taking care not to create one accidentally is just because of how I was using the existence of a child to test for an item already being in a slot. That’s why I think for your game it would be easier to use a condition better suited for what you got setup.

My Hotbar event looks something like this:

I’m still trying to understand some things, but for now, it’s almost finished, I just need to understand how I’m going to make an item that’s in the hotbar increase the amount of it and also decrease when an item in a slot is used, I’ll be waiting for you :saluting_face:

In that version it won’t change the Amount variable because the condition is that isFull bool is true, but that whole thing is a subcondition requires isFull bool to be false.

Pick the Slot nearest to… should be a subcondition to 2 other condition. First you’re checking that the item does not already exist in a slot. If not, then subcondition Pick all Slots bool isFull is false. Then subcondition Pick Slot nearest to… So first you’re looking, does this item not already exist in a slot? Ok then lets see if we have any empty slots. Ok we do? Then of those, pick the slot closest to this coordinate.

But probably it is all solved by now lol.

well i tried to do what you said, i didn’t understand very well, because it’s kind of complicated to make a hotbar, i did it and it’s still creating instead of stacking

(by the way, I tried to do something using my logic, but it’s the same thing)

You’re doing good so far. The second condition looking for a slot with the item already in it can go before the first condition of creating an item in an empty slot. Otherwise the game will first look for an empty slot and create an item in in, then immediately look for a slot with that item and add more to Amount. Also for that one maybe do a Pick all slots isFull = true, then subcondition variable item id of Slot = ItemDrop then action change amount.

Well I did that, and even so it’s creating the same item in different slots and not stacking, and the system of showing the quantity is “working”, kind of like when I take an item it adds it normally, and when I take another item, like wood, it doesn’t register and leaves it as zero, but it doesn’t count as the other id of the item, so that’s good, but I want to let it stack and not create other items and leave the inventory full, you know? This will be the first time I make a working hotbar! If possible, could you give an example with the variables in the objects, I’d really appreciate it

Ok so walk me through your setup. Your Slot is a different object from the ItemInv. All your items are different animations of ItemInv? You’re storing all your variables in Slot? How are you collecting objects I assume named ItemDrop and how are you deciding the ItemDrop.Amount? Anyway I can see what I can do tomorrow.

Okay, each slot has an instance variable called ID, this variable determines each slot, the ItemInv is an object that has its ID , Quantity and others variables, the most important are the ones I mentioned, each item has an animation according to the id of the object, 1 = Wood, 2 = Stone, among others, and how I collect the items, I use an extension called boids movment, which makes it so that when I’m near the ItemDrop, it chases me until it collides with me, so when it collides with me, it picks up the variables and takes them to the empty slot (ID to Slot.ItemID, amount to Slot.Amount)

I’ve explained most things well, i think, I’ll see you tomorrow.

Mmkay this will have to do for now because I have no more time to give it tonight. So go to the link I gave above and you’ll see a second download called HotBarWithObjectVar. I have not proofed so it might have small bugs but the overall thing you wanted to see should be working.

If you view the preview to see it in action, use the arrow keys to move the basket to the items. Yes I tried the Boids extension. I just can’t. So had to just use collision instead.

On the left side of the screen you will see some ItemIcons, one for each Item. I didn’t know what to check since I couldn’t use a scene variable to see if the items already existed in a slot. So instead I’m just using a collision like this:

When the quantity is back to 0 the ItemIcons move back to their spot. You would want to hide these of course I just left them visible for you to see what’s going on. You can probably find a more clever condition to check for but I was too tired to be clever haha, so just using that as an example so you can see the order of events.

If you want save events I will add them tomorrow night, I won’t have time to write them up right now especially since we’re using individual variables.

How can I thank you, man, thank you so much! Everything’s working fine now! Thanks to you, really, thank you so much, now I’m going to add more items and work on the rest of the game, thanks again

image

1 Like