question

malsowayegh avatar image
malsowayegh asked

CloudScript: UnlockContainerInstance

Hello,

Im trying to do the following:

* unlock a container using cloud script for a players

* retrieve ItemIds of the items dropped in the container (container contains a drop table)

because I want to do some extra actions on the ItemIds only

my problem is the following: when I try to access GrantedItems from the response and get an item on a specified index I can't retrieve ItemId.

it throws an error and tells me to check my argument. it Also set it to null although I have ItemIds in response.

i'm accessing the ItemIds like this: unlockContainerBox.GrantedItems[0]["ItemId"];

I have the following Code:

handlers.unlockContainer = function (args, context) 
{
  var containerID = args.containerID;
  //unlock box
  var unlockContainerBox = server.UnlockContainerInstance
  (
   		{
  			PlayFabId: currentPlayerId,
  			ContainerItemInstanceId: containerID
		}
  );
  var Item =  unlockContainerBox.GrantedItems[0]["ItemId"];
	return Item;


}
CloudScript
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Andy avatar image Andy ♦♦ commented ·

Have you tried logging the full response from UnlockContainerInstance to determine exactly what's being returned? What do you see there?

0 Likes 0 ·
malsowayegh avatar image malsowayegh Andy ♦♦ commented ·

check this out.

even this doesn't work. it seems its reading it as null

but if I try with RemainingUses or UnitPrice it works fine

 var testItems = {
        "ItemId": "shield_level_5",
        "ItemInstanceId": "Unique Item ID",
        "ItemClass": "shields",
        "PurchaseDate": "2014-03-07T00:00:00Z",
        "Expiration": "2015-10-01T00:00:00Z",
        "RemainingUses": 10,
        "Annotation": "No Annotation",
        "CatalogVersion": "5",
        "BundleParent": "crate block 1",
        "UnitPrice": 0
      }
  return JSON.parse(JSON.stringify(testItems["ItemId"]));
0 Likes 0 ·
malsowayegh avatar image malsowayegh Andy ♦♦ commented ·

Here is the full response of the original code using: log.info(unlockContainerBox);

{
    "FunctionResult": null,
    "Logs": [
        {
            "Level": "Info",
            "Message": "{\"UnlockedItemInstanceId\":\"99DE963BA84C971E\",\"GrantedItems\":[{\"ItemId\":\"CARD:94\",\"ItemInstanceId\":\"A0D3FCA0E0C4CA0C\",\"ItemClass\":\"Card\",\"PurchaseDate\":\"2018-09-30T16:52:54.912Z\",\"RemainingUses\":2,\"UsesIncrementedBy\":1,\"Annotation\":\"Unlocked Crate\",\"CatalogVersion\":\"Card\",\"DisplayName\":\"Defensive Trap Extra troops\",\"UnitPrice\":0},{\"ItemId\":\"CARD:78\",\"ItemInstanceId\":\"2979460DBA8B2566\",\"ItemClass\":\"Card\",\"PurchaseDate\":\"2018-09-30T13:58:22.017Z\",\"RemainingUses\":7,\"UsesIncrementedBy\":1,\"Annotation\":\"Unlocked Crate\",\"CatalogVersion\":\"Card\",\"DisplayName\":\"Troops Per Turn 3\",\"UnitPrice\":0}]}",
            "Data": null
        }
    ],
    "ExecutionTimeSeconds": 0.206374,
    "MemoryConsumedBytes": 37400,
    "APIRequestsIssued": 1,
    "HttpRequestsIssued": 0,
    "Error": null
}
0 Likes 0 ·
brendan avatar image
brendan answered

It looks like you're running into the same issue as in this thread: https://community.playfab.com/questions/3816/list-is-returned-as-list.html. Can you try updating with the deserialization step?

5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

malsowayegh avatar image malsowayegh commented ·

I've already tried JSON.parse() multiple times. I also had to use JSON.stringify() inside the parse() because it threw an error.

myJSON = JSON.parse(JSON.stringify(unlockContainerBox));
firstItem = myJSON.GrantedItems[0];

now if I access firstItem like >

firstItem["itemId"]

or

 firstItem.ItemId 

<<< i get error

if I access >

firstItem[0] 

<I just get null <<<< this is so weird because I printed firstItem everything is there

=========

note: I already used Object.values(firstItem) << it returned an array of values which is good because everything is there when I print but when I access the array at any value that is in between "" I get null. other values like UnitPrice works fine

0 Likes 0 ·
malsowayegh avatar image malsowayegh commented ·

check this out:

log.debug() shows the value correctly

return gives me error

if I try to assign it to a var it also gives me an error

handlers.unlockContainer = function (args, context) 
{
  var containerID = args.containerID;


  //unlock box
  var unlockContainerBox = server.UnlockContainerInstance
  (
   		{
  			PlayFabId: currentPlayerId,
  			ContainerItemInstanceId: containerID
		}
  );
  
  log.debug(Object.values(unlockContainerBox.GrantedItems[0])[0]);

// if this line exist log.debug won't run
  return Object.values(unlockContainerBox.GrantedItems[0])[0];


}

0 Likes 0 ·
brendan avatar image brendan malsowayegh commented ·

Hm. It's not clear why, but yes, I'm seeing the same thing. In my testing, this resulted in getting you the Item ID in the response:

handlers.unlockContainer = function (args, context) 
{
  var containerID = args.containerID;
  //unlock box
  var unlockContainerBox = server.UnlockContainerInstance
  (
    {
      PlayFabId: currentPlayerId,
      ContainerItemInstanceId: containerID
    }
  );
  var Item = unlockContainerBox.GrantedItems[0];
  var ItemId = JSON.stringify(Item.ItemId);
  return ItemId;
}
1 Like 1 ·
malsowayegh avatar image malsowayegh brendan commented ·

Thats interesting. I guess its related to limitations on return in cloudscript

0 Likes 0 ·
Show more comments
malsowayegh avatar image
malsowayegh answered

it seems that I can't return the value for some reason

but I assigned it in a variable successfully and used it for other purpose.

Object.values()

// small sample of what I used


var itemID = Object.values(unlockContainerBox.GrantedItems[0])[0]
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.