Search Unity

UNET Multiple Player Prefabs

Discussion in 'UNet' started by iSleepzZz, Jan 21, 2016.

  1. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Hello everyone!!!
    So I am soooo close to figuring this all out and making it work properly!
    However, it is just not there yet.

    I have this as my custom network manager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class MyNetworkManager : NetworkManager {
    6.  
    7.     [SerializeField]
    8.     GameObject[] characters;
    9.  
    10.     private GameObject selectedChar;
    11.  
    12.     public void SelectCharacter(int i) {
    13.         selectedChar = characters[i];
    14.     }
    15.  
    16.     public override void OnClientConnect(NetworkConnection conn) {
    17.         Debug.LogError("OnClientConnect !");
    18.     }
    19.  
    20.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
    21.         if (selectedChar == null)
    22.             selectedChar = characters[0];
    23.         Debug.LogError("OnServerAddPlayer");
    24.         GameObject p = (GameObject)GameObject.Instantiate(selectedChar, MyNetworkManager.singleton.GetStartPosition().position, MyNetworkManager.singleton.GetStartPosition().rotation);
    25.         NetworkServer.AddPlayerForConnection(conn, p, playerControllerId);
    26.         GetComponent<ChoosePlayer>().enabled = false;
    27.     }
    28.  
    29. }
    30.  

    However, whatever the host (or server+player) picks for his prefab (for example picks "Warrior") then ALL players will be that prefab the host picks (Warrior) since his network manager is on "Warrior". There needs to be some way to send the server "what I want to be" before adding the player in, and I cannot figure out how to do this. Any help please??

    It's almost like I need to add inside the OnClientConnect method something like:
    "My connection is 'conn' and please let me be 'selectedChar' "
    And then somehow receive that message on the server before spawning that connection's player. I don't know if this is possibly, or even how to do it..
     
  2. EDevJogos

    EDevJogos

    Joined:
    Jul 24, 2014
    Posts:
    75
    Just an ideia, don't know if it will work, but you could send a Command ( Message from client to server only ) with the instantiate request. Note that all parameters inside a command function will have there values automatically replicated on the server. So if you call:

    [Command]
    void CmdSpawnPlayer()
    {
    //GameObject player = Set player prefab to clients choice
    //NetworkServer.AddPlayerForConnection(conn, p, playerControllerId);
    }

    It should spawn the right character for everyone.
     
  3. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Tried a few things for this to work.. Could not get it to work because the script derives from NetworkManager which cannot have "Commands".
    Then I tried making another script just to do this command and this will not work because it derives from NetworkBehaviour which is disabled on-load.

    Any other way for anyone to make this all work please?
     
  4. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    206
    Fixed it.
     
    Last edited: Jan 23, 2016
  5. defiantprodigy

    defiantprodigy

    Joined:
    Dec 29, 2015
    Posts:
    1
    What did you end up doing, if you dont mind me asking? I've been struggling with the same issue for multiple prefabs. currently sending Commands and RPCs to change appearance over the network but hasn't proven to be the most efficient way to solve the problem.
     
  6. mr_rayskinner

    mr_rayskinner

    Joined:
    Nov 16, 2013
    Posts:
    37
    Whoops, wrong account. The question above is from me. Really interested to see what you ended up doing :) #iSleepzZz
     
  7. Cristian-Serrano-Martinez

    Cristian-Serrano-Martinez

    Joined:
    Dec 16, 2016
    Posts:
    2
    How?
     
  8. KingTooTall

    KingTooTall

    Joined:
    Oct 17, 2014
    Posts:
    61
    This is HOW I do it with my current project.The comments should be enough to get an understanding. its the MESSAGE part that you are most concerned with as its what is passed from the client machine (and host/client machine) LOCALLY, then the server code takes over after OnserverAddPlayer is done executing. Hope that helps.

    NETWORK MANAGER CODE:
    Code (CSharp):
    1.  
    2. //Called on client when connect
    3. public override void OnClientConnect(NetworkConnection conn)
    4. {
    5. print("THIS IS WHERE A OnClientConnect  START GOES");
    6.     //Get the LOCAL player prefab you want to have for this player. (all this does is lookup my list of player prefabs INDEX and kick back the one your requesting)
    7.     curPlayerPrefabIndex = KnifeFighterGameController.Instance.globalPlayerManager.MP_PirateIndexToSpawn();
    8.     // Create message to set the player
    9.     IntegerMessage msg = new IntegerMessage(curPlayerPrefabIndex);
    10.     // Call Add player and pass the message
    11.     ClientScene.AddPlayer(conn, 0, msg);
    12.    
    13. }
    14.  
    15. public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
    16. {
    17.     print("THIS IS WHERE A OnServerAddPlayer  START GOES");
    18.     // Read client message and receive index
    19.     if(extraMessageReader != null)
    20.     {
    21.         var stream = extraMessageReader.ReadMessage<IntegerMessage>();
    22.         curPlayerPrefabIndex = stream.value;
    23.     }
    24.     //Select the prefab from the spawnable objects list FROM the local machine index that we created with our message earlier
    25.     tmpPrefabGO = KnifeFighterSpawnController.Instance.objectPooler.allAvailablePlayerPrefabs[curPlayerPrefabIndex];
    26.     //Spawn my player GO from the selected prefab
    27.     tempPlayerGO = KnifeFighterSpawnController.Instance.MP_CreatePlayer(tmpPrefabGO);
    28.     //Save the player to a list so we can do stuff to both players later
    29.     KnifeFighterGameController.Instance.globalPlayerManager.MP_AddNetworkedPlayerGO(tempPlayerGO);
    30.     //Init of lists used for health, avatar, manager scripts etc.
    31.     KnifeFighterGameController.Instance.globalPlayerManager.MP_GetPlayerListsSetup();
    32.     // Add player object for connection
    33.     NetworkServer.AddPlayerForConnection(conn, tempPlayerGO, playerControllerId);
    34. }
    35.