Author Topic: Destroyed after joining  (Read 3889 times)

Zerk

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Destroyed after joining
« on: July 12, 2016, 01:34:44 AM »
Hi,

I'm having an issue with TNet3.  I start a game as a host.  The host creates its player avatar and a bot.  When I join with another instance, the client creates the two players and my player but after a second, destroys my player.  I cant figure out why and have been unable to track down why the joining player gets destroyed.  All I can see is ts occurring here in TNManager:

  1.         /// <summary>
  2.         /// Notification of a network object being destroyed.
  3.         /// </summary>
  4.  
  5.         void OnDestroyObject (int channelID, uint objID)
  6.         {
  7.                 TNObject obj = TNObject.Find(channelID, objID);
  8.  
  9.                 if (obj)
  10.                 {
  11.                         if (obj.onDestroy != null) obj.onDestroy();
  12.                         UnityEngine.Object.Destroy(obj.gameObject);
  13.                 }
  14.         }
  15.  
  16.  

My network code:
  1.         void OnNetworkConnect(bool success, string message)
  2.         {
  3.                 Debug.LogError("OnNetworkConnect" + " " + message + " (Player ID #" + TNManager.playerID + ")");
  4.                 if (success)
  5.                 {
  6.                         TNManager.JoinChannel(channelID, GameManager.Instance.GetMapSceneName());
  7.                 }
  8.                 else
  9.                 {
  10.                         UIManager.Instance.ShowMessageDialog("CONNECTION ERROR", message, "Okay");
  11.                         UIManager.Instance.ResetMainMenu();
  12.                 }
  13.         }
  14.  
  15.         void OnNetworkDisconnect()
  16.         {
  17.                 Debug.LogError("OnNetworkLeaveChannel" + " (Player ID #" + TNManager.playerID + ")");
  18. #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
  19.                 Application.LoadLevel("main");
  20. #else
  21.                 UnityEngine.SceneManagement.SceneManager.LoadScene("main", UnityEngine.SceneManagement.LoadSceneMode.Single);
  22. #endif
  23.                 UIManager.Instance.ResetMainMenu();
  24.         }
  25.  
  26.         void OnNetworkJoinChannel(int channelID, bool success, string message)
  27.         {
  28.                 Debug.LogError("OnNetworkJoinChannel" + " " + message + " (Player ID #" + TNManager.playerID + ")");
  29.                 if (success)
  30.                 {
  31.                         UIManager.Instance.CloseMessageDialog(false);
  32.                         UIManager.Instance.mainMenuCanvas.SetActive(false);
  33.                         GameManager.Instance.SpawnPlayer(channelID);
  34.  
  35.                         //spawn some bots
  36.                         if (TNManager.isHosting)
  37.                                 GameManager.Instance.SpawnPlayer(channelID, true);
  38.                 }
  39.                 else
  40.                 {
  41.                         UIManager.Instance.ShowMessageDialog("CONNECTION ERROR", message, "Okay");
  42.                         UIManager.Instance.ResetMainMenu();
  43.                         TNManager.Disconnect();
  44.                 }
  45.         }
  46.  
  47.         void OnNetworkLeaveChannel(int channelID)
  48.         {
  49.                 TNManager.Disconnect();
  50.         }
  51.  
  52.         public void StartServer(string servername, string playername, int gametype, bool usebots, int connectiontype)
  53.         {
  54.                 Debug.LogError("Starting server...");
  55.                 isTeam = gametype;
  56.  
  57.                 int udpPort = Random.Range(10000, 40000);
  58.                 TNLobbyClient lobby = GetComponent<TNLobbyClient>();
  59.  
  60.                 if (lobby == null)
  61.                 {
  62.                         TNServerInstance.Start(serverTcpPort, udpPort, "server.dat");
  63.                 }
  64.                 else
  65.                 {
  66.                         TNServerInstance.Type type = (lobby is TNUdpLobbyClient) ? TNServerInstance.Type.Udp : TNServerInstance.Type.Tcp;
  67.                         if (TNServerInstance.Start(serverTcpPort, udpPort, lobby.remotePort, "server.dat", type))
  68.                         {
  69.                                 TNManager.player.name = playername;
  70.                                 TNManager.Connect();
  71.                         }
  72.                 }
  73.         }
  74.  
  75.         public void JoinServer(string ip, int port, string playername)
  76.         {
  77.                 TNManager.player.name = playername;
  78.                 if (port < 1)
  79.                         TNManager.Connect(ip);
  80.                 else
  81.                         TNManager.Connect(ip, port);
  82.         }
  83.  

