Concurrent pick up

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Concurrent pick up

ChazAshley
2017-02-21 23:01:12

Hello there. As you already figured out I want to implement concurrent picking up in my game (with 4 players max).
I have already checked the demo, but I don't really understand why it is concurrent, and how it prevents the situation when two players trying to pick up the same object.

As far as I can see, in the demo we doing the next.

When a player picks up an object, we check, whether it's our player or not, and if it is, then we send an RPC to all players, via server. When the RPC is called via server, we check if this call of RPC was performed by us, and if it is, then we add score.

But what will happen if on the other client another player picks up the same object at the same time? Then it will call the same RPC twice, but for different players, and when it come via server then two players get a score, right?

Please, tell me if I'm right or not, and if I am, how to deal with the problem.

Comments

nindim
2017-02-23 01:02:42

I was wondering about the same thing, but didn't see this post till now:

My thread: http://forum.photonengine.com/discussion/9313/stopping-multiple-players-collecting-same-powerup#latest

It does offer some of my own thoughts on how to deal with this...

nindim
2017-02-23 02:53:04

@ChazAshley
I've taken a closer look at the demo scene, I believe this does prevent 2 players concurrently pickig up the same pickup.

Each client executes its own RPCs and the other players. so the first PunPickup RPC to get processed from the servers (in-order) RPC list would disable the pickup game object. The pickup game object is checked for whether it is active or not in PickUpItem::PunPickup() and if it's not, it bails early. This is the cruciial piece of logic that prevents multiple players gettign the same pickup.

From my understanding, it will look like this, assume that latency is zero and the frames are the frame number on the sevrer.

  • Server Frame 0: Player 1 picks up Item 0 and sends PunPickup RPC

  • Server Frame 0: Player 2 picks up Item 0 and sends PunPickup RPC

  • Server Frame 1: Server receives both PunPickup RPCs at the same time and arbitrarily orders them in a list: Player 1 followed by Player 2.

  • Server Frame 2: Player 1 receives the in-order RPCs from the server, it processes the RPC from itself first (as this is how the server ordered it) and successfully gets the pickup. It then process the Player 2 RPC, but as the pickup GameObject is now disabled, it bails early.

  • Server Frame 2: Player 2 receives the in-order RPCs from the server, it processes Player 1's first (as this is how the server ordered it) and Player 1 gets the pickup. It then processes it's own RPC, but as the pickup GameObject is now disabled, it bails early, narrowly missing out on getting the pickup for itself.

hannahmontanna
2021-03-05 16:16:40

Don't send the rpc "via server" so it executed locally immediately... Then do an ownership request callback and course correct for the edge case (just disappear it for 2nd player in line or do a clever animation or something).

Back to top