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.

Pat avatar image
Pat answered

TitleID : AA91

PlayFabID : F04556967DEF4E0

CharacterID : 9863DD60773DDBD9

ItemInstance : Changes each time

Data SlotNumber : 1

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 tried it with the documentation and it works with the data being past in. 

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

I also tested it with Postman, and the values work fine:

POST /Server/UpdateUserInventoryItemCustomData HTTP/1.1
Host: aa91.playfabapi.com
Content-Type: application/json
X-SecretKey: { {SecretKey}}
{
"PlayFabId": "F04556967DEF4E0",
"CharacterID" : "9863DD60773DDBD9",
"ItemInstanceId": "7A178061E1D346C3",
"Data": { "SlotNumber": "1" }
}

{
"code": 200,
"status": "OK",
"data": {}
}

I also copied your script to my own test project and tried it, and everything worked fine. So the issue has to be with the data values being passed by the client code. Reviewing the service code, I believe what's happening is that your client code is passing one or more of the values (PlayFab ID, Character ID, or Item Instance ID in particular) as a long int, rather than a string. In that case, it would be a decimal numeric value, whereas the UpdateUserInventoryItemCustomData is expecting a hexadecimal number in string format. Could you check that?

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 tried everything to make sure it is a string going through and the error is still happening. I do not know what I am doing wrong with this call at all.

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

So, I believe the issue is that what you're passing is the decimal version of the PlayFab ID. Can you put a breakpoint in and check the actual value being passed? For example, the PlayFab ID for your test should be "F04556967DEF4E0" (the hex representation). If the PlayFab ID is the problem, I believe what you'll see is "1082083721678353632" instead (the decimal representation). Please check the PlayFab ID, Character ID, and Item Instance ID, to make sure the hex number is what is passed in, in string form.

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

Check to make sure they are in string format and are in the hexadecimal format and all of the values are. Is there something wrong with my server call?

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

    return itemCustomData;
}

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 figured it out. I forgot to put args.data.(whatever the variable name is here). I wasn't getting the data that was being passed in. Thanks for your time again though. You have been such a great help.

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

Ah, of course! So easy to miss the obvious, sometimes. Nice catch! :)

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.