Grab instance data of an inventory item?

Hi all,
I'm working on some server side logic and am trying to get the player inventory, I think cloud code is going to be the best way for me to go but I am having some trouble getting the cloud code to work. I might just be looking in the wrong place, so if this does exist somewhere please let me know!

So I have this in my cloud code:

const { InventoryApi } = require("@unity-services/economy-2.3");
module.exports = async ({params, context, logger}) =>
{
const { projectId, playerId } = context;
const inventory = new InventoryApi(context);
const result = await inventory.getPlayerInventory({ projectId, playerId });
return result.data;
}

which seems like an obvious starting point. So I am guessing I need to take that "result" const and check to see if it has an item. Let's just say its the sword item from the inventory examples. So if I want to check if the player has "sword" for do I do that here in the cloud code?

Even better, if I just want the custom parameters to be sent back (like the "rarity" in the economy example) so I can just call this cloud code script with a playerID and an item name and it would just return the parameters (assuming the player has the item) How do I get instance data of an item from cloud code?

Any help at all here would be greatly appreciated

EDIT:
code is now working! For anyone that might need it, here is how to check for an item:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { itemId } = params;
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const item = await inventory.getPlayerInventory({projectId, playerId, inventoryItemIds : itemId});
  return item.data;
}

just make sure you set up the itemId parameter in the cloud code script.

and just in case if anyone needs the code to add an item (which I did later) here you go:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { itemId } = params;
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const createdItem = await inventory.addInventoryItem({ projectId, playerId, addInventoryRequest : {inventoryItemId: itemId} });
}

both of these are scripts in their most basic form (which is about all I can understand lol) so it might be healthy to throw in some checks and logs when implimenting.

I'm starting basic at the moment, just trying to add an item to the inventory, but I am getting an invocation error.

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const createdItem = await inventory.addInventoryItem(projectId, playerId, { inventoryItemId:"TESTSWORD" });

  const playersInventoryItemId = createdItem.data.playersInventoryItemId;
  logger.info("created Item : " + createdItem.data.playersInventoryItemId);
}

so this should in theory be adding the testsword item to the player. But I get this error:

Invocation Error

RequiredError: Required parameter projectId was null or undefined when calling addInventoryItem.

{
"message": "Required parameter projectId was null or undefined when calling addInventoryItem.",
"name": "RequiredError",
"stackTrace": [ ]
}

so I am not sure what to do about that. I can't find any info on where the projectId is needed when calling the addInventoryItem. I actually can't find "addInventoryItem" anywhere in the documentation.

I tried making a parameter for the cloud code for playerId and projectId, but they don't seem to be doing anything at all.

Any ideas?

I think your projectId & playerId is already a local variable from this line of code you already have:
const { projectId, playerId } = context;

This is how I'm adding items. Maybe it will help you:
const itemId = "SWORD";
const addInventoryRequest = { inventoryItemId: itemId };
const requestParameters = { projectId, playerId, addInventoryRequest };
await economy.addInventoryItem(requestParameters);

After looking at your code more closely, I think I found the culprit:
inventory.addInventoryItem(projectId, playerId, { inventoryItemId:"TESTSWORD" });

The addInventoryItem takes an object not individual parameters. It should be written like this:
inventory.addInventoryItem({projectId, playerId, { inventoryItemId: "TESTSWORD" }});

[quote=“lsaeteurn”, post:3, topic: 908718]
I think your projectId & playerId is already a local variable from this line of code you already have:
const { projectId, playerId } = context;

This is how I’m adding items. Maybe it will help you:
const itemId = “SWORD”;
const addInventoryRequest = { inventoryItemId: itemId };
const requestParameters = { projectId, playerId, addInventoryRequest };
await economy.addInventoryItem(requestParameters);
[/quote]
That definitely seems to be better, but now I am either getting an error that projectId is not defined, or that “economey” is not defined. Neither of these make much sense to me though, projectId should already be baked when I run the code through test or through unity right? I shouldn’t need to specify that right? I tried adding a parameter for the projectId and it still gives the same error.

The economy error also doesn’t make much sense, since shouldn’t that just be grabbing the economy api?

This system is confusing in some very odd places :smile:

[quote=“lsaeteurn”, post:4, topic: 908718]
After looking at your code more closely, I think I found the culprit:
inventory.addInventoryItem(projectId, playerId, { inventoryItemId:“TESTSWORD” });

