Inventory system - part 1

The next step in my project will be to create an inventory system for the player and storage containers such as chests. The inventory system will have three components: the player character, the collectible items, and the inventory where the collectible items are stored.

Inventory Component

To create the inventory, I will use an Actor Component, a special type of Blueprint that can be attached to any Actor and can add variables, functions, interfaces, etc. However, it cannot add meshes or renderable components.

The component will have three important variables:

  • InventoryItems: a list of items that have been collected and stored in the inventory.
  • MaxSize: an integer that indicates the maximum number of items that can be stored in the inventory.
  • ActualSize: an integer that indicates the number of items currently in the inventory. It is used to check if an item can be taken.

The InventoryItems variable is of array type and stores instances of the BP_BaseCollectable object. The component will also have an "AddItem" function that checks if there is space in the inventory and adds the collectible item if possible. The function takes in a BP_BaseCollectable object and returns "true" if the item was added and "false" otherwise. This is the Blueprint logic:

Third Person Character

The inventory component will be added to the player character using the Third Person Character Blueprint. The player will have access to the "AddItem" function in all Blueprints.

Remember the post about interaction using interfaces where we have implemented a useful system for activating items.

Collectable Objects

Collectible objects will inherit from BP_BaseCollectable, which is a Blueprint Actor that implements the BPI_Interactable interface.

The base object has four variables, three of which will be used in a future post for the user interface:

  • Size: the size of the object in the inventory.
  • Name: the name that will be displayed in the user interface.
  • UIImage: the image that will be displayed in the inventory.
  • IsCollected: indicates whether the object has been collected. If it has, it will not be rendered and will be positioned away from the player.

When the player interacts with a collectible object, the following steps occur in the event graph:

  • The player's inventory component is obtained.
  • The collectible object is added to the inventory component through the AddItem function.
  • The collectible object disappears.

If the object is collected, it will be positioned above the player to prevent "ghost" collisions.

For this example, I created two collectible objects to test with a mesh from the Mech Constructor package. Each object has a name and image set in the default Details panel.

The next step will be show the inventory on the screen.