question

RGV avatar image
RGV asked

Asking for inventory concurrently grants duplicated items

We have a function which returns some item from user's inventory. That's called by another function, in order to check whether or not player already owns the item. If not, grants a new one. Otherwise it updates custom data from the existing one.

That being said and assuming stackable items, we're facing some critical problems when two concurrent calls ask whether or not such an item already exists in player inventory.

Flow:

[FunctionCall1] ask if [Item] exists. GetItem() calls server.GetUserInventory() and returns [Item] doesn't exist.

Thus, [FunctionCall1] will grant [Item] to player's inventory and set custom data properly.

Meanwhile, [FunctionCall2] follows the same flow. When [FunctionCall2] ask if [Item] exists in inventory, GetItem() says it doesn't — but [FunctionCall1] is granting it right now!.

Therefore, [FunctionCall2] will grant another new [Item] instance, so they coexist, two different stacks (different instance ID of the same item). But we need any stack never to be instantiated more than once.

Having such a basic problem, we think we're misunderstanding how to properly check+grant items. Any thoughs?

Thanks so much.

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

·
brendan avatar image
brendan answered

Concurrent calls are processed concurrently. Since the data being queried is the same in both cases, yes, they're doing the same thing - adding the item. What you're looking for is a way to have a distributed "lock" on the player inventory for each action.

With the legacy commerce service, that's simply not possible. With variances in delivery time for packets, it should be fairly rare, apart from low-latency "players" who are deliberately forming those calls to try to cheat your game.

The newer commerce service we're working on releasing supports idempotency natively, so that you can prevent 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.

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.