question

rkacalski avatar image
rkacalski asked

Revoke Inventory Items CloudScript,Revoke inventory items cloud script

Hey everyone!

I'm new to playfab and have been able to create a few cloudscripts with little difficulty. That is up until now! I'm having trouble creating a cloud script to implement the Revoke Inventory Items found here:

https://learn.microsoft.com/en-us/rest/api/playfab/server/player-item-management/revoke-inventory-items?view=playfab-rest

Originally I used the singular version, Revoke Inventory Item and got a functioning script. That said I dont think its the best way to revoke multiple items as I had it in a for loop and it will make multiple calls, where as the Revoke Inventory Items will only make one.

So far I tried a method like this (newCardsToDelete is an array of ItemInstanceIds transferred to the cloud script from the client):

var newCardsToDelete = args.newCardsToDelete;

//Delete old Cards
    var deleteOldCards = server.RevokeInventoryItems(
        {
            PlayFabID: currentPlayerId,
            Items : newCardsToDelete
        });

When this didnt work I looked at the documentation again and noticed it doesnt need the PlayFabID field. So I removed that but it still didn't work. Then I noticed the Items field wants an array of player items rather than string InstanceIDs. So I went back and created some code where I got the players inventory checked if the item's ID matched an InstnaceID that the client sent. Once this was done that object was added to another array. However this still did not work.

var oldCardIds = args.oldCardIds;

var getInventoryResult = server.GetUserInventory(
	    {
	        PlayFabID: currentPlayerId
	    });
	    
	var inv = getInventoryResult.Inventory;
	const CardsToDelete = [];
    for(let i = 0; i < inv.length; i++)
	{
	    if(oldCardIds.includes(inv[i].ItemInstanceId))
	    {
	        CardsToDelete.push(inv[i]); 
	    }
	}
	
    //Delete old Cards
    var deleteOldCards = server.RevokeInventoryItems(
        {
            PlayFabID: currentPlayerId,
            Items : CardsToDelete
        });

I feel like I'm missing something obvious and have been stuck for a couple days now. Any help here would be much appreciated!

,

Hey everyone,

So this is my first time using playfab and I'm having some difficulties creating a cloud script for Revoke Inventory Items. I've read through the documentation posted here https://learn.microsoft.com/en-us/rest/api/playfab/server/player-item-management/revoke-inventory-items?view=playfab-restand I'm still having some trouble. Originally, I was going to use the Revoke Inventory Item feature, and even got a cloud script to function the way I wanted. However I realized this wasn't an efficient way to do it since it was in a for loop and would create multiple calls rather than just one call by using Revoke Inventory Items. If anyone could help me out and give me a basic cloud script that will execute this I would appreciate it!

Thanks!

CloudScriptPlayer Inventory
10 |1200

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

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

You can refer to the code below.

handlers.CTs = function (args, context)
{
    let itemsWantToDel = args.ItemsWantToDel;
    let inventory = server.GetUserInventory({PlayFabId: currentPlayerId}).Inventory;
    let itemsToDel = [];
    for(let i = 0; i < inventory.length; i++)
                {
                    if(itemsWantToDel.includes(inventory[i].ItemInstanceId))
                    {
                        itemsToDel.push({ItemInstanceId:inventory[i].ItemInstanceId,PlayFabId:currentPlayerId}); 
                    }
                }
    let ret = server.RevokeInventoryItems({Items : itemsToDel});
    return ret;
};
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.

rkacalski avatar image rkacalski commented ·

Thanks so much worked perfectly!

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.