Intro
This will be the first of two parts to create a full-fledged inventory system with gooey. As said before, there are multiple ways of doing this, even with gooey itself, but we'll stick to one to teach about different things with the library.
In this first part, we will create a basic graphical inventory system to display the current items the player has. We will be able to use the "I" key to open/close the inventory.
The current state
Currently in the game, if you take a look at the obj_Player's code, the inventory data structure is already created, and you can actually pick up stuff already, as you might have seen. However, there's no way to display the player inventory graphically.
Let's take a look at how it's built (click the code block to expand). The inventory instance variable is an array of structs, where each struct is an item and is composed of the keys item_name (the name) and qty (the number of items of that object). The array currently grows and shrinks1 as items are added and removed, and the maximum inventory size is controlled by the inventory_max_size instance variable. There are methods to add and remove items from the inventory as needed:
Code
The panel and grid
Let's setup a UIPanel and UIGrid as before. We will have a square player inventory, with 2 rows and 2 columns. We will write some preliminary code similar to what we've done before to calculate the width and the height of the panel. In this case, we do want the inventory window to be moveable, but not resizable, so we'll set that up.
Also, in this case I want to use the darker brown panel sprite (dt_box_9slice_c) for the inventory and the lighter brown sprite (lt_box_9slice_c) for the slots. Remember from the Grids tutorial that grids are essentially invisible. However, they are also a collection of UIGroup widgets functioning as cells. Since a UIGroup can have a sprite as a background, we will use this to set the light sprite for every cell of the grid using a double for loop:
Up to now, we have created a basic inventory panel. We set its visibility to false by default, and we can add a simple check on the Step event to show/hide it (the Input verb is already configured):
| Game / Step | |
|---|---|
With this, we have successfully created a 2x2 inventory panel like so:
You can play around with the _num_rows and _num_cols to see how the inventory grows/shrinks, but since we've set a max size of 4 for the obj_Player object, we will stick to that size for the rest of the tutorial.
Displaying the currently held items
For each slot of the inventory, we can show a sprite of the currently held item. Also, we can show a little text indicating the quantity of that item in the inventory. For this, we will add UISprite and UIText widgets to each slot of the inventory, and set them initially to undefined and empty strings. Let's add these to our double for loop:
Next, since we want our gooey inventory to be updated in sync with the player's inventory array, let's use .setPreRenderCallback to define a function. This runs every frame and thus allows us to keep the sprites and texts in sync. We will check every index up until the inventory max size: if there's an item in the player's inventory, we will set the sprite and the text to the contents of the struct in that position2; if not, we will set them to undefined, 0x0 dimensions and empty string respectively. It is important to do this every frame, especially when items are removed from the inventory (such as when we hand the quest items to the NPC to complete the quest).
In order to define the row and column, we want to use modulo arithmetic. If we think about this, we can map each index in the inventory array (0 to 3) to a row and column index. If we start filling from top to bottom and from left to right, we can calculate the row as the integer division of the index by the number of columns, and we can calculate each column as the remainder of the division by the number of columns.
Let's take a look:
The final result
Now that we've done this, we already have a fully working basic inventory! We can check out what we have and it gets updated as new items are added or removed. Check out the gifs below:
In the next tutorial we will add some more advanced functionality to the inventory, such as the ability to reorder and drop items in the inventory, and adding a tooltip to the items to get some information about them.
-
We will change this behavior in the advanced inventory tutorial, in order to allow for inventory reorganization via drag and drop. ↩
-
On line 150, we need to perform a small "hack", as sprite names for non-crops are equal to the item names, but sprite names for crops have the
_05suffix (when fully grown). ↩