Player inventory
Use the SDK to manage player inventory items including adding, updating, and deleting items.
Read time 4 minutesLast updated a day ago
The methods in the
PlayerInventoryGetInventoryAsync
Retrieves the current inventory item instances associated with the currently signed in player. This method optionally takes aGetInventoryOptionsPlayersInventoryItemPlayersInventoryItemThis next sample code retrieves only the player's// 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}
SWORDThese methods return aGetInventoryOptions options = new GetInventoryOptions{ InventoryItemIds = new List<string>() { "SWORD" }};GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);List<PlayersInventoryItem> listOfItems = inventoryResult.PlayersInventoryItems;// ... etc
GetInventoryResultGetInventoryOptions
The options object for aGetInventoryAsync- : 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
AGetInventoryResult- : A
PlayersInventoryItemswith the currently fetched items.List<PlayersInventoryItem>
- : 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 anAddInventoryItemOptionsPlayersInventoryItemIdnullPlayersInventoryItemDictionary<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 aAddInventoryItemAsync- : 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 aDeletePlayersInventoryItemOptionsDeletePlayersInventoryItemOptions options = new DeletePlayersInventoryItemOptions{ WriteLock = "writeLock"};EconomyService.Instance.PlayerInventory.DeletePlayersInventoryItemAsync("playersInventoryItemID", options);
DeletePlayersInventoryItemOptions
The options object for aDeletePlayersInventoryItemAsync- : 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 anUpdatePlayersInventoryItemOptionsDictionary<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 aUpdatePlayersInventoryItemAsync- : A string. Defaults to
WriteLock. Use this to set a write lock for optimistic concurrency. See Write Lock.null
PlayersInventoryItem
APlayersInventoryItem- : 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
GetItemDefinitionAsync
This method fetches the configuration of this player's inventory item, type ofInventoryItemDefinitionPlayersInventoryItem 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 theplayersInventoryItemEconomyService.Instance.PlayerInventory.PlayersInventoryItemUpdated += playersInventoryItemID => { Debug.Log($"The players inventory item that was updated was {playersInventoryItemID}");};
Using InstanceDataDeserializable
UseInstanceDataDeserializableobjectIDeserializableInstanceDataDeserializableYou can deserialize the instance data by doing the following:class MyInstanceData { public int Durability; public string Rarity; }MyInstanceData myInstanceData = new MyInstanceData() { Durability = 100, Rarity = "purple"};PlayersInventoryItem updatedItem = await EconomyService.Instance.PlayerInventory.UpdatePlayersInventoryItemAsync("playersInventoryItemId", myInstanceData);
MyInstanceData fetchedInstanceData = updatedItem.InstanceData.GetAs<MyInstanceData>();int durability = fetchedInstanceData.Durability;string rarity = fetchedInstanceData.Rarity;