My player spawning code:
  1.         public void SpawnPlayer(int channelid, bool bot = false)
  2.         {
  3.                 channelID = channelid;
  4.                 PlayerManager pm = PlayerManager.Instance;
  5.                 Quaternion rot;
  6.  
  7.                 string pname = bot ? "BOT" : pm.playerList[0].playerName;
  8.  
  9.                 Debug.Log("Spawing Player " + " (Player ID #" + TNManager.playerID + ") " + pname);
  10.  
  11.                 int selac = bot ? Random.Range(0, 6) : pm.playerList[0].aircraftSelected;
  12.                 int flag = bot ? Random.Range(0, DataManager.Instance.flagSprites.Length) : pm.playerList[0].flagIndex;
  13.  
  14.                 Teams team = pm.playerList[0].team;
  15.                 if (bot)
  16.                         team = (team == Teams.FirstTeam) ? Teams.SecondTeam : Teams.FirstTeam;
  17.                 Vector3 pos = GetSpawnPos(team, out rot);
  18.                 if (bot) pos.y += 10f;
  19.                 TNManager.Instantiate(channelID, "CreatePlayerAircraft", "PlayerAircraft", false, pos, rot,
  20.                         pname,
  21.                         (int)team,
  22.                         selac,
  23.                         flag,
  24.                         bot
  25.                         );
  26.                 UIManager.Instance.ingameCanvas.SetActive(true);
  27.                 UIManager.Instance.interfaceUI.SetActive(true);
  28.         }
  29.  
  30.         [RCC]
  31.         static GameObject CreatePlayerAircraft(GameObject prefab, Vector3 pos, Quaternion rot,
  32.                 string playername,
  33.                 int team,
  34.                 int aircraftselected,
  35.                 int flagindex,
  36.                 bool bot
  37.                 )
  38.         {
  39.                 Debug.Log("RCC CreatePlayerAircraft " + playername + " " + ((Teams)team).ToString());
  40.                 // Instantiate the prefab
  41.                 GameObject go = prefab.Instantiate();
  42.  
  43.                 PlayerAircraft pa = go.GetComponent<PlayerAircraft>();
  44.                 pa.playerName = playername;
  45.                 pa.team = (Teams)team;
  46.                 pa.selectedAircraft = aircraftselected;
  47.                 pa.flagIndex = flagindex;
  48.                 pa.isBot = bot;
  49.  
  50.                 pa.Initialize();
  51.  
  52.                 // Set the position and rotation based on the passed values
  53.                 Transform t = go.transform;
  54.                 t.position = pos;
  55.                 t.rotation = rot;
  56.                 return go;
  57.         }
  58.  

My PlayerAircraft initializing code:
  1.         public void Initialize()
  2.         {
  3.                 Debug.Log("INITIALIZE PLAYERAIRCRAFT " + isBot + " " + playerName + " " + tno.isMine);
  4.                 if (tno.isMine && !isBot)
  5.                 {
  6.                         Instance = this;
  7.                         flightView.Target = gameObject;
  8.                         selectedAircraft = PlayerManager.Instance.playerList[0].aircraftSelected;
  9.                         flagIndex = PlayerManager.Instance.playerList[0].flagIndex;
  10.                 }
  11.                 else
  12.                 {
  13.                         Destroy(playerController);
  14.                         Destroy(flightView);
  15.                         if (isBot)
  16.                         {
  17.                                 gameObject.AddComponent<AIController>();
  18.                         }
  19.                 }
  20.          }
  21.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Destroyed after joining
« Reply #1 on: July 13, 2016, 11:07:42 AM »
  1. TNManager.Instantiate(channelID, "CreatePlayerAircraft", "PlayerAircraft", false, ...)
You are passing 'false' for the persistent flag, meaning the object will disappear when the player that created it leaves. It's good for player avatars, but for bots you probably want that to be 'true' instead so they stick around. Also why always create bots on the host? I'd suggest checking to see if you have the bots present before doing it.

Now as for the object being destroyed... In your Initialize() function you are destroying some scripts. I'm not sure what they are but that looks suspicious to me. In general, all scripts should already be present and they should not be destroyed. You can prevent them from running their code by exiting out of Update() functions early if the criteria don't match (such as if (!tno.isMine) return;) -- but you should not be adding or removing components at this time. Any and all RFCs on those components will fail if you add or remove them at runtime.

Zerk

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Destroyed after joining
« Reply #2 on: July 13, 2016, 04:02:05 PM »
Hey AronMook, thanks for the reply!

