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,767 visits, 37,384 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

As the player is moving around the stage, they can use the Z Key to pick up items from the green boxes. This starts off a chain of events which differs slightly depending if the player already has the item in their inventory. The common thing between the two cases, is that as soon as the player picks up an item, the FoundItem text object will be set to visible, and show the player what item and how many they have picked up. The text object will then fade out.

If the player already has the item in their inventory, the value for the corresponding key in the dictionary is updated, and then the functions to update and save the inventories are called.

If the player does not have the item, then a new value needs to be added to the correct array. First, we check which array is needed by comparing the instance variable ItemCat on the ItemBox object with the Inventories family and pick the array that matches. Then, if the array already has items in, we simply push a new value (ItemBox.ItemID) to the back of the Y axis. If the array is empty, we set the value at (0,0) to ItemID stored in the ItemBox the player interacted with. Once the item has been added to the inventory, the save function is called.

When the inventory is open, the player can also use the Z Key to drop an item. This will simply remove the item from the inventory. If the player has more than one of a given item, they will drop one at a time until none are left.

If the Z key is pressed when the InvOpen Boolean is true, the game will check which inventory the player is currently looking at to ensure that the correct array is picked to remove the item from.

When the player only has a single copy of an item, its dictionary entry is set to 0 and the array size adjusted for the dropped item:

Sub-event Condition

Inventories ▶︎ CategoryID = CurrentInvID

Sub-event Condition

Dictionary ▶︎ Key ItemData.Get("items." & CurrentItemID & ".name") = 1

Sub-event Action

Dictionary ▶︎ Set key ItemData.Get("items." & CurrentItemID & ".name") to value 0

Sub-event Condition

Inventories ▶︎ Y size = 1

Sub-event Action

Inventories ▶︎ Set value at (0,0) to 0

Functions ▶︎ Call UpdateArray (ArrayName: CurrentInvCat)

Functions ▶︎ Call SaveInventories

Sub-event Condition

Inventories ▶︎ Y size > 1

Sub-event Condition

System ▶︎ CurrentInvPage = 0

Sub-event Action

Inventories ▶︎ Delete index CurrentItemSlot from Y Axis

Functions ▶︎ Call UpdateArray (ArrayName: CurrentInvCat)

Functions ▶︎ Call SaveInventories

Sub-event Condition

System ▶︎ CurrentInvPage > 0

Sub-event Action

Inventories ▶︎ Delete index CurrentItemSlot + (20*CurrentInvPage) from Y Axis

Functions ▶︎ Call UpdateArray (ArrayName: CurrentInvCat)

Functions ▶︎ Call SaveInventories

If the player has multiple copies of an item then when the Z key is pressed, the following event is called:

Sub-event Condition

Dictionary ▶︎ Key ItemData.Get("items." & CurrentItemID & ".name") > 1

Sub-event Action

Dictionary ▶︎ Set key ItemData.Get("items." & CurrentItemID & ".name") to value Dictionary.Get(ItemData.Get("items." & CurrentItemID & ".name")) -1

Functions ▶︎ Call SaveInventories

  • 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)