The addInventoryItem takes an object not individual parameters. It should be written like this:
inventory.addInventoryItem({projectId, playerId, { inventoryItemId: “TESTSWORD” }});
[/quote]
I did try that at one point, I found a slightly different code somewhere that has the extra {} in there, but when I try that the code tells me there’s an unexpected { so I am not sure if this is the way to do it or not…

So this is where I am at right now with the way I was doing the code before:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const createdItem = await inventory.addInventoryItem({projectId, playerId, { inventoryItemId: "TESTSWORD" }});

which is currently giving me the error:

Compilation Error

SyntaxError: Unexpected token '{'

AddInventory.js:7:77

Which is just a little bit annoying. I tried removing the {} but then I get the error:

Invocation Error

RequiredError: Required parameter projectId was null or undefined when calling addInventoryItem.

{
"message": "Required parameter projectId was null or undefined when calling addInventoryItem.",
"name": "RequiredError",
"stackTrace": [ ]
}

So I am just stumped as to how this is supposed to work.

I did try using your code to add an item:

const _ = require("lodash-4.17");

module.exports = async ({ params, context, logger }) => {
  const itemId = "TESTSWORD";
  const addInventoryRequest = { inventoryItemId: itemId };
  const requestParameters = { projectId, playerId, addInventoryRequest };
  await economy.addInventoryItem(requestParameters);
}

but I am currently getting the error:

Invocation Error

ReferenceError: projectId is not defined

at module.exports (AddItem.js:6:31)

Right now I am just completely stumped as to how you just add an item to an inventory. Seems weirdly complicated for something so incredibly simple.

My code was just a snippet so it won't really run like that.

I think the problem is that when you nest like that in JS, it means something a bit different.

Try this:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const itemInfo = { inventoryItemId: "TESTSWORD" };
  const addInventoryRequest = { inventoryItemId: itemInfo };
  const createdItem = await inventory.addInventoryItem({projectId, playerId, addInventoryRequest});
}

The nested version of that would look like this:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const createdItem = await inventory.addInventoryItem({ projectId, playerId, addInventoryRequest : {inventoryItemId: "TESTSWORD"} });
}

[quote=“lsaeteurn”, post:8, topic: 908718]
My code was just a snippet so it won’t really run like that.

I think the problem is that when you nest like that in JS, it means something a bit different.

Try this:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const itemInfo = { inventoryItemId: "TESTSWORD" };
  const addInventoryRequest = { inventoryItemId: itemInfo };
  const createdItem = await inventory.addInventoryItem({projectId, playerId, addInventoryRequest});
}

The nested version of that would look like this:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const createdItem = await inventory.addInventoryItem({ projectId, playerId, addInventoryRequest : {inventoryItemId: "TESTSWORD"} });
}

[/quote]
So weird thing, the first version gave me a big error that I haven’t seen before, but the second one works like a charm! I will test and refine it, but so far it seems to be working perfectly! Thanks!

Now I need to move onto the next of many steps haha

[quote=“lsaeteurn”, post:8, topic: 908718]
My code was just a snippet so it won’t really run like that.

I think the problem is that when you nest like that in JS, it means something a bit different.

Try this:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const itemInfo = { inventoryItemId: "TESTSWORD" };
  const addInventoryRequest = { inventoryItemId: itemInfo };
  const createdItem = await inventory.addInventoryItem({projectId, playerId, addInventoryRequest});
}

The nested version of that would look like this:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const createdItem = await inventory.addInventoryItem({ projectId, playerId, addInventoryRequest : {inventoryItemId: "TESTSWORD"} });
}

[/quote]
Quick question, I am trying to replace the “TESTSWORD” with a parameter, seems to be giving me more trouble than I imagined and not sure why. I tried just a simple replace:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);
  const { itemId } = params;

  const createdItem = await inventory.addInventoryItem({ projectId, playerId, addInventoryRequest : {inventoryItemId: itemId} });
}

