Best way to check for existence of photonView on reconnect?

Options
I'm testing an edge case for a mobile game. In the game, player's get a
PhotonNetwork.Instantiate("PlayerObj", ...)
for each of them - this only gets instantiated at certain parts of the game, but usually doesnt exist.

If one them accidentally goes idle/hits the lock screen of their phone/or lose internet connection, they will get disconnected. Upon reconnecting, if the game state is at the same place as it was before, I need to create the "PlayerObj". But since the player is rejoining, PhotonNetwork will throw a duplication ViewID error when i try to create the PlayerObj again... because Photon cached the instantiated networked object and is creating it on its own.

It doesnt seem like photon network has a graceful way of handling this? I found a work around that simply uses a coroutine to wait for photonnetwork to reinstantiate cached objects before I try to create an object.
IEnumerator CreatePlayer(object[] data)
{
     yield return new WaitForSeconds(1);
     if (GameObject.Find(string.Format("PlayerObj-{0}", PhotonNetwork.LocalPlayer.UserId)) == null)
     {
         PhotonNetwork.Instantiate("PlayerObj", default(Vector3), default(Quaternion), 0, data);
     }
}

This seems like an ugly solution. Is there a better way?

Comments

  • Casey
    Options
    Hello, I'm still having issues with this. My hack solution doesn't seem to work perfectly. Anyone have any ideas?

    To add some more detail:
    • Disconnected player's PhotonView gets destroyed automatically for all clients who are still in the game
    • The player who rejoins, gets the photonView automatically recreated for them (by Photon design), but none of the clients do. So, when that rejoining player does RPC calls on their photonView, all the other clients get "PhotonView does not exist!" whenever an RPC is sent.

    Any ideas what I'm doing wrong?
  • Casey
    Options
    I think I may have finally figured it out...

    If a player is randomly disconnected from the internet, you need to make sure to still call
    PhotonNetwork.Destroy(photonView)
    
    even if you're the client who got disconnected (and is currently offline). That way when you come back online and rejoin the game, it doesnt reinitialize the view when everyone else's has already been permanently destroyed.
    
    private void OnDestroy()
    {
          Kill();
    }
    
    private bool killed = false;
    private void Kill()
    {
         if (photonView != null && photonView.IsMine && !killed)
         {
             killed = true;
             PhotonNetwork.Destroy(photonView);
         }
    }
    


    I had to add a "!killed" check because Kill() can be called in many different situations and sometimes would be called twice.