Search Unity

Confused by new features.

Discussion in 'Multiplayer' started by Nemox, Jun 11, 2015.

  1. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    'ey folks, I'm having trouble understanding the intention and flow behind the new networking features, primarily with regards to managing data on player objects.

    What I currently understand (and please correct me if I'm mistaken) is that a lobby player takes up some kind of slot on the server and also the client, and then during gameplay an associated (arbitrary) player object is spawned at some location on the game map. I'm assuming this is intended to be, say, a character which is to be controlled by the lobby player?

    The confusing part is how (or whether) these objects are somehow linked together or not, and how to go about applying input. In my mind, I would list some variables on the lobby player and change them based on input, and then the character would then read the input from the lobby player (so that it doesn't get mixed up with which player on the couch it's supposed to be getting input from).

    Is this anywhere close to the intended workflow, or should I try something completely different?
     
  2. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    What seems to be the intended workflow is that you have a player control script of some sort on the player, something like this:
    Code (csharp):
    1.  
    2. [Client]
    3. void Update(){
    4.     // This keeps you from trying to send commands for other players or the server.
    5.     // Doing that generates an error message.
    6.     if (!isLocalPlayer)
    7.         return;
    8.     if (HasMoveInput())
    9.         CmdMove(GetMoveInput());
    10.     if (HasFireCommand())
    11.         CmdFire(GetFireCommand())
    12. }
    13.  
    14. [Server]
    15. [Command]
    16. void CmdMove(MoveInput input) {
    17.    // ...Handle move input (move the player object).
    18. }
    19.  
    20. [Server]
    21. [Command]
    22. void CmdFire(FireInput input) {
    23.    // ...Handle fire input (check that the weapon is ready, and activate it - shoot, swing, throw, etc.).
    24. }
    25.  
    So in short, the client generates and sends commands, and the server processes them. You'd then use NetworkIdentity, NetworkTransform, and [SyncVar] to keep the objects synchronized.
     
    Nemox likes this.
  3. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    Hm... perhaps I should refactor my ultimate question: How are the Lobby Player and the Game Player associated? Are they linked together in some manner, and can the Lobby Player be referenced after the Game Player is spawned?
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    There is a virtual function on NetworkLobbyManager that is called afer the gamePlay player is created

    Code (CSharp):
    1.         // for users to apply settings from their lobby player object to their in-game player object
    2.         public virtual bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
    3.         {
    4.             return true;
    5.         }
    6.  
     
    Nemox likes this.
  5. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    That, I have no idea. I'm still learning UNet myself. Sorry.
     
  6. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    You are a fantastic person.