Skip to content

YaBackpackComponent

YaBackpackComponent manages the backpack and items to be put in or moved out of the backpack.

Functions

Array<YaBackpackStacks> GetAll()

Returnsitems Info
Gets the information of all the items in the backpack.

number GetCapacity()

Returnsthe capacity
Gets the capacity of the backpack.

number GetCount(number itemID)

Returnsthe count
Returns the total number of the items in the backpack by itemId.
  • itemID the item id

number Addable(number itemID, number count)

Returnscould be added to the backpack's count
Whether the item could be add to the backpack.
  • itemID itemId
  • count add count

void Sort()

Sort the backpack.

void Clear()

Clear the backpack.

boolean Replace(number fromIndex, number toIndex)

SERVER ONLY 
Returnstrue if success
Exchange the position of the two item in backpack.
  • fromIndex from
  • toIndex to

void Add(number itemID, number count)

SERVER ONLY 
Add item to backpack.
  • itemID itemId
  • count count

void Add(number itemID, number entityId, number count)

SERVER ONLY DEPRECATED 
Obsoleted,use Add(int itemID, int count) instead.
  • itemID itemId
  • entityId item's entityId
  • count item count

void Remove(number index, number count)

SERVER ONLY 
Remove an item from backpack.
  • index the index in backpack
  • count count

void RemoveByItemId(number itemID, number count, number fromIndex)

SERVER ONLY 
Remove item from backpack by itemId.
  • itemID itemId
  • count count
  • fromIndex start position in backpack

void Enable()

SERVER ONLY 
Enable backpack.

void Disable()

SERVER ONLY 
Disable backpack.

Events

ChangedEvent<void>

Fired when the backpack's data changed.

ItemAddedEvent<BackpackItemChange>

Fired when an item is added.
  1. ChangeInfo The added item info.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function OnPlayerJoined(playerId)
    local player = YaGame:GetPlayer(playerId)
    local backpack = YaScene:GetComponent(script:GetComponentType("YaBackpackComponent"), player)

    local function BackPackAddItem(info)
        print(info.ItemId)
        print(info.ChangeCount)
        print(info.ItemEntity.EntityId)
    end
    EventHelper.AddListener(backpack, "ItemAddedEvent", BackPackAddItem)
end
EventHelper.AddListener(YaGame, "PlayerJoinedEvent", OnPlayerJoined)

ItemRemovedEvent<BackpackItemChange>

Fired when an item is removed.
  1. ChangeInfo The removed item info.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function OnPlayerJoined(playerId)
    local player = YaGame:GetPlayer(playerId)
    local backpack = YaScene:GetComponent(script:GetComponentType("YaBackpackComponent"), player)

    local function BackPackRevItem(info)
        print(info.ItemId)
        print(info.ChangeCount)
        print(info.ItemEntity.EntityId)
    end
    EventHelper.AddListener(backpack, "ItemRemovedEvent", BackPackRevItem)
end
EventHelper.AddListener(YaGame, "PlayerJoinedEvent", OnPlayerJoined)

Code sample

Code sample

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
-- Prepare the component.
local backpack = YaScene:GetComponent(script:GetComponentType("YaBackpackComponent"), playerEntity)
-- Get all the items in the backpack.
local items = backpack:GetAll()
-- Check if the item is addable with itemId and count. If 0 is returned, you cannot add the item anymore.
local count = backpack:Addable(itemId, countToAdd)
-- Put 10 items to the backpack.
backpack:Add(itemId, 10)
-- Remove an item from the backpack of ```countToRemove```. ```fromIndex``` is the index position of the item in the backpack.
backpack:Remove(fromIndex, countToRemove)
-- Remove all items from the backpack.
backpack:Clear()