Building an RPG-Style Inventory

23

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

inventory-system.c3p

Download now 344.98 KB

Stats

13,766 visits, 37,383 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 16 Jul, 2020. Last updated 30 Jul, 2020

This inventory system allows for several ways of navigating around the items it holds and includes different categories to better sort your objects. To enable the project to scroll through these correctly some 'settings' need to be defined for the inventory.

For each inventory category, we need to tell the system whether or not it can scroll left or right to the next category, set the category title to the correct text and update the ItemSlot objects to make sure they're drawing their information from the correct array.

There are also some settings needed for the page scrolling Booleans, which are controlled depending on how many pages the currently selected inventory has. For example, if a category only has one page, then scrolling through pages needs to be disabled. If a category has multiple pages but you're looking at the last one, then scrolling to the right needs to be disabled, but leftwards scrolling enabled:

Navigating around the inventory itself is only enabled when the InvOpen Boolean is true – in the same way that player movement is only enabled when InvOpen is false. Remember, simply pressing the spacebar will toggle whether InvOpen is true or false.

When we set InvOpen to true, the Inventory layer is set to visible, the UpdateInv function is called to ensure the correct data is being displayed and the player's Tile Movement behavior is disabled.

The left and right keys allow slot by slot movement and will scroll to the next/previous category if you're at the edge of an inventory page. A modulo operator is used to determine if the player is currently selecting one of the edge slots. Taking the Right key as an example:

Condition

Keyboard ▶︎ On Right Key pressed

Sub-event Condition

System ▶︎ CurrentItemSlot % 5 = 4

Sub-event Action

Functions ▶︎ Call CategoryCheck (CheckWhich: "Page", Direction: "Right")

Functions ▶︎ Call UpdateInv

Sub-event Conditions

System ▶︎ Else

Sub-event Action

Functions ▶︎ Call MoveCursor (Direction: "Right")

Functions ▶︎ Call UpdateInv

The up and down keys allow you to scroll between rows in the inventory, unless you're already looking at an item in the top or bottom row. A simple variable comparison enforces the movement rules here:

Condition

Keyboard ▶︎ On Down Key pressed

System ▶︎ CurrentItemSlot ≤ 14

Action

Functions ▶︎ Call MoveCursor (Direction: "Down")

Functions ▶︎ Call UpdateInv

Sometimes, it's useful to be able to skip directly to the next/previous category without having to scroll through multiple pages. The square brackets are the keys assigned to this:

Condition

Keyboard ▶︎ On ] pressed

Action

Functions ▶︎ Call CategoryCheck (CheckWhich: "Cat", Direction: "Right")

  • 9 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hi Laura! I'm struggling to understand where the images for the items are being pulled in from. I don't see it mentioned in the tutorial. Could you please explain? Thanks!

      • [-] [+]
      • 1
      • Laura_D's avatar
      • Laura_D
      • Construct Team Community Manager
      • 1 points
      • (1 child)

      So, both the ItemShowcase and the ItemSlot objects have animation frames for each item. Then, in the JSON file, each item has an ID and these ID numbers dictate the order that the frames are stored in on the Showcase and Slot sprites.

      The Slot images are set from these ID numbers in the Inventory Category Settings event group, and the Showcase images are set during the UpdateInv function.

      Does that help?

  • Thanks for the tutorial Laura! Just a question, is there any reason you specify the items on a JSON file?

    Feels to me that it would be easier to manage if they were created individually under a folder or family since we load the json to memory anyway, unless there is a performance gain I'm missing.

      • [-] [+]
      • 2
      • Laura_D's avatar
      • Laura_D
      • Construct Team Community Manager
      • 2 points
      • (0 children)

      I've not done any testing in terms of performance, but for me, it was down to preference. I didn't want to have loads of different objects even if they were nicely sat in a folder! I assume it would still work using a different object for each item, but I've not tried it!

  • we know that CategoryName is an instance variable of the array(inventory) but where did ArrayName come from?

    edit:o, i just now know it, we can add a thing called parameter in the function.

  • i hope the no. of events is not greater than 50 cause i havent bought construct yet. maybe i can cut down some events for strings.

  • eu nao comsigo fazer um inventario

  • Load more comments (1 replies)