How do I make when I click on this; it goes into the inventory bar: ?
try using lists!
In what way?
You can use a list to symbolize the inventory, with the first space being the first slot in the inventory, second space being the second slot in the inventory, and so on. When you click on it, it can add a 1 (maybe 1 represents this object) to the end of the current list. Then, in the inventory slot bar, you can check what number it is, what place it’s in, and put the correct object in the correct spot.
When making an inventory you need 2 basic components (or at least that’s how I do it);
1: an internal representation that stores data, usually a list of what objects you have in your inventory, how many of each item, their position in the inventory, etc.
2: an external representation that the user can see
The first component is easy enough;
- have a number or text list that represents the current items. If you want an amount for each item, you can have both a text and number list with the name/type of item in the text and amount in the number list, like so
(Item ID) (Number of items) Text List index in list Number list "Apple" 1 3 "Sword" 2 1 "Potion" 3 4
Here the “index in list” has to be the same for both the text and number list; therefor your logic has to handle that.
Note that the text list could also be a number list instead, replacing the text names with ID number values of your choosing. This also makes it easier, since numbers can be easily sent between objects with the message behavior
For simply adding an item, here is how I would do it
- Have the trigger code (for your example clicking on an object) send a number value to the inventory object. This number value would be your ID for what object you want to add
- When the inventory object receives the message, search the ID:
- if the ID is found, simply increment the corresponding value in the item count list
- if the ID is not found, push a the ID to the end of the list (using the push input) and then repeat this step
That’s pretty much it actually, then you just need to update the display to show the current number of items. There are a bunch of different ways you can do that, but I think you can figure it out. Just create a UI object that can display an image of the item in your inventory and then use a label to show the amount. I do something like that in the example game below: