Hi @Corva-Nocta ,
I’ve moved your post over to the Cloud Code forum.
You are on the right track.
One thing to point out though, CustomData
refers to parameters that you define and attach to an Inventory Item definition. When a player picks up an inventory item they are holding an Instance of it that does not contain any custom data but does contain an empty InstanceData
object. Take a look at the Custom Data & Instance Data documentation.
You would therefore probably want to copy the Custom Data
or certain parameters from it, from the item definition to the Instance Data
as the player receives the item, so you can access and potentially modify the instance’s configuration over time. e.g. think about decreasing durability as the item is used.
With that in mind, here are some code snippets that might help.
A Cloud Code script to retrieve player inventory
/*
* -------- Cloud Code Script ----------------
* Get Player Inventory - iterate over pages
* --------------------------------------------
*/
const { InventoryApi } = require("@unity-services/economy-2.4");
module.exports = async ({ params, context, logger }) => {
const {
projectId,
playerId,
accessToken
} = context;
const inventoryApi = new InventoryApi(context);
// Default to pages of 20 inventory items if none specified as input params.pageSize
const pageSize = params.pageSize != undefined ? params.pageSize : 20 ;
let pages = 0 ;
let inv = [];
// get a page worth of inventory items
let playerInventory = await inventoryApi.getPlayerInventory({projectId, playerId, limit:pageSize});
// When we have results
while( playerInventory.data.results.length > 0) {
// Add them to the local inv array
playerInventory.data.results.forEach( i => inv.push(i));
// Grab the playersInventoryItemId of the last one
lastPlayersInventoryItemId = playerInventory.data.results[playerInventory.data.results.length -1].playersInventoryItemId;
// Then go looking for another page worth
playerInventory = await inventoryApi.getPlayerInventory({projectId, playerId, limit: pageSize, after: lastPlayersInventoryItemId});
pages ++;
}
// Return the JSON result to the client
return {
inventory: inv,
pages: pages
};
};
Is called from this C# client side script
public async void GetInventoryFromCloudCodeButtonClicked()
{
Dictionary<string, object> requestParams = new Dictionary<string, object>();
requestParams.Add("pageSize", 15);
ResultType response = await CloudCodeService.Instance.CallEndpointAsync<ResultType>("getInventory", requestParams);
InventoryResponse[] playerInventoryArray = response.inventory;
Debug.Log($"Returned {playerInventoryArray.Count()} items of inventory");
Debug.Log($"pages {response.pages}");
foreach(InventoryResponse playerInventoryItem in playerInventoryArray) {
Debug.Log($"itemId : {playerInventoryItem.inventoryItemId} ");
Debug.Log($"playersInventoryItemId : {playerInventoryItem.playersInventoryItemId }");
string instanceData = playerInventoryItem.instanceData != null ? playerInventoryItem.instanceData.ToString() : "NULL";
Debug.Log($"InstanceData : {instanceData} ");
}
}
Assuming these two classes have been defined to work with the repsonse from the Cloud Code script.
private class ResultType
{
public InventoryResponse[] inventory;
public int pages;
}
private class InventoryResponse
{
public JObject created;
public JObject instanceData;
public string inventoryItemId;
public JObject modified;
public string playersInventoryItemId;
public string writeLock;
}
I hope that helps