question

rewardclients avatar image
rewardclients asked

Cloud Script - get player inventory (update)

Hey!

I have to know how to get player inventory through Cloud Script, I mean that other player would run this Cloud Script and the second player inventory would be updated and he would see that on his device like this method:

    public void GetInventoryItemTime(string id)
    {
        var request = new PlayFab.ServerModels.GetUserInventoryRequest
        {
            PlayFabId = id,


            
        };


        PlayFabServerAPI.GetUserInventory(request, OnGetInventoryItemTime, OnError);
    }

but called by other player through Cloud Script.

I made something like this:

C# (Unity):

	public void OnRewardRevokedSuccess(PlayFab.AdminModels.RevokeInventoryResult result)
    {
		Debug.Log("Revoked Success");




		var request = new ExecuteCloudScriptRequest
		{
			FunctionName = "removeItem",


			FunctionParameter = new
			{
				playerID = whatID


			}


		};


		PlayFabClientAPI.ExecuteCloudScript(request, OnExecuteSuccess, OnExecuteError);


	}

public void OnExecuteSuccess(PlayFab.ClientModels.ExecuteCloudScriptResult result)
    {
		GameObject.Find("objects").GetComponent<objectsScript>().GetInventoryItemTime(result.FunctionResult.ToString());
    }


-------------------------------

Cloud Script:

handlers.removeItem = function(args){
    var playerID = args.playerID;


 
 return playerID;


}

But it doesn't works, please help guys, 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

From what I understand, you want to get other player's inventory through Cloud Script. You can call server.GetUserInventory in Cloud Script to get this info and return it to the client. You should not enable server API or admin API in your client project, because it may cause some security issues.

Here the Cloud Script code example:

handlers.removeItem = function(args){
	var playerID = args.playerID;//playerID should be PlayfabId
	var getInventoryResult = server.GetUserInventory({PlayFabId:playerID});
	return getInventoryResult.Inventory;
}

If there are any other problems ,feel free to let us know.

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.