Player inventory
Use the SDK to manage player inventory items including adding, updating, and deleting items.
Read time 4 minutesLast updated 7 months ago
The methods in the namespace allow you to retrieve and update the player's inventory instances.
PlayerInventoryThese methods will return inventory data for the currently signed in player from the Authentication SDK.
GetInventoryAsync
Retrieves the current inventory item instances associated with the currently signed in player.
This method optionally takes a object. This can be used to set the number of items per fetch and for filtering.
GetInventoryOptionsYou can filter the inventory items using a list of configuration item IDs and/or IDs. If you use these filter options then only players items that have the specified configuration item IDs and IDs will be returned.
PlayersInventoryItemPlayersInventoryItemThe following sample code retrieves the first five items for the current user, and then retrieves the next five.
// Optional, defaults to 20GetInventoryOptions options = new GetInventoryOptions{ ItemsPerFetch = 5};GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);List<PlayersInventoryItem> firstFiveItems = inventoryResult.PlayersInventoryItems;// do something with your itemsif (inventoryResult.HasNext) { inventoryResult = await inventoryResult.GetNextAsync(5); List<PlayersInventoryItem> nextFiveItems = inventoryResult.PlayersInventoryItems; // do something with your items}
This next sample code retrieves only the player's items.
SWORDGetInventoryOptions options = new GetInventoryOptions{ InventoryItemIds = new List<string>() { "SWORD" }};GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);List<PlayersInventoryItem> listOfItems = inventoryResult.PlayersInventoryItems;// ... etc
These methods return a .
GetInventoryResultGetInventoryOptions
The options object for a call. It has the following fields:
GetInventoryAsync- : A list of strings. Defaults to
PlayersInventoryItemIds. ThenullIDs of the items in the player's inventory that you want to retrieve.PlayersInventoryItem - : A list of strings. Defaults to
InventoryItemIds. The configuration IDs of the items you want to retrieve.null - : An int. Defaults to 20. Use this to set the maximum number of items to fetch per call between 1 and 100 inclusive.
ItemsPerFetch
GetInventoryResult
A provides paginated access to the list of player's inventory items retrieved. It has the following field:
GetInventoryResult- : A
PlayersInventoryItemswith the currently fetched items.List<PlayersInventoryItem>
It has the following methods:
- : This method asynchronously fetches more results. It has one optional parameter to limit the amount of results fetched (this can be between 1 and 100 inclusive, default is 20). It will return a new result, which contains both the original items and the newly fetched items in its
GetNextAsync(int itemsToFetch = 20)list. The result will bePlayersInventoryItemsif there are no more results to fetch.null
AddInventoryItemAsync
Adds an item to the player's inventory.
This method optionally takes an object. This is used to set a custom — if , one is autogenerated. Can also be used to set a dictionary of instance data.
AddInventoryItemOptionsPlayersInventoryItemIdnullReturns a representing the inventory item added to the player's inventory.
PlayersInventoryItemDictionary<string, object> instanceData = new Dictionary<string, object>{ { "rarity", "purple" }};AddInventoryItemOptions options = new AddInventoryItemOptions{ PlayersInventoryItemId = "customID", InstanceData = instanceData };PlayersInventoryItem createdInventoryItem = await EconomyService.Instance.PlayerInventory.AddInventoryItemAsync("SWORD", options);
AddInventoryItemOptions
The options object for a call. It has the following fields:
AddInventoryItemAsync- : A string. Defaults to
PlayersInventoryItemId. Sets the ID of the creatednull. If not supplied, one is generated.PlayersInventoryItem - : A
InstanceData. Used to set a dictionary of instance data.Dictionary<string, object>
DeletePlayersInventoryItemAsync
Deletes an item from a player's inventory.
This method optionally takes a object used to set the write lock. If a write lock is provided, it will only delete the item if the write lock is accepted by the Economy service. If no write lock is provided then the operation is forced through.
DeletePlayersInventoryItemOptionsDeletePlayersInventoryItemOptions options = new DeletePlayersInventoryItemOptions{ WriteLock = "writeLock"};EconomyService.Instance.PlayerInventory.DeletePlayersInventoryItemAsync("playersInventoryItemID", options);
DeletePlayersInventoryItemOptions
The options object for a call. It has the following field:
DeletePlayersInventoryItemAsync- : A string. Defaults to
WriteLock. Use this to set a write lock for optimistic concurrency. See Write Lock.null
UpdatePlayersInventoryItemAsync
Updates an item with new instance data.
Returns the updated players inventory item.
This method optionally takes an object used to set the write lock. If a write lock is provided, it will only update the item if the write lock is accepted by the Economy service. If no write lock is provided then the operation is forced through.
UpdatePlayersInventoryItemOptionsDictionary<string, object> instanceData = new Dictionary<string, object>{ { "rarity", "purple" }};UpdatePlayersInventoryItemOptions options = new UpdatePlayersInventoryItemOptions{ WriteLock = writeLock };PlayersInventoryItem playersInventoryItem = await EconomyService.Instance.PlayerInventory.UpdatePlayersInventoryItemAsync("playersInventoryItemID", instanceData, options);
UpdatePlayersInventoryItemOptions
The options object for a call. It has the following field:
UpdatePlayersInventoryItemAsync- : A string. Defaults to
WriteLock. Use this to set a write lock for optimistic concurrency. See Write Lock.null
PlayersInventoryItem
A represents a single unique item in a player's inventory. It contains the following fields:
PlayersInventoryItem- : The ID of this unique inventory item in the players inventory.
PlayersInventoryItemId - : The resource ID of the inventory item configuration associated with this instance.
inventoryItemId - : Any instance data associated with this players inventory item.
InstanceData - : Any instance data associated with this players’ inventory item, as an
InstanceDataDeserializable. See Using InstanceDataDeserializable.IDeserializable - : The current
WriteLockstring for this player's inventory item.writelock - : The date this player's inventory item was created. It is an EconomyDate object.
Created - : The date this player's inventory item was modified. It is an EconomyDate object.
Modified
It also has the following helper method:
GetItemDefinitionAsync
This method fetches the configuration of this player's inventory item, type of .
InventoryItemDefinitionPlayersInventoryItem playersInventoryItem = // ... fetch the players inventory itemInventoryItemDefinition itemDefinition = playersInventoryItem.GetItemDefinitionAsync();
PlayersInventoryItemUpdated
This event can be subscribed to in order to be notified when the SDK updates a specific item in the player's inventory. The subscriber is passed the ID of the item that was updated.
playersInventoryItemEconomyService.Instance.PlayerInventory.PlayersInventoryItemUpdated += playersInventoryItemID => { Debug.Log($"The players inventory item that was updated was {playersInventoryItemID}");};
Using InstanceDataDeserializable
Use to add custom data for specific items in a player's inventory. It is passed in as type and fetched as type . This allows you to pass in your own custom classes as instance data.
InstanceDataDeserializableobjectIDeserializableFor example, a player's shield may have a durability rating. This could be set and updated using :
InstanceDataDeserializableclass MyInstanceData { public int Durability; public string Rarity; }MyInstanceData myInstanceData = new MyInstanceData() { Durability = 100, Rarity = "purple"};PlayersInventoryItem updatedItem = await EconomyService.Instance.PlayerInventory.UpdatePlayersInventoryItemAsync("playersInventoryItemId", myInstanceData);
You can deserialize the instance data by doing the following:
MyInstanceData fetchedInstanceData = updatedItem.InstanceData.GetAs<MyInstanceData>();int durability = fetchedInstanceData.Durability;string rarity = fetchedInstanceData.Rarity;