Search Unity

Copying scene?

Discussion in 'Multiplayer' started by MikeUpchat, Feb 21, 2015.

  1. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    Just about to start the adventure that is creating a multiplayer game and am I just trying to research the basics behind multiplayer stuff. One thing that has me a little confused is recreating a scene when a player joins a game. The game could have half a dozen players already in it and the scene could have a load of new objects added to it, or moved, or the game logic as added components to objects or changed a load of values on existing components so a new player cant just use LoadScene to have the scene recreated, so what is the best method to recreate the current hosts scene for the new player, is it a case of serializing the host scene to say an xml file and then passing that as a string to the new player who then de serializes it or is there some other system I don't know about for handling this kind of thing, all the tutorials I find for doing multiplayer games just do basic stuff with cubes or maybe a mecanim character but not found one that goes into detail on recreating the scene.
     
  2. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    "Join-in-progress" is tricky to support with a lot of game architectures.

    I find the easiest strategy in Unity is to keep a record of critical world events, and ensure that new joining clients replay those events. With Unity networking, you can do this easily using buffered RPCs - it remembers them internally and plays them back on new clients in the correct order. Then when these RPCs are all played out on the new client, critical world state should be synchronized - that is, the right objects should exist, and the client should know who controls each object. From that point, the regular NetworkView serialization should kick in and get the right values into each object. It's a while since I did this with Unity but I think you also need to set some flags to prevent NetworkViews from processing incoming data until you're done with the RPCs, because otherwise you might receive some data for a NetworkView that doesn't even exist yet, causing errors.

    There will often be times when some objects have inconsistencies in their values - e.g. after all the buffered RPCs, maybe there are ten objects all sitting at the same position because that was where they spawned originally, and they haven't received new position data yet. You need to make your game tolerate this kind of thing, which will also help you deal with networking glitches during regular play.

    The alternate strategy you suggested, serializing the entire scene state from another client and replicating it, is also possible, but Unity doesn't help as much with this. Whether it is viable probably depends on your scene complexity.

    This page is useful, though not really about your question:
    http://docs.unity3d.com/Manual/net-NetworkLevelLoad.html

    This page mentions Buffered RPCs:
    http://docs.unity3d.com/Manual/net-RPCDetails.html

    Note that Network.Instantiate is always buffered.