Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to add limited quantity items?

Discussion in 'Economy' started by boyplayunity, Nov 16, 2023.

  1. boyplayunity


    Nov 8, 2017
    I have 2 question:
    1. If there is an item, each person is limited to one. It could be bought by a currency or real money or just increase from cloud code. What should i do to limit the count?
    2. If there is an virtual purchase have a limit of 5 times per person. How could i do?
    I saw some cloud code apis have a parameter said "writelock". Is it used in these situation? How to use them?
  2. boyplayunity


    Nov 8, 2017
    Maybe i could use currency for my question 1. The currency have a max limit.
  3. HeatherBolte


    Nov 18, 2023
    I also want to know. I hope someone will reply to you soon.

    Edit: I got my answer here. Thanks for solving my issue. You made my day :)
    Last edited: Nov 25, 2023
  4. GabKBelmonte


    Unity Technologies

    Dec 14, 2021

    There's a few things I can think of, the support for WriteLock would be one to avoid concurrency (race conditions), A race condition could be a cheater trying to use two different game clients to make the purchase, or perhaps an error in the code that the purchase is made twice.

    The idea is that if there's a different call made between the call you're making, and the one you're doing, then the second call will fail, as it will not have the same "key".

    This would allow you to check if the number of items is already one, before changing the player balance.

    If you're not worried about concurrency, then using a currency (or a "virtual" currency, a fake currency to track the max number of virtual purchases) could also work.

    CloudCode can host the logic to reject or accept the transaction based on whether the user has "used up" his virtual currency (1 item purchase, or 5 virtual transactions), you just need a place to save how many times the item/transaction has been executed, which Economy can serve.

    If you want to make sure this is "server authoritative", you may want to use Access Control to prevent users from faking api calls to economy, and use CloudCode. This will prevent cheaters since the only way to change the item/transaction would be through code you control in the server too.

    Hope this helps!

    Last edited: Nov 21, 2023
    boyplayunity likes this.