Search Unity

A connection has already been set as ready.

Discussion in 'Multiplayer' started by SaphiBlue, Apr 20, 2016.

  1. SaphiBlue

    SaphiBlue

    Joined:
    Apr 18, 2016
    Posts:
    43
    Hello,

    I have a Problem.

    I have a Cube as PlayerPrefab with a Script "client" and an NetworkIdentity

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class Client : NetworkBehaviour {
    6.  
    7.     public string playerName;
    8.     public string clientId = string.Empty;
    9.     public string prefab;
    10.  
    11.     public GameObject uiPrefab;
    12.     public GameObject mainCamera;
    13.  
    14.     void Start ()
    15.     {
    16.  
    17.         if(isLocalPlayer)
    18.         {
    19.             Tools.gameController.client = this;
    20.             Tools.gameController.menuController.ui.loadingScreenData.slider.value = 1.0f;
    21.             Tools.gameController.menuController.ui.loadingScreen.SetActive(false);
    22.             Tools.UI.locked = false;
    23.             Tools.UI.MenuHide();
    24.             Tools.ClearChildren(Tools.gameController.ui);
    25.             if(mainCamera != null)
    26.             {
    27.                 Tools.SetMainCam(mainCamera);
    28.             }
    29.             if(uiPrefab != null)
    30.             {
    31.                 Tools.SetUI(uiPrefab);
    32.             }
    33.  
    34.             CmdSetClientInfo(PlayerPrefs.GetString("cliendId"), Settings.playerName);
    35.         }
    36.     }
    37.  
    38.     /* Server */
    39.  
    40.     [Command]
    41.     void CmdSetClientInfo(string clientId, string name)
    42.     {
    43.         if(this.clientId == string.Empty)
    44.         {
    45.             PersistentEntity.Client persistentEntity = Tools.gameController.persistence.LoadClientEntity(clientId);
    46.  
    47.             if(persistentEntity.clientId != null)
    48.             {
    49.                 SpawnPersitent(persistentEntity);
    50.             }
    51.         }
    52.         this.clientId = clientId;
    53.         playerName = name;
    54.     }
    55.  
    56.     [Command]
    57.     public void CmdChatMessage(string text)
    58.     {
    59.         ChatMessage chatMessage = new ChatMessage();
    60.         chatMessage.clientId = clientId;
    61.         chatMessage.netId = this.netId;
    62.         chatMessage.playerName = playerName;
    63.         chatMessage.message = text;
    64.  
    65.         RpcGetChatMsg(chatMessage);
    66.     }
    67.  
    68.     [Command]
    69.     public void CmdSpawn(string prefab)
    70.     {
    71.         Spawn(prefab);
    72.     }
    73.  
    74.     [Server]
    75.     public void Spawn(string newPlayerPrefabName)
    76.     {
    77.  
    78.         Destroy(gameObject);
    79.  
    80.         if(newPlayerPrefabName == string.Empty || newPlayerPrefabName.Length < 1)
    81.         {
    82.             newPlayerPrefabName = Tools.gameController.defaultPlayerPrefabName;
    83.         }
    84.  
    85.         GameObject newPlayerPrefab = Tools.gameController.registerPrefabs.FindPlayerPrefab(newPlayerPrefabName);
    86.         if(newPlayerPrefab != null)
    87.         {
    88.             GameObject newPlayer;
    89.             newPlayer = (GameObject)Instantiate(newPlayerPrefab, Vector3.zero, Quaternion.identity);
    90.             NetworkServer.ReplacePlayerForConnection(connectionToClient, newPlayer, 0);
    91.         }
    92.     }
    93.  
    94.     [Server]
    95.     public void Spawn(GameObject newPlayerPrefab)
    96.     {
    97.  
    98.         Destroy(gameObject);
    99.  
    100.         if(newPlayerPrefab != null)
    101.         {
    102.             GameObject newPlayer;
    103.             newPlayer = (GameObject)Instantiate(newPlayerPrefab, Vector3.zero, Quaternion.identity);
    104.             NetworkServer.ReplacePlayerForConnection(connectionToClient, newPlayer, 0);
    105.         }
    106.     }
    107.  
    108.     [Server]
    109.     public void SpawnPersitent(PersistentEntity.Client persistentEntity)
    110.     {
    111.         string newPlayerPrefabName = persistentEntity.prefab;
    112.         Destroy(gameObject);
    113.  
    114.         if(newPlayerPrefabName == string.Empty || newPlayerPrefabName.Length < 1)
    115.         {
    116.             newPlayerPrefabName = Tools.gameController.defaultPlayerPrefabName;
    117.         }
    118.  
    119.         GameObject newPlayerPrefab = Tools.gameController.registerPrefabs.FindPlayerPrefab(newPlayerPrefabName);
    120.         if(newPlayerPrefab != null)
    121.         {
    122.             GameObject newPlayer;
    123.             newPlayer = (GameObject)Instantiate(newPlayerPrefab, persistentEntity.position, persistentEntity.rotation);
    124.             NetworkServer.ReplacePlayerForConnection(connectionToClient, newPlayer, 0);
    125.             newPlayer.GetComponent<PersistentEntity>().SetClientEntity(persistentEntity);
    126.         }
    127.     }
    128.  
    129.  
    130.  
    131.     /* Client */
    132.  
    133.     [ClientRpc]
    134.     public void RpcGetChatMsg(ChatMessage chatMessage)
    135.     {
    136.         Tools.gameController.chat.ClientMsg(chatMessage);
    137.     }
    138.  
    139. }

    The Script by itself does not mutch

    If i want to Join:

    Server View:

    Both Cubes are spawned with the Console Message
    Client View:
    I see both Cubes for one frame and then all objects are gone with the message

    This is a strange behavior because I use the Default Networkmanager (mostly)
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class NetManager : NetworkManager {
    6.  
    7.  
    8.  
    9.     public override void OnServerSceneChanged (string sceneName)
    10.     {
    11.         base.OnServerSceneChanged (sceneName);
    12.         if(sceneName != offlineScene)
    13.         {
    14.             Tools.gameController.persistence.BasePath(sceneName);
    15.             Tools.gameController.persistence.Load();
    16.         }
    17.     }
    18. }
    19.  

    And this Code for the Matchmaking (the Script is Attached to the Same Gameobject as the NetworkManager):
    (I cut something for readability)
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Networking.Types;
    4. using UnityEngine.Networking.Match;
    5. using UnityEngine.UI;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8.  
    9. public class MenuController : MonoBehaviour
    10. {
    11.  
    12.     void Start()
    13.     {
    14.         serverSettings.inputMap.options.Clear();
    15.         serverSettings.inputMap.AddOptions(gameSettings.maps);
    16.  
    17.         NetworkManager.singleton.StartMatchMaker();
    18.     }
    19.  
    20.  
    21.     public void GetHostList()
    22.     {
    23.  
    24.         if(NetworkManager.singleton.matchMaker == null)
    25.         {
    26.             NetworkManager.singleton.StartMatchMaker();
    27.         }
    28.  
    29.         ListMatchRequest request = new ListMatchRequest();
    30.         request.includePasswordMatches = true;
    31.         request.pageSize = 10;
    32.         request.nameFilter = string.Empty;
    33.         NetworkManager.singleton.matchMaker.ListMatches(request, OnMatchList);
    34.  
    35.     }
    36.  
    37.     public void JoinDialog(MatchDesc match)
    38.     {
    39.    
    40.         ui.joinDialog.ui.SetActive(true);
    41.         ui.joinDialog.ui.GetComponent<RectTransform>().SetAsLastSibling();
    42.         ui.joinDialog.serverName.text = match.name;
    43.  
    44.         gameSettings.matchToJoin = match;
    45.     }
    46.  
    47.     public void JoinMatch()
    48.     {
    49.         if(gameSettings.matchToJoin == null)
    50.         {
    51.             return;
    52.         }
    53.  
    54.         JoinMatchRequest request = new JoinMatchRequest();
    55.         request.networkId = gameSettings.matchToJoin.networkId;
    56.         request.password = ui.joinDialog.passwordInput.text;
    57.  
    58.         NetworkManager.singleton.matchMaker.JoinMatch(request, OnJoinMatch);
    59.  
    60.         ui.loadingScreen.SetActive(true);
    61.         ui.loadingScreenData.text.text = Lang.GetString("Loading");
    62.         ui.loadingScreenData.slider.value = 0.0f;
    63.         ui.joinDialog.ui.SetActive(false);
    64.         ui.hostGameDialog.SetActive(false);
    65.         Tools.UI.MenuHide();
    66.     }
    67.  
    68.     void OnJoinMatch(JoinMatchResponse res)
    69.     {
    70.         if(res.success)
    71.         {
    72.             ui.loadingScreenData.text.text = Lang.GetString("Loading");
    73.             ui.loadingScreenData.slider.value = 0.1f;
    74.  
    75.             Utility.SetAccessTokenForNetwork(res.networkId, new NetworkAccessToken(res.accessTokenString));
    76.             NetworkManager.singleton.StartClient(new MatchInfo(res));
    77.         }
    78.         else
    79.         {
    80.             ui.loadingScreen.SetActive(false);
    81.             Tools.UI.MenuVisible();
    82.         }
    83.     }
    84.  
    85.     void OnMatchList(ListMatchResponse res)
    86.     {
    87.         if(res.success)
    88.         {
    89.             UIList<MatchDesc, ServerListItem> serverList = new UIList<MatchDesc, ServerListItem>(gameSettings.serverListUI, gameSettings.serverListUIItemPrefab, res.matches);
    90.             serverList.UpdateList();
    91.         }
    92.     }
    93.  
    94.     public void StartHost()
    95.     {
    96.         if(NetworkManager.singleton.matchMaker == null)
    97.         {
    98.             NetworkManager.singleton.StartMatchMaker();
    99.         }
    100.  
    101.         CreateMatchRequest request = new CreateMatchRequest();
    102.  
    103.         request.name = serverSettings.inputName.text;
    104.         request.size = 4;
    105.         request.password = serverSettings.inputPasswort.text;
    106.         request.advertise = true;
    107.  
    108.         Tools.gameName = request.name;
    109.  
    110.         ui.loadingScreen.SetActive(true);
    111.         ui.loadingScreenData.text.text = Lang.GetString("Loading");
    112.         ui.loadingScreenData.slider.value = 0.0f;
    113.  
    114.         ui.joinDialog.ui.SetActive(false);
    115.         ui.hostGameDialog.SetActive(false);
    116.         Tools.UI.MenuHide();
    117.         NetworkManager.singleton.onlineScene = serverSettings.inputMap.options[serverSettings.inputMap.value].text;
    118.  
    119.         NetworkManager.singleton.matchMaker.CreateMatch(request, OnMatchCreate);
    120.  
    121.     }
    122.  
    123.     void OnMatchCreate(CreateMatchResponse matchResponse)
    124.     {
    125.         if(matchResponse.success)
    126.         {
    127.             MatchInfo info =  new MatchInfo(matchResponse);
    128.  
    129.             ui.loadingScreenData.text.text = Lang.GetString("Loading");
    130.             ui.loadingScreenData.slider.value = 0.1f;
    131.  
    132.             Utility.SetAccessTokenForNetwork(matchResponse.networkId, new NetworkAccessToken(matchResponse.accessTokenString));
    133.  
    134.             NetworkManager.singleton.StartHost(info);
    135.  
    136.         }
    137.         else
    138.         {
    139.             ui.loadingScreen.SetActive(false);
    140.             Tools.UI.MenuVisible();
    141.         }
    142.     }
    143.  
    144.  
    145.     }
    146.  
    147.  
    148.  
    149. }
    150.  

    Even more strange:

    If I use the Default "NetworkManagerHUD Script", everything works fine....
    what do I do different?
     
  2. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    First thing to check is if you have "Auto Create Player" set in your Network Manager. Make sure its unchecked.
    This error is caused by the server trying to spawn a new player object for a client when they already have one. What seems to be happening is: You are calling the spawn player code which triggers NetworkServer.ReplacePlayerForConnection() on the server which starts the process of spawning your character. Meanwhile on the client (because of Auto Create Player) the ClientScene.AddPlayer() code is triggered because you don't have a player object yet, this code makes a call to the server to add a player object. On the server it gives you that error because you already have a player object(the one you spawned with ReplacePlayer())
     
  3. SaphiBlue

    SaphiBlue

    Joined:
    Apr 18, 2016
    Posts:
    43
    Hello, thank you for your response.

    now I use in me Network Manager:
    Code (CSharp):
    1.     public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId)
    2.     {
    3.         GameObject player;
    4.  
    5.         player = (GameObject)Object.Instantiate(this.playerPrefab, Vector3.zero, Quaternion.identity);
    6.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    7.  
    8.     }
    9.  
    10.     public override void OnClientSceneChanged(NetworkConnection conn)
    11.     {
    12.         ClientScene.AddPlayer(conn, 0);
    13.     }
    But there is still an issue: For the server is erverthing fine, but the client does not see any object or the server player object, expect for the first frame.
     
  4. SaphiBlue

    SaphiBlue

    Joined:
    Apr 18, 2016
    Posts:
    43
    hello, sorry for double post but i Solved the problem, here is the solution:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class NetManager : NetworkManager {
    6.  
    7.  
    8.     public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId)
    9.     {
    10.  
    11.         GameObject player;
    12.         SpawnData spawnData = Tools.gameController.GetSpawnData();
    13.  
    14.         player = (GameObject)Object.Instantiate(this.playerPrefab, Vector3.zero, Quaternion.identity);
    15.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    16.  
    17.     }
    18.  
    19.  
    20.  
    21.     public override void OnClientSceneChanged(NetworkConnection conn)
    22.     {
    23.         ClientScene.AddPlayer(conn, 0);
    24.     }
    25.  
    26.     public override void OnClientConnect(NetworkConnection conn)
    27.     {
    28.         //base.OnClientConnect(conn);
    29.     }
    30.  
    31.  
    32. }
    33.  
    Just overriding the OnClientConnect and disable erverthing
     
    Sophistic_WaTer and ThaiCat like this.