Managing inventory system¶
Inventory systems allow players to store and manage their in-game items. In this tutorial, you'll learn how to use APIs to manage inventory in your game, specifically how to:
- Add items to and remove items from a player's inventory.
- Receive updates on inventory changes.
Before you begin¶
Before you dive into the tutorial, make sure you have the following prerequisites:
-
Visual Studio Code is installed and integrated with your Studio. If you haven't done this already, follow the instructions in How to start coding to download and integrate.
-
The Inventory feature is enabled in Studio. This feature is enabled by default and you can check all default settings for it by visiting Inventory.
-
A new Lua script is added to an entity in your scene. If you're not sure how to do this, check out Creating or adding scripts.
Managing inventory with Lua¶
Suppose you have an item with Item ID 1, and you need to give 20 copies of this item to the first player entering the game and remove 8 copies after 5 seconds. In addition, you want to receive detailed information about these inventory changes. To achieve this, we provide a code breakdown that introduces key steps for managing inventory through coding, as well as a full code sample for direct use.
Note that to create an item, in the Explorer window click Gameplay>Item. The Item ID is the unique identifier of an item and can not be changed. For more information, see Items.
1. Get the player entity¶
First we need to get the player entity to manage the player's inventory. Here's a sample to get the player entity using the player ID:
local playerEntity = nil
EventHelper.AddListener(YaGame, "PlayerJoinedEvent", function (playerId)
playerEntity = YaGame.Instance:GetPlayerEntity(playerId)
end)
2. Get YaBackpackComponent¶
Get YaBackpackComponent before using its functions for managing inventory.
local backpack = YaScene:GetComponent(script:GetComponentType("YaBackpackComponent"), playerEntity)
3. Add & remove inventory items¶
Use the
local function AddRemoveSample()
-- add item
backpack:Add(1, 20)
-- remove item
backpack:Remove(0, 8)
end
4. Receive updates on inventory changes¶
Add more event listeners for inventory changes. When inventory data changes due to changing items in the inventory, you can get item details, including item ID and the number of increased or decreased item copies.
local function BackPackChanged()
print("backpack changed")
end
local function BackPackAddItem(AddedData)
print(AddedData.ItemId)
print(AddedData.ChangeCount)
end
local function BackPackRevItem(RemovedData)
print(RemovedData.ItemId)
print(RemovedData.ChangeCount)
end
local function ListenSample()
-- backpack changed event --
EventHelper.AddListener(backpack, "ChangedEvent", BackPackChanged)
-- backpack item added event --
EventHelper.AddListener(backpack, "ItemAddedEvent", BackPackAddItem)
-- backpack item removed event --
EventHelper.AddListener(backpack, "ItemRemovedEvent", BackPackRevItem)
end
5. Save the script and playtest it in Play Mode.¶
Full code sample You can use the following script directly in your editor. Save the script and playtest it in Play Mode.
local playerEntity=nil
local function BackPackChanged()
print("backpack changed")
end
local function BackPackAddItem(AddedData)
print(AddedData.ItemId)
print(AddedData.ChangeCount)
end
local function BackPackRevItem(RemovedData)
print(RemovedData.ItemId)
print(RemovedData.ChangeCount)
end
EventHelper.AddListener(YaGame, "PlayerJoinedEvent", function (playerId)
if playerEntity ~= nil then
return
end
playerEntity = YaGame.Instance:GetPlayerEntity(playerId)
local backpack = YaScene:GetComponent(script:GetComponentType("YaBackpackComponent"), playerEntity)
-- add items
backpack:Add(1, 20)
EventHelper.AddListener(YaTime:WaitFor(5), "TimeEvent", function()
-- remove items starting from the first slot in the inventory
backpack:Remove(0, 8)
end)
-- backpack changed event --
EventHelper.AddListener(backpack, "ChangedEvent", BackPackChanged)
-- backpack items added event --
EventHelper.AddListener(backpack, "ItemAddedEvent", BackPackAddItem)
-- backpack items removed event --
EventHelper.AddListener(backpack, "ItemRemovedEvent", BackPackRevItem)
end)
Note that Adventure games typically save inventory data from the previous round. However, in other game types, this inventory data may not be saved.
You have learned how to add and remove items from an inventory and receive updates on inventory changes. Armed with this knowledge, you can start building a more advanced and robust inventory system that meets your specific requirements. For additional inventory APIs and resources, please visit YaBackpackComponent.