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

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

  • Solved it, thanks for looking into it to whomever did.
  • JohnTube
    JohnTube ✭✭✭✭✭
    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?
  • 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
    JohnTube ✭✭✭✭✭
    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();
  • 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.
  • Hello @Thomukas1 , Did you solve your problem of calling up RPC before an unplanned player exit?
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited August 2019
    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.
  • @JohnTube , Thank you very much for your answer!
  • 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
    JohnTube ✭✭✭✭✭
    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.