question

hunelgames avatar image
hunelgames asked

Replaced item

Hi,

I have an inventory system in my game.

Currently there are only 2 items(Angel wing piece and angel wing).

I want to do a simple thing: When i have 2 angel wing pieces in my inventory,i want them them removed from the inventory and replaced by an angel wing.

I wrote the code but it seemed a little complicated.

public void InventoryUpdate()
    {
        GetUserInventoryRequest request = new GetUserInventoryRequest();
        PlayFabClientAPI.GetUserInventory(request, result =>
        {
            List<ItemInstance> ii = result.Inventory;

            int slotid = 0;
            int AngelPieces = 0;

            for (int i = 0; i < 6; i++)
            {
                Slots[i].Unassign();
            }
            foreach (ItemInstance i in ii)
            {
                if (i.ItemId == "Angel Wing Piece")
                {
                    AngelInstanceId[AngelPieces] = i.ItemInstanceId;
                    AngelPieces++;
                }

                if (i.ItemId == "Angel Wing")
                {
                    Slots[slotid].Assign(UIItemDatabase.Instance.GetByID(31));
                    slotid++;
                }
            }
            if (AngelPieces== 2)
            {
                for (int i = AngelPieces - 1; i >= 0; i--)
                {
                    PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
                    {
                        FunctionName = "DeleteItems",
                        FunctionParameter = new { InvItemId = AngelInstanceId[i] },
                        GeneratePlayStreamEvent = true,
                    }, result3 => Debug.Log(""), error => Debug.Log(""));
                }


                PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
                {
                    FunctionName = "GrantItem",
                    FunctionParameter = new { catalogVer="Items",ItemId="Angel Wing"},
                    GeneratePlayStreamEvent = true,
                }, result3 => Debug.Log(""), error => Debug.Log(""));


                Slots[slotid].Assign(UIItemDatabase.Instance.GetByID(31));
                slotid++;
            }
            else if (AngelPieces == 1)
            {
                Slots[slotid].Assign(UIItemDatabase.Instance.GetByID(28));
                slotid++;
            }
        }, error =>{});
    }

*Is there a simpler way to do this?

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

·
Sarah Zhang avatar image
Sarah Zhang answered

According to your game design, the main logic can’t be simplified. For the purpose of optimization, you can consider combining “DeleteItems” function and "GrantItem" to one function to avoid clients call the "GrantItem" function separately. Hosting the judgment logic to the server-side can avoid cheating to a certain extent.

Besides, we would suggest you define “Angel Wings Piece” as the stackable consumable item, if so you can get its count from the “RemainingUses” property directly. The code would be something like this.

foreach (ItemInstance i in ii)
            {
                if (i.ItemId == "Angel Wing Piece")
                {
                    AngelInstanceId = i.ItemInstanceId;
                    AngelPieces = i.RemainingUses.Value;
                }
            }

After you set the “Angel Wings Piece” to the stackable consumable item. You can use one instance item Id to consume muti them on the server-side. So you can omit polling for “AngelPieces”. The corresponding code could be the following one. In the “DeleteItem” function, you need to use the server.ConsumeItem API. For more details about item’s configuration, please refer to the documentation -- https://docs.microsoft.com/en-us/gaming/playfab/features/commerce/items/catalogs.

       if (AngelPieces >= 2)
            {
                PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
                {
                    FunctionName = "DeleteItems",
                    FunctionParameter = new { InvItemId = AngelInstanceId, count = (int)Math.Floor((double)AngelPieces / 2) * 2 },
                    GeneratePlayStreamEvent = true,
                }, result3 => Debug.Log(""), error => Debug.Log(""));
            }
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.