Help with players being able to pick up an item twice as RPC does not remove it instantly

Options

Hello all!

I've ran into a issue and would appreciate if anyone could give some advice on how to approach it. Basically, I am working on a game where random items can spawn and the players can pick them up through the level. The items are instantiated fine and when a player picks up an item, an RPC is sent which destroys the item for everyone. This all works fine, but there is an issue I've noticed with a lot of players where if both players press the pick up button at the same time on the same item, they will both get the item as I assume the RPC is of course not executed in that split second where both players press the pick up button.

Of course these leads to players "duplicating" items and I've wracked my brain trying to come up with a solution to prevent this. I understand this may be more of a logical solution or problem so if anyone has any ideas around a solution it would be appreciated. I've thought about RPCs to check if the item has already been picked up but it would likely be the same issue as the original destroying the item RPC as if both players press the button at the same time it would likely be the same result


Appreciate any input or suggestions on this!

Answers

  • Tobias
    Options

    This is not an easy case in PUN, as the state is distributed. It would be easier in Fusion.

    Within PUN you only have the option to write a pickup system leveraging "Check and Swap" for Custom Properties.

    In the Custom Properties you need a key per object and the value tells you who picked it up. To do so everyone tries to set the property with the condition that it doesn't belong someone else. You can do this by sending which value the key has before you pick up the item. Hope that makes sense.

    If you are not too far into the project, I would recommend Fusion. There are a few samples which implement pickups and power ups, e.g. Fusion Karts.