Search Unity

Manually Spawn Player Outside of NetworkManager or its Subclass

Discussion in 'Multiplayer' started by Deleted User, Jun 27, 2015.

  1. Deleted User

    Deleted User

    Guest

    I'm modifying an existing game and want to take care of the spawning myself. Should I use NetworkServer.AddPlayerForConnection or ClientScene.AddPlayer? I'd like to put this call right after where I am currently instantiating the player in my game.
     
  2. Deleted User

    Deleted User

    Guest

    NetworkServer.AddPlayerForConnection is called on the server after it receives a message from a client that used ClientScene.AddPlayer.

    ClientScene.AddPlayer sends a request to spawn a character for the client that executes the function. You can not specify what GameObject to use, you only request a Player object.

    NetworkServer.AddPlayerForConnection will need to be executed on the server inside an override function. If you do not override it, the default behaviour will occur. The Network Manager's Player prefab will be used and spawned after you call the client function.

    If you create a derived class off of NetworkManager, you can override OnServerAddPlayer(NetworkConnection conn, short controllerID) to instanciate your own GameObject and then call NetworkServer.AddPlayerForConnection using the parameters of the overrided function. The only way of spawning something "different" than what you put in the Spawning Info section on the Network Manager component is by using a derived class and doing what I mentioned.
     
  3. Deleted User

    Deleted User

    Guest

    Thanks for the information Mr Wolf. I think I'm already fairly clear on how to use the playerPrefab in the NetworkManager. My question is about calling NetworkServer.AddPlayerForConnection manually, as mentioned in the below source.

    "The function AddPlayerForConnection does not have to be called from within OnServerAddPlayer. It could be called asynchronously, such as when a request to another service like a database returns information on what kind of player to created."

    Source: http://docs.unity3d.com/Manual/UNetPlayers.html
     
  4. Deleted User

    Deleted User

    Guest

    You can call that anytime after the initial OnServerAddPlayer function as triggered with information about the player that is requesting a spawned player. It does not have to be called in that function, just anytime after, because you do have to get the connection info and controllerID. Of course you will have to execute the function on the server because that is where the player's information will be sent when he requests a spawn.
     
  5. mephistonight

    mephistonight

    Joined:
    Dec 14, 2016
    Posts:
    75
    @13lueWolf I realise this is a very old thread but here's hoping... I'm completely stuck on this same topic.

    Do you know, from the Networkmanager class, how to differentiate between the local player and a remote player when an object is being spawned on the client? I'm trying to find the localplayer before an object is instantiated for it so I can change the prefab just on that local client, for the local player.
     
  6. Pickelzz

    Pickelzz

    Joined:
    May 5, 2017
    Posts:
    2
    It sounds like what you're looking for is OnStartLocalPlayer

    https://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.OnStartLocalPlayer.html
     
  7. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    If you want to change what prefab is instantiated, you'll need to override the spawn function (as you would do for client-side pooling) but you probably can't determine which player is local from there because there is no network ID yet. There are probably other ways to handle it, but I would spawn an invisible dummy player for each client which can do things like receive a message (could be just a syncvar) that tells it which object is going to be the local player's active player object. Delay spawning, either on the server side or client side until that value is received.