but that throws a big error. I tried working backwards a little bit to get to your expanded example of it, but I keep getting the same error:
Invocation Error
------------------------------
Error: Request failed with status code 404
{
“message”: “Request failed with status code 404”,
“name”: “Error”,
“request”: {
“data”: “{"inventoryItemId":"TESTSTICK"}”,
“headers”: {
“Accept”: “application/json, text/plain, /”,
“Authorization”: “Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6InB1YmxpYzpBNTYwOTVEQS0xODJDLTQ1MjMtOUQyNS1DNzlEMzNBNEY5OUIiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOlsiaWRkOjQ4YWYxYTAwLWEzZmMtNGMyOC04OTlkLTg1NzQwMDZiZjY4MCIsImVudk5hbWU6cHJvZHVjdGlvbiIsImVudklkOjA3NmU4N2I2LWIwMzEtNDI5YS1hNzZiLWFmZGFiZmEzNzNiYSIsInVwaWQ6MDkzZmUwNjUtNzU0MC00YmI3LWI4ZmItY2JiMDYyOTA0NThjIl0sImV4cCI6MTY3NjA2MDM0MSwiaWF0IjoxNjc2MDU2NzQxLCJpZGQiOiI0OGFmMWEwMC1hM2ZjLTRjMjgtODk5ZC04NTc0MDA2YmY2ODAiLCJpc3MiOiJodHRwczovL3BsYXllci1hdXRoLnNlcnZpY2VzLmFwaS51bml0eS5jb20iLCJqdGkiOiI2NmJiZTZlOC00YWU5LTQzMmUtYjQyYS04NDRmZGY5YWUwNGEiLCJuYmYiOjE2NzYwNTY3NDEsInByb2plY3RfaWQiOiIwOTNmZTA2NS03NTQwLTRiYjctYjhmYi1jYmIwNjI5MDQ1OGMiLCJzaWduX2luX3Byb3ZpZGVyIjoiYW5vbnltb3VzIiwic3ViIjoiN2hIbTBQajVFa1kxNFYxdzFXS1FjbjVZdUNhQyIsInRva2VuX3R5cGUiOiJhdXRoZW50aWNhdGlvbiIsInZlcnNpb24iOiIxIn0.icoqbcHmfpfVar6eKYMb_I5DslugWaVl7VZihSjcMsPsa8hgZCH5tThkH89eXB3Q__LlIIIypbK4ishHwhOXqD0Jmuk1Ga_Aao4jbMPokrWSCKPaiEhxhceJba30Dh83HbDRGX6bErxe7s96LCqovwpmRJNuuIMY9itqu2TtVi5mIIbEE0WnT_Fs46A1YxMROOKnLn07uBclp9NhjhijImqwxSMoQMqAKWw7MeaRHQneGnOzwoUP6aJclyB5DfMIJKHxNGxuawhsp-DNmwV2MRzl5GLc4KE8O5zlOB2xMeLX9bIkoWdAhILsDmvFHfSHHKE-xr5AuV-F-wIgyJ_ipQ”,
“Content-Length”: 31,
“Content-Type”: “application/json”,
“User-Agent”: “axios/0.21.4”
},
“method”: “post”,
“url”: “https://economy.services.api.unity.com/v2/projects/093fe065-7540-4bb7-b8fb-cbb06290458c/players/7hHm0Pj5EkY14V1w1WKQcn5YuCaC/inventory
},
“response”: {
“code”: 10303,
“detail”: “Inventory item not found”,
“instance”: null,
“status”: 404,
“title”: “Not found”,
“type”: “problems/basic”
}
}

Not really sure what to make of that. Its acting like the item doesn’t exist, but it definitely does exist. I have checked multiple times to make absolutely sure

Edit: Although now it seems to be working fine. So now I don’t know what to believe haha

Is the item "TESTSTICK" properly added to Economy?

[quote=“lsaeteurn”, post:11, topic: 908718]
Is the item “TESTSTICK” properly added to Economy?
[/quote]
It was properly implemented and published. Honestly have no idea what the problem was, but it is working now. I probably implemented something incorrectly and just fixed it without realizing it.

[quote=“lsaeteurn”, post:11, topic: 908718]
Is the item “TESTSTICK” properly added to Economy?
[/quote]
I think I found the problem, and I don’t think it has anything to do with the code. It seems when I create a new item through configuration it takes a few minutes before I can access it through things like code. That seems to be the only consistent thing in the tests I have been running on new items. Seems like it should have instant access, but if I have to work around a delay of a few minutes I can live with that. I’ve dealt with far worse haha.

Anyway thanks for all the help, I can add inventory items now properly.

And just in case if anyone else needs the code I used:

const { InventoryApi } = require("@unity-services/economy-2.3");

module.exports = async ({ params, context, logger }) => {
  const { itemId } = params;
  const { projectId, playerId } = context;
  const inventory = new InventoryApi(context);

  const createdItem = await inventory.addInventoryItem({ projectId, playerId, addInventoryRequest : {inventoryItemId: itemId} });
}