Search Unity

Network Object Disconnection Cleanup

Discussion in 'Multiplayer' started by Kirkules_, Oct 23, 2014.

  1. Kirkules_

    Kirkules_

    Joined:
    Aug 5, 2014
    Posts:
    65
    I'm building an online multiplayer vehicle racing game and I'm having an issue with cleaning up all of the Network.Instantiate objects of disconnected players, from both the client and server side.

    I need to know what the proper calls are to clean up all of the objects from a networked session.

    Scenario:

    Server Initializes - not dedicated, it has a local client
    The local client spawns a Player using Network.Instantiate.
    Player spawns NetVehicle using Network.Instantiate.
    Server Objects
    Player(Clone)
    NetVehicle(Clone)

    Remote Client Connects to the server.
    client gets servers objects added to hierarchy.
    Server's
    Player(Clone)
    NetVehicle(Clone)
    The remote client (this client from the server's perspective) spawns a Player using Network.Instantiate.
    Player spawns a NetVehicle using Network.Instantiate.
    Client's
    Player(Clone)
    NetVehicle(Clone)

    The Remote Client now has 4 network objects, 2 of the servers, 2 of it's own.

    Remote client disconnects.
    What do I have to call on both the Server and the remote client to destroy all of the relevant network instantiated objects?

    On the remote client, I always have the server's Player(clone) and NetVehicle(clone) remaining.
    On the server, the remote client's objects persist.

    On the server, I've tried CloseConnection, Destroy (netView), Destroy ( gameObject), and DestroyPlayerObjects in various combinations and can't seem to find the correct order to get everything cleaned up.
    Do I need to keep a list of all of these network instantiated objects, and cycle through deleting all of them? That seems like what I need to do, but I'm not certain.
     
  2. Kirkules_

    Kirkules_

    Joined:
    Aug 5, 2014
    Posts:
    65
    Figured it out.

    Takeaways:

    Learned about the MonoBehaviour.OnDisconnectedFromServer, which is a huge help.
    Only knew about Network.OnDisconnectedFromServer

    Learned that after you call Network.Disconnect(), all of your networkView objects, local or from a remote client, will receive the MonoBehaviour.OnDisconnectedFromServer, which makes clean-up much easier.

    GameObjects not being removed from the Hierarchy view has to do with not properly calling Destroy on the game object (error: Destroy( this)), so from my networked objects component script, this is correct: Destroy( this.gameObject);

    Final question, is there an event loop diagram for Networking in Unity, like there is for the rest of the MonoBehavior class. http://docs.unity3d.com/Manual/ExecutionOrder.html