How do I send RPC call right before I disconnect from the server? (or leave the room)

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.

How do I send RPC call right before I disconnect from the server? (or leave the room)

Thomukas1
2019-04-26 15:22:49

Hi, thanks for taking interest in my question. So basically, I am making a multiplayer VR game and I have run into a problem. When I grab an object, I take ownership of it and send RPC call to everyone that now this object is grabbed and do other stuff, like disable gravity. I noticed that if I am holding an object and I leave the game (stop Unity editor play mode), Both my virtual player and the grabbed object disappear. What I would like to do is to Release any objects that I am holding before disconnecting.

What I tried to do was to set OnDestroy method on my root VR character script that should release any objects being held (the object itself is responsible for RPC calls, like GetGrabbedRPC and GetReleasedRPC). This doesn't work because I get disconnected before OnDestroy is called.

The other thing I tried was to get my VR character tagged upon instantiation (OnPhotonInstantiate). And when OnPlayerLeftRoom, I would get the tagged object and use it to release any held items and then destroy itself, but I probably don't have enough knowledge for that, because what happens is that RPC doesn't get fired and the VR character doesn't even destroy itself, rather it becomes IsMine object of another active player.

Any help would be appreciated, I hope you guys understood what the problem is. Let me know if I can provide any useful information!

Comments

Thomukas1
2019-04-30 12:26:51

Solved it, thanks for looking into it to whomever did.

JohnTube
2019-04-30 12:38:45

Hi @Thomukas1,

Thank you for choosing Photon!

Could you share your solution so others can benefit from this?

Did you call PhotonNetwork.SendAllOutgoingCommands() to force sending RPC events ASAP before disconnects?

Thomukas1
2019-04-30 12:49:40

What I did was use tagobject to get a reference of the Photon player's GameObject. When that player leaves, the master client uses this tagobject reference (becomes owner of the player object) and calls "ReleaseHeldObjects". when all the held objects have been released, RPC call is made from master client to all clients and then the player object is destroyed.

However, I would like to hear how SendAllOutgoingCommands works, seems like a quicker and better solution?

JohnTube
2019-05-02 09:57:26

My suggestion to use SendAllOutgoingCommands is based on the title of the discussion.
I did not really read the content of the original post.

If you want the RPC to be sent right away call SendAllOutgoingCommands just after calling the RPC, something like:

photonView.RPC("methodName", args);   
PhotonNetwork.SendAllOutgoingCommands();

Thomukas1
2019-05-03 13:09:05

That would only work if the user disconnects willingly, like pressing a button to exit room. I'm sorry, maybe the title is incorrectly phrased.

blazz
2019-08-04 16:51:06

Hello @Thomukas1 , Did you solve your problem of calling up RPC before an unplanned player exit?

JohnTube
2019-08-05 09:49:40

Hi @blazz,

Thank you for choosing Photon!

Next PUN version 2.14 will have a change regarding this:

Changed: Execution order for disconnect on quit. As OnApplicationQuit is followed by OnDisable, PUN now disconnects in OnDisable, if OnApplicationQuit was called previously. Note: This means that you can now send a final message in OnApplicationQuit. It's not guaranteed to arrive anywhere, as the message won't be repeated on loss.

This is only for allowing sending events before quitting the app.
You still need to send the event or call RPC manually as shown in previous messages.

blazz
2019-08-05 12:27:44

@JohnTube , Thank you very much for your answer!

Jayantbarthwal
2020-05-31 06:05:18

How to call the rpc just before player disconnected from server , when phone shut down. Due to phone shutdown I can not use onApplicationQuit()

JohnTube
2020-06-01 09:38:53

Hi @Jayantbarthwal,

Thank you for choosing Photon!

Read the forum posts and the documentation carefully.
the answer is in the previous post above and in the documentation here.

Back to top