question

chris avatar image
chris asked

List is returned as List

Hi,

I'm attempting to use the UnitySDK by working with the ProgressiveRewards recipe. It seems that when I receive the list of grantedItems I'm receiving a List<System.Object> instead of List<ItemInstance> and it throws a cast exception if I attempt to cast the list.

In ProgressiveRewards.cs the line:

List<ItemInstance> grantedItems = (List<ItemInstance>)result.FunctionResult; 

Throws an exception:

InvalidCastException: Cannot cast from source type to destination type. 

When looking at the contents of result.FunctionResult it was a List<System.Object>. It did not seem to matter whether or not items were being granted (ie whether there were items in the list).

I am using the .unitypackage from here: https://github.com/PlayFab/UnitySDK/blob/master/PlayFabClientSDK.unitypackage

public const string VersionString = "UnitySDK-0.32.160711";

Any ideas on how I can fix this?

Thanks

2 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.

chris avatar image chris commented ·

It looks as tho the title stripped out my generics. :)

0 Likes 0 ·
chris avatar image chris commented ·

I have just pulled the full sample from Git and run the standalone project. After fixing the 'checkkkkin' error and runnning, the grantedItems is always returning null.

0 Likes 0 ·
brendan avatar image
brendan answered

Ah, wait, I see. You're talking about the GrantedItemInstance array you get back from the call to ExecuteCloudScript. The thing is, what you get back from Cloud Script calls is indeed JSON objects that you need to deserialize into the appropriate types. If you have a look at the sample, in OnCheckInCallback, we do exactly that.

Edit: And to correct one thing - the GrantedItemInstance is Server API only, so please ignore the suggestion to use it. Deserializing into the ItemInstance is indeed the right thing to do.

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.

chris avatar image chris commented ·

Hi Brendan, thanks for the reply. Also I wasn't sure if I should use Github or this. I don't mean to be spamming you. :)

To clarify, the sample in OnCheckInCallback does not correctly deserialize, that is the exception that I'm referring to. You can see my workaround in my answer below. But converting a JsonObject *back* into a string and then calling DeserializeObject feels a bit odd. Is there a better way to do that? For example:

JsonWrapper.DeserializeObject<List<ItemInstance>>(result.FunctionResult);

Thanks

0 Likes 0 ·
brendan avatar image brendan chris commented ·

Sorry, the initial question showed an attempt to cast the FunctionResult, which isn't valid, so I was assuming that was what you were referring to. If you mean the existing sample doesn't work as-is, can you please file that as an issue in GitHub on it? Thanks!

0 Likes 0 ·
chris avatar image chris brendan commented ·

I have filed both issues on github (#5 and #6).

0 Likes 0 ·
brendan avatar image
brendan answered

Thanks for calling that out (and in particular for adding the issue to GitHub, so that our tools folks had that report). The type for the FunctionResult from a grant operation is actually GrantedItemInstance. Can you give that a try?

10 |1200

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

chris avatar image
chris answered

I have found a solution to the problem. It looks as though SimpleJson is deserializing everything but cannot reflect the type of FunctionResult because it is of type object. It creates a list of System.Object which contains a list of JsonObjects. It is possible to convert the json object back to a string then deserialize it to the correct type, which is pretty ridiculous but it works.

List<System.Object> grantedItems = (List<System.Object>)result.FunctionResult;
    
    if(grantedItems != null && grantedItems.Count > 0)
    {
      Debug.Log(string.Format("You were granted {0} items:", grantedItems.Count));
      
      string output = string.Empty;
      foreach(var obj in grantedItems)
      {
        ItemInstance item = JsonWrapper.DeserializeObject<ItemInstance>(obj.ToString());
        output += string.Format("\t {0}: {1}\n", item.ItemId, item.Annotation);
      }
      Debug.Log(output);
    }


I would definitely say this is a dumb way to get data out of this SDK. But I'm at least able to use it now. I would prefer to know the correct way to use this SDK. Or if I'm somehow misconfigured?

Thanks

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.