Inventory & Items

Learn how to add items to your game mode.

Adding Items

Keywords and Inventory Syntax

Keywords and Inventory Syntax

The inventory is setup as a single array. The slot determines how the item is displayed in the inventory interface and the index is where the item is in the array.

So accessing individual items for a player is something like...

Here's what they may look like from a visual perspective.

In the above example if we used player.data.inventory[0] we sould get the burger item

Difference Between Slot and Index

Slot is where the item is placed when the item is displayed to the user.

Index is where the item is placed inside of the array.

If a function returns an index for an inventory item then it means you can get the item like...

If the function returns a slot for an inventory item. Then it means you need to get it like this...

Using Inventory Functions

The inventory does not always save after each update. If you use any inventory functions you should be throwing a save and a sync immediately after performing any functions.

Deep Cloning Objects

Because these objects can be rather complicated there is a simple function that does a deep clone of an object and ensuring that you're not modifying the original data. You should always deep clone an object before adding it to a player inventory to prevent data from referencing other inventories and such.

Add / Create Items

Items come in a variety of flavors but their general creation follows a specific template.

Now when you create templates for items and want to use a reference item like the one above. You must perform a deep copy of that item reference.

After making the deepl clone of your object you can modify it.

Giving Players Items

More often than not the one location you will be putting new items in the player's inventory. Athena comes with a handful of utility functions to help you add and remove items from a player's inventory.

Here's the basic gist of it.

Removing Players Items

If you need to remove a player's item you can run the following functions to check if it is removed.

Working with Player Inventories

Depending on what you want to do with the player inventory there are a ton of ways to work with it. However, more often than not the most common use case for working with an inventory is finding an item and removing it. Which is what we'll be covering here.

Item Effects

If you wish to use item effects when you consume an item it's quite simple.

You will need to add a event to the data section of your item.

When the item is consumed it will reduce the quantity by 1 and then call that event over alt.emit.

It can then be recieved with alt.on.

Item Example

Item Factory

The item registry is a relatively new concept but it allows you to create item templates that can be fetched from the Database so you're not storing them in your code as much. Your mileage may vary on this system as I personally do not use it very much.

Below is an example of how to create, fetch, and quantify an item before giving it to a player.

Last updated