I tried what you suggested by changing:
  1.                 TNManager.Instantiate(channelID, "CreatePlayerAircraft", "PlayerAircraft", false, pos, rot,
  2.  
to
  1.                 TNManager.Instantiate(channelID, "CreatePlayerAircraft", "PlayerAircraft", bot, pos, rot,
  2.  
and
  1.                         Destroy(playerController);
  2.                         Destroy(flightView);
  3.                         if (isBot)
  4.                         {
  5.                                 gameObject.AddComponent<AIController>();
  6.                         }
  7.  
to
  1.                         playerController.enabled = false;
  2.                         flightView.enabled = false;
  3.                         if (isBot)
  4.                         {
  5.                                 aiController.enabled = true;
  6.                         }
  7.  
with no luck. :(
I turned on DEBUG_PACKETS and I get these messages on the host after running the editor as the host (zerk2) and a build as the client (zerk1):
  1. Sending: RequestCreateObject to zerk2 (101 bytes)
  2. Sending: ResponseID to zerk1 (16 bytes)
  3. Server: RequestJoinChannel (14 bytes)
  4. Sending: ResponseJoiningChannel to zerk1 (273 bytes)
  5. Sending: ResponsePlayerJoined to zerk2 (16 bytes)
  6. Sending: ForwardToOthers to zerk1 (43 bytes)
  7. Client: ResponsePlayerJoined (16 bytes) (TCP)
  8. Sending: ResponseCreateObject to zerk1 (104 bytes)
  9. RCC CreatePlayerAircraft zerk1 FirstTeam
  10. INITIALIZE PLAYERAIRCRAFT False zerk1 False
  11. Sending: ForwardToOthers to zerk2 (43 bytes)
  12. Client: ForwardToOthers (43 bytes) (TCP)
  13. Sending: ResponseDestroyObject to zerk1 (10 bytes)
  14. DESTROY PLAYERAIRCRAFT False zerk1 False
  15.     UnityEngine.Debug:Log(Object)
  16.     PlayerAircraft:OnDestroy() (at Assets/_Scripts/PlayerAircraft.cs:131)
  17.  

FYI
  1.                 Debug.Log("DESTROY PLAYERAIRCRAFT " + isBot + " " + playerName + " " + tno.isMine);
  2.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Destroyed after joining
« Reply #3 on: July 16, 2016, 12:40:07 AM »
What's the stack trace on the "Sending: ResponseDestroyObject" line? I don't see anyone actually sending a RequestDestroyObject packet...

You may need to define SINGLE_THREADED to get a proper stack trace btw.

Zerk

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Destroyed after joining
« Reply #4 on: July 16, 2016, 01:47:58 AM »
Before SINGLE_THREADED
  1. Client: ResponseDestroyObject (10 bytes) (TCP)
  2. UnityEngine.Debug:Log(Object)
  3. TNet.GameClient:ProcessPacket(Buffer, IPEndPoint) (at Assets/TNet/Client/TNGameClient.cs:931)
  4. TNet.GameClient:ProcessPackets() (at Assets/TNet/Client/TNGameClient.cs:911)
  5. TNet.TNManager:<ProcessPackets>m__C() (at Assets/TNet/Client/TNManager.cs:514)
  6. TNet.TNManager:Update() (at Assets/TNet/Client/TNManager.cs:1772)

After:
  1. UnityEngine.Debug:Log(Object)
  2. TNet.GameClient:ProcessPacket(Buffer, IPEndPoint) (at Assets/TNet/Client/TNGameClient.cs:931)
  3. TNet.GameClient:ProcessPackets() (at Assets/TNet/Client/TNGameClient.cs:911)
  4. TNet.TNManager:<ProcessPackets>m__C() (at Assets/TNet/Client/TNManager.cs:514)
  5. TNet.TNManager:Update() (at Assets/TNet/Client/TNManager.cs:1772)

Same...  And yes I'm not sending this packet.  Thus the dilemma.  I put debug logs at the points where I do a DestroySelf and Destroys but they aren't getting called at any point up to this.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Destroyed after joining
« Reply #5 on: July 16, 2016, 01:59:43 AM »
Check the server side: add a Debug.Log to the GameServer.BeginSend function printing the packet type. If you can't figure it out, zip up the project, put it up on dropbox or some other private place and PM me the link so that I can have a look.

Zerk

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Destroyed after joining
« Reply #6 on: July 16, 2016, 12:17:25 PM »
You mean here in TNGameServer, line 727?

  1.         BinaryWriter BeginSend (Packet type)
  2.         {
  3.                 mBuffer = Buffer.Create();
  4.                 BinaryWriter writer = mBuffer.BeginPacket(type);
  5.                 return writer;
  6.         }
  7.  

Zerk

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Destroyed after joining
« Reply #7 on: July 16, 2016, 01:24:31 PM »
Thank you for the help Sir!  I was able to track down the problem.  It was a DestroySelf on a collision.  I have no idea why the collision was being called, but at least I found the problem.