File Size algorithm.png 384KB Array based inventory.capx 83KB

## Translations

Know another language? Translate this tutorial!

# Array-based inventory

Favourite 67 favourites
Tutorial written by WaltanOriginally published on 7th, July 2013 - 5 revisions

## Creating inventory in array

Let's create some items in our inventory array.
Add to the second slot 21 coins and to the fourth 3 apples just like there:

Your item ID must be item.animationframe + 1. In this case, apple will have ID = 1, as I said before.

Here comes the hardest part of this tutotial. I promise, will be easier further %)

Now we need to visually display our inventory.
For creating visual part Im using this algorithm:

(check for full size in attached files)

But all is not as difficult as it seems to ( or not :D )

### What this algorithm does:

- Automatically creates a new line for your inventory slots. If you want 5 slots in one line and 10 slots total, it will automatically split it.

This formula is probably scariest thing throughout algorithm :

Let's examine it in more detail:
X = (((inventory.curx)-(sn(a-1)))ss)+tx

- inventory.curx - current X index in a For Each loop

- sn - number of slots in one line

- a - just some variable for calculating. It is increased by 1 each time when inventory.curx MOD (remainder of division of one number by another) sn = 0

- ss - slot size (32 + border between slots (2)) = 34

- tx - my algorithm is not perfect, it is necessary to subtract some number from the X and Y coordinates if you want to get true coordinates. tx and ty are this "some number" to subtract.

Here is formula to calculate tx variable:

This algorithm is fully exposed to criticism. I will be very happy to hear your feedback to improve its performance :S

So, at first, it creates an inventory slot and sets the values ​​of the variables according to the data in the array.

/ A small correction. Despite the fact that the inventory array starts at 0, the slot's ID we will start with 1. /

After, it checks for the object in the inventory by checking value at inventory.curx.
If its > 0, then object is exists and then it creates it.
That's why items ID are by 1 more than it's animation frame.

At the end, it checks if number of this item > 0. If it so, it also creates Text object.
It sets value item.number to number in the array and Text.text to item.number.
Open full size image

### Share and Copy this Tutorial

2
SeductiveDungeon 8,181 rep

Hero! I'm gonna make one right now. :D

Sunday, July 07, 2013 at 11:01:37 AM
1
Waltan 8,055 rep
Hero! I'm gonna make one right now. :D

I hope you do not get confused in my bad English %)

Sunday, July 07, 2013 at 11:04:20 AM
1
Lordshiva1948 44.2k rep

Nice one I will start making mine soon

Sunday, July 07, 2013 at 12:06:58 PM
1
Waltan 8,055 rep

I just found a little bug in my algorithm with tx variable. (3rd page)
Check it if you got problems with it.

Sunday, July 07, 2013 at 1:39:38 PM
0
sosensible 10.3k rep

dont need it yet but this will defenetily come in handy

Sunday, July 07, 2013 at 3:22:23 PM
1
SergioRM 6,968 rep

This is really good.
Thank you!

Sunday, July 07, 2013 at 10:25:33 PM
1
AbelaNET 14.9k rep

Thanks for sharing

Monday, July 08, 2013 at 10:27:11 PM
1
Zebra 1,457 rep

Thanks you so much for this!! (and sorry for my englisch)
I followed your tutorial, but I don't know how to add another item in the inventory.
When I try to do your example of "inventory efficiency", new items replace the ones that are already in the inventory, I can't place them in the empty slots.

Friday, July 12, 2013 at 11:26:56 AM
2
Waltan 8,055 rep
Thanks you so much for this!! (and sorry for my englisch)
I followed your tutorial, but I don't know how to add another item in the inventory.
When I try to do your example of "inventory efficiency", new items replace the ones that are already in the inventory, I can't place them in the empty slots.

Here you go. goo.gl/A16f8 Download this .capx of "inventory efficiency". I made comments for you. All you need is in "Control system" group.

Friday, July 12, 2013 at 12:32:35 PM
1
Joannesalfa 28.4k rep

This is really efficient, actually I'm wondering how to substract an item when it's used?

I would like to know if you improve this when an item is used, it should be destroyed when value is 0 or -1

Saturday, July 13, 2013 at 8:36:34 AM
2
Waltan 8,055 rep
This is really efficient, actually I'm wondering how to substract an item when it's used?

I would like to know if you improve this when an item is used, it should be destroyed when value is 0 or -1

As I said in tutorial (end of page 3) it checks if number of this item > 0.
So, for example, if you will subtract 1 every time when you right click on slot, when value in array at this slot will = 0, you also should to erase data about item ID.

Check this .capx: goo.gl/A16f8 ("Tutorial" group)

I also updated "inventory efficiency" example. You can now download .capx through it.

Saturday, July 13, 2013 at 12:28:14 PM
1
Altheon 1,041 rep

Hi Vladoss! your tutorial was great and this is the best inventory implementention we've seen here, but i would like to know how to expand with 5 equipment slots, its the missing piece that i cant wrap my head around on how to do on your inventory.
Can you help me?

Monday, October 14, 2013 at 12:44:22 AM
1
ffman22 6,645 rep

Thursday, April 03, 2014 at 8:22:27 PM
1
Waltan 8,055 rep
Hi Vladoss! your tutorial was great and this is the best inventory implementention we've seen here, but i would like to know how to expand with 5 equipment slots, its the missing piece that i cant wrap my head around on how to do on your inventory.
Can you help me?

Can you tell me what you want in details?
Sorry for the long aswer.

Friday, April 18, 2014 at 5:49:28 PM
1
RookieDev 6,493 rep

Vladoss, If i wanted to use this method to make my Hotbar it would work fine.
What i dont understand is could i be able to move slots wherever i want?

I have a backpack that the player will keep all of his ingame items and it will not always be visible to the player and i would love to know if i can create slots where they are needed and if so how would i do that?
Or am i limited to how the array is drawn?

Tuesday, May 27, 2014 at 10:11:59 AM