Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Is there a way to grant multiple quantities of an item with 1 call?

Discussion in 'Economy' started by CameronDWills, Jul 17, 2022.

  1. CameronDWills

    CameronDWills

    Joined:
    Feb 26, 2021
    Posts:
    73
    I'm using cloud code to grant a user some items:
    Code (JavaScript):
    1. inventoryApi.addInventoryItem(projectId, playerId, { inventoryItemId: chestContents[i].ID });
    Is there a way to pass in an integer to represent the quantity of said item that should be granted to the user? Right now I have loot boxes that give a random quantity of each item in the loot box between 1 and 5, for a max possibility of 15 item rewards. Right now I have to run this in a for loop which makes 15 inventoryapi calls which I feel like is going to be a bunch of unneccesary requests.

    Infact, while testing this script in cloud code I had to run the test code a few times back-to-back and I think this triggered a limit, as I started getting "There was an error test running the script. Please try again." errors on any script I tried to run for a few minutes.

    I've looked through the 'command batching' use case found here: com.unity.services.samples.use-cases/CommandBatch_ProcessBatch.js at main · Unity-Technologies/com.unity.services.samples.use-cases (github.com)

    But this doesn't seem to be a solution to this problem.
     
    Last edited: Jul 17, 2022
  2. stu_dious_

    stu_dious_

    Joined:
    Apr 18, 2022
    Posts:
    4
    We had to add our own support for 'stackable' items. You could add something like 'stackable : true' or 'amount : 0' in the item definition's customData then test for it. It true or exists then manage the changing item balance using it's instanceData object.
     
  3. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    169
    It isn't currently possible to add multiple items to a player's inventory in a single call, but it is someting we are investigating. I'll add both your voices to the existing feature request for Inventory batch operations. Being able to add or remove multiple inventory items in a single API call will be much more efficient.

    Yes, this is exactly what I have been doing.

    I have an RPG style inventory experiment that uses Custom Data to configure how different items can be stacked and control how many item slots chests and containers can hold. I manipulate all the items locally on the client holding a clean and dirty state then synchronise them back to the Economy server less frequently, using the Instance data to hold the quantity for each stack.

    This gets around the problem of having a crazy amount of calls and network requests as the player runs around collecting loot, but it does mean there will be opportunities for the player's local and server inventory state to get out of sync, so needs to be handled carefully.

    When I get my experiment cleaned up I'll share more detail

    My Custom Data looks like
    Code (JavaScript):
    1. {
    2.     "category": "FOOD",
    3.     "slots": 0,
    4.     "quantity": 1,
    5.     "maxStack": 6,
    6.     "isStackable": true,
    7.     "canPickup": true,
    8.     "assetName": "Apple"
    9. }