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.