Search Unity

LOBBYMANAGER: Setting the spawn prefab index, prior to OnLobbyServerCreateGamePlayer

Discussion in 'Multiplayer' started by mcbauer, May 5, 2018.

  1. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    524
    I have a found a couple of topics on this, but no one is addressing my particular issue.

    Essentially, I have the system working pretty well, I have updated the Network Lobby from the asset store, to include my buttons for selecting the player character. I set the prefab index from those buttons in this script which is applied to the LobbyManager object:

    Code (CSharp):
    1. public class NetworkLobbyHook : LobbyHook
    2. {
    3.     public short p_index;
    4.  
    5.     public override void OnLobbyServerSceneLoadedForPlayer(NetworkManager manager, GameObject lobbyPlayer, GameObject gamePlayer)
    6.     {
    7.         LobbyManager nlm = NetworkLobbyManager.singleton.GetComponent<LobbyManager> ();
    8.         Debug.Log ("Before LobbyManager playerPrefabIndex = " + nlm.playerPrefabIndex);
    9.         nlm.playerPrefabIndex = p_index;
    10.         Debug.Log ("After LobbyManager playerPrefabIndex = " + nlm.playerPrefabIndex);
    11.  
    12.     }
    13.  
    14.     public void chooseRedBoy ()    {p_index = 2;}
    15.     public void chooseRedGirl (){p_index = 3;}
    16.     public void chooseBlueBoy (){p_index = 4;}
    17.     public void chooseBlueGirl (){p_index = 5;}
    18.     public void chooseYellowBoy (){    p_index = 6;}
    19.     public void chooseYellowGirl (){p_index = 7;}
    20.  
    21. }

    and in LobbyManager:

    Code (CSharp):
    1.  
    2. public short playerPrefabIndex;
    3. public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
    4.         {
    5.             Debug.Log ("CreateGamePLayer = " + playerPrefabIndex);
    6.             GameObject _temp = (GameObject)GameObject.Instantiate(spawnPrefabs[playerPrefabIndex], startPositions[conn.connectionId].position, Quaternion.identity);
    7.             NetworkServer.AddPlayerForConnection(conn, _temp, playerControllerId);
    8.             return _temp;
    9.         }
    My issue is that the setting of my prefab index happens after the lobby has created the game player. My output log looks like this:

    CreateGamePLayer = 2 (default value)
    Before LobbyManager playerPrefabIndex = 2
    After LobbyManager playerPrefabIndex = 7 ( value set from the button)

    How can I set the index prior to the server creating the game player?
     
  2. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    524
    Is there a suggested approach to do this? I'm fine if how I'm going about this is wrong, I'm really in limbo until I can get past this.