question

Pat avatar image
Pat asked

Updating an item intances custom data

So i am trying to change an character's item's custom data called Slot Number. I added a new server version with this function in it:

handlers.GetSlotNumber = function (args)
{
    var charaId = args.CharaId;
    var itemInstance = args.ItemInstance;
    var slotNumber = args.SlotNumber;
    var playfabId = args.playFabId;
        
    var itemCustomData = server.UpdateUserInventoryItemCustomData(
    {
        CharacterId : charaId,
        PlayFabId : playfabId,
        ItemInstanceId : itemInstance,
        Data : slotNumber
    });

    return("Updated");
}

In unity I am running this code and returning i to the item's variable. My question is when I do this I get an error.

int GetSlotNumber(int v)
    {
        int i = -1;

        Dictionary<string, string> slotChange = new Dictionary<string, string>()
        {
            {"playFabId", player.playFabID},
            {"CharaId", player.playerID},
            {"ItemInstance", instanceId},
            {"SlotNumber", v.ToString()}
        };

        RunCloudScriptRequest request = new RunCloudScriptRequest()
        {
            ActionId = "GetSlotNumber",
            Params = new { data = slotChange }
        };

        PlayFabClientAPI.RunCloudScript(request,(
        result) => 
        {
            Debug.Log("Success!");
            i = v;
        }, 
            (error) =>
        {
            Debug.Log ("Error: " + error.Error);
            Debug.Log ("Error Message: " + error.ErrorMessage);
        });

        return i;
    }

This is the error message I am getting:

Error Message: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object

10 |1200

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

brendan avatar image
brendan answered

In your script, and specifically in the UpdateUserInventoryItemCustomData call, the Data parameter is actually an object containing Key/Value pairs for the data to be set. So the slot number should actually be something like:

slotNumber: slotNumber

In the data. And bear in mind that the Key is going to be interpreted as a literal (see this post for more info: https://community.playfab.com/hc/en-us/community/posts/206963508-Writing-to-a-dynamic-Key-in-Cloud-Script). 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.

Pat avatar image
Pat answered

Okay, I changed the call to:

var itemCustomData = server.UpdateUserInventoryItemCustomData(
    {
        CharacterId : charaId,
        PlayFabId : playfabId,
        ItemInstanceId : itemInstance,
        Data : { slotNumber : slotNumber }
    });

and go the same error.

10 |1200

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

Pat avatar image
Pat answered

I also tried:

var dataPayload = {};
    var keyString = "SlotNumber";
    dataPayload[keyString] = slotNumber;
    
    var itemCustomData = server.UpdateUserInventoryItemCustomData(
    {
        CharacterId : charaId,
        PlayFabId : playfabId,
        ItemInstanceId : itemInstance,
        Data : dataPayload
    });

giving the same error. I followed the post and tried everything and always giving me the same error.

10 |1200

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

brendan avatar image
brendan answered

Can you give us the rest of the error message and callstack? There should be an "at..." line immediately following the TargetInvocationException text you posted. To be clear, the NullReferenceException means that something in your game's code is dereferencing a null pointer somewhere - that's not occurring in the Cloud Script, but in the local code. So that's what we need to track down.

10 |1200

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

Pat avatar image
Pat answered

Ah, so it is happening when I am creating the dictionary for the params. Here is the code where I am running the call:

int GetSlotNumber(int v)
    {
        int i = -1;

        Dictionary<string, string> slotChange = new Dictionary<string, string>()
        {
            {"playFabId", player.playFabID},
            {"CharaId", player.playerID},
            {"ItemInstance", instanceId},
            {"SlotNumber", v.ToString()}
        };

        RunCloudScriptRequest request = new RunCloudScriptRequest()
        {
            ActionId = "GetSlotNumber",
            Params = new { data = slotChange }
        };

        PlayFabClientAPI.RunCloudScript(request,(
        result) => 
        {
            Debug.Log("Success!");
            i = v;
        }, 
            (error) =>
        {
            
            Debug.Log ("Error: " + error.Error);
            Debug.Log ("Error Message: " + error.ErrorMessage);
        });

        return i;
    }

 

This function is being called when I am setting the value in the script.

10 |1200

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

brendan avatar image
brendan answered

And what are the values of all your parameters, when viewed in the debugger? In particular, what is player set to?

10 |1200

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

Pat avatar image
Pat answered

Okay, so the values were coming up null to begin with. I added some code to add the values there before the function gets called and I get Fatal error in gc too many threads.

10 |1200

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

brendan avatar image
brendan answered

That error is specifically from Unity - there are quite a few threads on it in their forums: http://answers.unity3d.com/search.html?f=&type=question&redirect=search%2Fsearch&sort=relevance&q=fatal+error+in+gc+too+many+threads. As it's not related to anything in the PlayFab SDK or service, I'd recommend opening a ticket with the Unity team, with complete details on the issue as you're seeing it.

I did do some review of the various threads, and from what I'm seeing, there's no strong consensus on the cause or solution. For some people, updating drivers seems to have worked, while for others, it was due to other programs running on the system at the same time.

 

10 |1200

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

Pat avatar image
Pat answered

Okay, I fix that problem by doing it differently. The CloudScript is being called now but coming back with an error:

Error Message: PlayFabAPIError InvalidParams: Invalid input parameters
    at __playfab_internal_v1.server_request (Script Document:36:13) ->             throw apiResult;
    at server.UpdateUserInventoryItemCustomData (Script Document:478:90)
    at handlers.GetSlotNumber (main.js:22:30)
    at __playfab_internal_v1.invokeHandler (Script Document:20:22)

 

The current script is:

handlers.GetSlotNumber = function (args)
{
    var charaId = args.CharaId;
    var itemInstance = args.ItemInstance;
    var slotNumber = args.SlotNumber;
    var playfabId = args.PlayFabId;
    
    var dataPayload = {};
    var keyString = "SlotNumber";
    dataPayload[keyString] = slotNumber;
    
    var itemCustomData = server.UpdateUserInventoryItemCustomData(
    {
        CharacterId : charaId,
        PlayFabId : playfabId,
        ItemInstanceId : itemInstance,
        Data : dataPayload
    });

    return("Updated");
}

10 |1200

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

brendan avatar image
brendan answered

What are the details of the values you're passing in (CharacterId, etc.), including the Title ID? With that, I can test this against my own title, to see if I can reproduce the error.

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.