Easy Inventory Using An Array

8

Index

Attached Files

The following files have been attached to this tutorial:

.capx

Stats

17,242 visits, 38,717 views

Tools

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 12 Jun, 2013. Last updated 19 Feb, 2019

Programming The Inventory

Get the slots and texts ready

The first thing we will do is create a function to update the invSlot and slotAmt objects. This is a simple for loop that sets each invSlot to the item ID of its respective slot, and sets the slotAmt to how many items are in that slot.

Picking Up Items

Parameters

Param0 is the ID of the item to add to the inventory.

Param1 (optional) is how many items should be added. If this is not passed, default is 1.

Variables

foundSlot - Used to keep track of whether or not another slot containing the same item was found.

emptySlot - Stores the slotID of the first empty slot that is found.

amountToAdd - How many items are being added to the inventory.

Events

Event 8 - Reset the variables.

Events 9-10 - Check if only 1 parameter was passed. If so, default amountToAdd to 1. Else, Set it to Param1.

Event 11 - Loop through each X element in the Inventory array. Don't bother proceeding if a slot was found already.

(Things to note: Inside of this loop, current slot will equal arr_inventory.CurX and current item ID will be arr_inventory.CurValue)

Event 12 - If the ID of item in the current slot is 0, this means it is empty. If an emptySlot has not been found yet (we want the first empty slot, not the last one) store the current slot in emptySlot.

Event 13 - If the ID of the item in the current slot matches the item ID we are adding (Param0) we set foundSlot to the current slot, add amountToAdd to the current amount of items in the slot, and finally call the UpdateInventory function.

Event 14 - First identify if the current slot is the last slot in the inventory array. If so, make sure a matching slot was not found and an empty slot does exist. Then the first empty slot (emptySlot) can be set to the ID of the item to add, and the amount in that slot can be set to amountToAdd. Then call the UpdateInventory function.

Dropping Items

Parameters

Param0 is the slotID we will be removing the item from.

Param1 is how many items should be removed.

Variables

itemsRemoved - How many items were removed from the slot (in case *Param1" was larger than how many items are actually in there)

Events

Event 18 - First set itemsRemoved to min(Current Slot Amount, Param1) This will get the lower of the two numbers to prevent removing more items than that slot actually has. Then remove that amount from the slot passed in Param0.

Event 19 - If that slot is now empty, set the slots ID to 0 to represent an empty slot. Setting the amount in that slot is not entirely necessary, I just have it there as a fail-safe.

Event 20 - Finally, call the UpdateInventory function and return itemsRemoved. Returning the variable is not necessary unless you need it somewhere else (eg. dropping those items on the ground)

Other Stuff

To add/remove items from the inventory we are simply calling these two functions when the groundItem or invSlot are clicked.

And that's all there is to it! I will be working on this tutorial in the future and adding/improving stuff.

.CAPX
  • 7 Comments

  • Order by
Want to leave a comment? Login or Register an account!