Author Topic: Player gets spawn twice, but only occasionally  (Read 2417 times)

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Player gets spawn twice, but only occasionally
« on: May 13, 2014, 09:07:00 AM »
I have another weird problem with TNet, and this time I'll be sure to flood you with information. :) It might be related to another problem I have, but I wanted to start a new thread "to make it clean."

I have a project with two scenes; Game and Disconnect. The latter is empty, only showing a GUIText "You are now disconnected", while the former has a cube (for ground), a directional light and a "Connection" object.

The "Connection" object has two components attached to it: a TNManager, with the Player prefab attached to it, and a ConnectionController script, which is pretty basic and looks like this:

  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class ConnectionController : MonoBehaviour {
  5.  
  6.         void Start () {
  7.                 TNManager.Connect( "127.0.0.1:5127" );
  8.         }
  9.  
  10.         void OnNetworkConnect ( bool success, string message ) {
  11.                 if ( success ) {
  12.                         TNManager.JoinChannel( 1, "Game" );
  13.                 }
  14.         }
  15.  
  16.         void OnNetworkJoinChannel ( bool success, string message ) {
  17.                 if ( success ) {
  18.                         TNManager.Create( "Prefabs/Player", false );
  19.                 }
  20.         }
  21.  
  22. }
  23.  

Connecting/disconnecting works just fine, and I can mention that my client (workstation) is a MacBook Pro running the latest version of OSX, the latest version of Unity3D, and have the latest version of TNet (1.9.0b).

The server is running Debian 6.0.9, and is not running on 127.0.0.1 as mentioned in the code above, but I want certain things to be a secret. :) But - as mentioned - connecting and disconnecting works just fine (or so it seems, at least.)

The Player prefab has two components attached to it; a TNObject (id = 0) and a PlayerController script, which looks like this:

  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class PlayerController : TNBehaviour {
  5.  
  6.         static public PlayerController instance;
  7.  
  8.         private CharacterController characterController;
  9.         private Camera playerCamera;
  10.  
  11.         void Awake () {
  12.                 Debug.Log ( "PlayerController.Awake()" );
  13.                 if ( TNManager.isThisMyObject ) {
  14.                         instance = this;
  15.  
  16.                         characterController = GetComponent<CharacterController>();
  17.                         playerCamera        = GetComponentInChildren<Camera>();
  18.  
  19.                         Instantiate( Resources.Load("Prefabs/UI Root") );
  20.                 }
  21.     }
  22.  
  23.  

The "UI Root" prefab has nothing attached to it, except the standard DF-GUI stuff. I know I probably should use NGUI, but I had to choose which one to spend $90 on, and I went for DF-GUI. Sorry about that. :)

Now - back to the issue at hand.

Every now and then - most of the time, it seems - when I play the game, the Player is instantiated twice, and they are assigned two different IDs. I've attached a screenshot.

Any ideas why this is happening?

UPDATE: It might be worth mentioning that when this happens, the server says that only one client is connected.

UPDATE #2: I added some debug messages in TNManager.cs, and the second Create() method is being called twice, but I'm 200% sure that Create() is being called only once from "my end" (ie. in the ConnectionController script.)

UPDATE #3: After some more debugging, I was wrong about the 200%. :D The OnNetworkJoinChannel() in my ConnectionController script is actually being called twice, but the OnNetworkConnect() is called only once when that happens. In what cirumstances can that happen?
« Last Edit: May 13, 2014, 09:27:21 AM by toreau »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Player gets spawn twice, but only occasionally
« Reply #1 on: May 13, 2014, 09:44:13 AM »
Alright, starting from the top.

1. You have JoinChannel, giving it a scene to load... and yet your OnNetworkJoinChannel is on the same script. Why not put it on a different script that actually exists as a part of your "Game" scene? You also mentioned you have 2 scenes, yet I'd expect 3 -- starting scene, game scene, and disconnect scene.

2. TNObject ID of 0 is only valid for something that's dynamically instantiated via TNManager.Create / AutoCreate. If you have an object that's already a part of your scene, then it must have a non-zero ID.

3. OnNetworkJoinChannel should only be called once per join, but it gets called on all scripts present in the scene. If you have two scripts with it, both will receive it. When in doubt, check the full call stack. NGUITools.GetHierarchy would be useful here. No idea if DFGUI has something similar.

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Player gets spawn twice, but only occasionally
« Reply #2 on: May 13, 2014, 09:58:37 AM »
Quote
1. You have JoinChannel, giving it a scene to load... and yet your OnNetworkJoinChannel is on the same script. Why not put it on a different script that actually exists as a part of your "Game" scene? You also mentioned you have 2 scenes, yet I'd expect 3 -- starting scene, game scene, and disconnect scene.

I'm actually just interested in the "Game" scene at the moment; as if it's I jump right into the game. I was thinking about creating "Connect" and "Disconnected" scenes later. I assume that's OK?

So all the scripts in question are part of the "Game" scene; the "Disconnected" have no scripts, only the GUIText-thingy I mentioned.

Quote
2. TNObject ID of 0 is only valid for something that's dynamically instantiated via TNManager.Create / AutoCreate. If you have an object that's already a part of your scene, then it must have a non-zero ID.

So that's correct, then; only the Player prefab has a TNObject, and it (the Player prefab) is instantiated over the network (via TNManager.Create().) I have no other objects with a TNObject component attached to it.

Quote
3. OnNetworkJoinChannel should only be called once per join, but it gets called on all scripts present in the scene. If you have two scripts with it, both will receive it. When in doubt, check the full call stack. NGUITools.GetHierarchy would be useful here. No idea if DFGUI has something similar.

OnNetworkJoinChannel() is only in the ConnectionController script, but it still gets called twice sometimes. Keep in mind that it doesn't happen every time I press play. I've also put some debugging into the TNManager script, and the OnNetworkJoinChannel broadcast message is sent only once. :-\

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Player gets spawn twice, but only occasionally
« Reply #3 on: May 13, 2014, 02:07:05 PM »
After I started from scratch and implemented the "three-scene-design" it seems like that has something do with it.