Stencyl – Creating a Dynamic Inventory

RPG’s, Point & Click games, and many, many, others use inventory systems. Such systems range from relatively basic ones, to fully fledged dynamic systems. While working on game that was non-linear, I needed a dynamic inventory system.

What is a dynamic inventory system?

In this instance I’m talking about an inventory that displays items in the order in which they were collected, and removes them as they’re used, bumping unused items down the list to fill vacant spaces. Some simpler inventory systems may just have specific slots for particular items, and may not rearrange items to fill unused spaces.

The Code

This code is an actor behaviour that’s attached to all items that the player can pick up. The first section checks if the item is on the ‘kill list’, this is important as when a player picks up an item, the item is then added to the kill list so that if the player visits the same scene again, the item isn’t respawned. (Technically it is respawned anyway, but if it’s on the kill list it is instantly killed so the player never notices.)


The code above is attached to the item which is picked up. The fist line isn’t part of the inventory system itself. It just displays a description when the player moves their mouse over the item.

I’ve limited by inventory system to 18 slots. This was done to keep things simple as my inventory icons are 32 pixels high, and the screen hight of my game is 576 pixels (576 / 32 = 18). The first ‘IF’ statement checks if inventory is full, i.e. is the inventory count 18? If the inventory is full, a message is displayed informing the player. In my example I have a list with several different messages that are picked at random to stop the message from getting stale for the player. However, chances are that in my game, the player will never actually fill the inventory completely.

In the ‘OTHERWISE IF’ statement, it checks if the inventory count is less than 18. First of all, the examine text attribute is cleared. If this wasn’t done, the text in the first piece of code would remain on screen until another event changed the attribute.

Next, we add the picked up item to a ‘kill list’ by generating a unique custom ID, as mentioned above for the first code snippet. This is important so that the same item cannot be picked up more than once.

The next line adds the corresponding inventory icon to the inventory list. The text ‘Shovel 1 Icon’ is the actual name of the actor, not just a nickname. I’ve done this so that I can easily refer to it and create it later. This isn’t the only way of doing this, but I went for simplicity.

Once we’ve finished running the code we need to, we simply kill the picked up item so show that it’s been picked up and to prevent the player from collecting it again, before we finally trigger a scene behaviour that updates our inventory.


This code is attached to all scenes via a scene behaviour. The inventory list doesn’t just add new items to the list. It refreshes. It’s does this by destroying the current inventory icons and creating new ones based upon the updated list.

The next bit of code checks how many item are currently in the list so that number can be referred to. It then runs the ‘create’ once for each item in the list.

The ‘create’ code pulls the relevant item from the list, and actually uses this as the actor name. The Y value for each icon is based upon its location in the inventory list. This only works properly if each of your icons are the same height (in this instance it’s 32 pixels.)

In this example the icons are displayed vertically, starting at the top and working down. If you wanted to display the icons horizontally you’d put the ‘current loop count x 32’ code as the X value, where 32 is the width of your icons.

Adding rows or columns to an inventory?

In this example you’re limited to the number of icons you can display according to the number/size of your icons and the dimensions of your screen. If you wanted to display more by using columns or rows, things become more complicated. For instance, if I wanted to allow more than 18 items in the inventory using columns, I would need to calculate the X value when creating the icons. See below:


Again, you may still need to limit the number of items. In the above example 36 is the max, otherwise icons after entry 36 in the list would then all be created at the X value 36 (the last value that attribute ‘X Value’ is set to.

Using rows and columns in this manner will obviously encroach into your screen space. This may be a problem if your icons are large, or you’re using lots of them.

Leave a Reply

Your email address will not be published. Required fields are marked *