question

shawnryanbruno avatar image
shawnryanbruno asked

Unexpected Character Parsing Value With ExecuteInventoryOperations

Getting this error:

 LogPlayFab: Response : {"code":200,"status":"OK","data":{"FunctionName":"ManageInventory","Revision":236,"FunctionResult":{"error":"An error occurred during inventory management"},"Logs":[{"Level":"Info","Message":"Current PlayFab ID: [object Object]"},{"Level":"Info","Message":"Attempting to manage inventory for PlayFab ID: [object Object]"},{"Level":"Error","Message":"PlayFab API request error","Data":{"api":"/Inventory/ExecuteInventoryOperations","request":{"Entity":{"Id":{"PlayerProfile":{"PublisherId":"33D95BF6C24D18B5","TitleId":"E4875","PlayerId":"B4D8CF94E61675B0","DisplayName":"888888
 "}},"Type":"title_player_account"},"Operations":[{"Add":{"Item":{"Id":"95713627-e963-4356-a9b6-bf47c1b0560e"},"Amount":1}},{"Remove":{"Item":{"Id":"269f32a7-9bc7-41a5-b4f9-6c805e0a27c5"},"Amount":1}}]},"result":null,"apiError":**{"code":400,"status":"BadRequest","retryAfterSeconds":null,"error":"InvalidRequest","errorCode":1071,"errorMessage":"Invalid JSON in request","errorHash":"0892dc8df9125426190e0ad6a1ce4075490da75495e065cd870349a3ff41221b","errorDetails":{"ValidationError":["Unexpected character encountered while parsing value: {. Path 'Entity.Id', line 1, position 17."]}}}},**{"Level":"Erro
 r","Message":"Error in ManageInventory function: undefined"}],"ExecutionTimeSeconds":0.079836,"ProcessorTimeSeconds":0.00293,"MemoryConsumedBytes":23488,"APIRequestsIssued":2,"HttpRequestsIssued":0}}

Using this code:

 handlers.ManageInventory = function (args, context) {
     try {
         var playFabId = server.GetPlayerProfile({ PlayFabId: currentPlayerId });
         log.info("Current PlayFab ID: " + playFabId); // Debugging log statement
    
         // Validate PlayFab ID
         if (!playFabId) {
             log.error("Invalid PlayFab ID.");
             return { error: "Invalid PlayFab ID" };
         }
    
         log.info("Attempting to manage inventory for PlayFab ID: " + playFabId);
    
         // Step 3: Execute operations using executeInventoryOperations or similar function
    
     var executeRequest = economy.ExecuteInventoryOperations({
     "Entity": { "Id": playFabId, "Type": "title_player_account" },
     "Operations": [
         { "Add": { "Item": { "Id": "95713627-e963-4356-a9b6-bf47c1b0560e" }, "Amount": 1 } }, // Replace with your actual item IDs and amounts
         { "Remove": { "Item": { "Id": "269f32a7-9bc7-41a5-b4f9-6c805e0a27c5" }, "Amount": 1 } }
     ]
     });
         log.debug("Execute Request:", executeRequest); // Debugging log statement
    
         log.info("Inventory management successful.");
         return executeResult; // Return the result of executing the operations
     } catch (ex) {
         log.error("Error in ManageInventory function: " + ex.message);
         return { error: "An error occurred during inventory management" };
     }
 };

Could someone please tell me how I am screwing this up so badly? I am wracking my brain atm.

CloudScript
10 |1200

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

Xiao Zha avatar image
Xiao Zha answered

The Entity Id parameter used in ExecuteInventoryOperations request should be Title Player Account Id, which is different from Master Player Account Id (PlayFabId). You could refer to https://learn.microsoft.com/en-us/gaming/playfab/features/entities/available-built-in-entity-types#master_player_account have more information about PlayFab entities. And the GetPlayerProfile API cannot get the Title Player Account entity, you may use GetUserAccountInfo API (https://learn.microsoft.com/en-us/rest/api/playfab/server/account-management/get-user-account-info?view=playfab-rest) to get the Title Player Account entity id. Below is the code example you could refer to.

 handlers.ManageInventory = function (args, context) { 
                    
         var title_player_id = (server.GetUserAccountInfo({ PlayFabId: currentPlayerId})).UserInfo.TitleInfo.TitlePlayerAccount.Id;
            
         // Debugging log statement
        
         log.info(title_player_id); 
        
         // Execute operations using executeInventoryOperations 
        
         var result = economy.ExecuteInventoryOperations({
             "Entity": { "Id": title_player_id, "Type": "title_player_account" },
             "Operations": [
             { "Add": { "Item": { "Id": "275877ab-e6e1-4c0d-b4f1-4463cf757f30" }, "Amount": 1 } }
             ]
         });
                
             return result; 
     };
10 |1200

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

shawnryanbruno avatar image
shawnryanbruno answered

I get this error now, which I cannot seem to debug. Is it because I cannot use ExecuteInventoryOperations on Economy V2 items?

LogPlayFab: Response : {"code":200,"status":"OK","data":{"FunctionName":"ManageInventory","Revision":238,"Logs":[{"Level":"Info","Message":"92865FA9D4EB506D"},{"Level":"Error","Message":"PlayFab API request error","Data":{"api":"/Inventory/ExecuteInventoryOperations","request":{"Entity":{"Id":"92865FA9D4EB506D","Type":"title_player_account"},"Operations":[{"Add":{"Item":{"Id":"95713627-e963-4356-a9b6-bf47c1b0560e"},"Amount":1}},{"Remove":{"Item":{"Id":"269f32a7-9bc7-41a5-b4f9-6c805e0a27c5"},"Amount":1}}]},"result":null,"apiError":{"code":500,"status":"InternalServerError","retryAfterSeconds":n ull,"error":"InternalServerError","errorCode":1110,"errorMessage":"Unexpected operation","errorHash":null,"errorDetails":null}}}],"ExecutionTimeSeconds":0.3862086,"ProcessorTimeSeconds":0.004445,"MemoryConsumedBytes":22008,"APIRequestsIssued":2,"HttpRequestsIssued":0,"Error":{"Error":"CloudScriptAPIRequestError","Message":"The script called a PlayFab API, which returned an error. See the Error logs for details.","StackTrace":"Error\n at Object.server_request (Script:181:24)\n at economy.ExecuteInventoryOperations (Script:818:84)\n at handlers.ManageInventory (E4875-main.js:10:27)\n

1 comment
10 |1200

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

Xiao Zha avatar image Xiao Zha commented ·

As you can see in the doc: https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/execute-inventory-operations?view=playfab-rest#inventoryoperation, PlayFab doesn’t support “Remove” operation, you may consider using “Delete” or “Subtract” operations instead.

0 Likes 0 ·

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.