Tasharen Entertainment Forum

Support => TNet 3 Support => Topic started by: Ludopathic on January 05, 2017, 08:58:46 AM

Title: OnConnection not firing
Post by: Ludopathic on January 05, 2017, 08:58:46 AM
Hi there,
totally new to tnet and just bought it, am playing around with the script to autojoin and modify it so that when the player logs in using the loginsystem (LoginPro + Mysql) the client retrieves the user ID and sets the channel to that number. The point of this is to create a temporary empty room for the user to be in which is his personal "party" channel.

I have manage to get the server to retrieve the user ID and set the channel to it, however it seems that OnConnect is no longer firing and setting the channel at the end - I can't really figure out why. What I've done so far is to call the ID retrieval code in "Awake" and manually call the connection method from the end of the retrieval method to start the connection.

  1. {
  2.     public class Menu_Autojoin : MonoBehaviour
  3.     {
  4.  
  5.         static public Menu_Autojoin instance;
  6.  
  7.         public string serverAddress = "127.0.0.1";
  8.         public int serverPort = 5127;
  9.         public int channelID = 1;
  10.         public bool persistent = false;
  11.         public bool allowUDP = true;
  12.         public bool connectOnStart = true;
  13.  
  14.  
  15.         void Awake()
  16.         {
  17.             LoginPro.Manager.ExecuteOnServer("GetUserID", UserIDRetrieval_Success, UserIDRetrieval_Error, null);
  18.  
  19.             if (instance == null)
  20.             {
  21.                 instance = this;
  22.                 DontDestroyOnLoad(gameObject);
  23.             }
  24.         }
  25.  
  26.         void OnEnable()
  27.         {
  28.             TNManager.onConnect += OnConnect;
  29.        
  30.         }
  31.  
  32.         void OnDisable()
  33.         {
  34.             TNManager.onConnect -= OnConnect;
  35.        
  36.         }
  37.  
  38.         void Start() { if (connectOnStart) Connect(); }
  39.         public void Connect()
  40.         {
  41.             // We don't want mobile devices to dim their screen and go to sleep while the app is running
  42.             Screen.sleepTimeout = SleepTimeout.NeverSleep;
  43.  
  44.             // Connect to the remote server
  45.             Debug.Log("Connection starting");
  46.             TNManager.Connect(serverAddress, serverPort);
  47.            
  48.            
  49.  
  50.  
  51.         }
  52.        
  53.         //retrieve user ID and set the channel number to this. This is to create user specific rooms on login (empty party channels with only the user)
  54.         public void UserIDRetrieval_Success(string[] datas)
  55.         {
  56.             Debug.Log("User ID Successfully retrieved" + datas[0]);
  57.  
  58.             channelID = int.Parse(datas[0]);
  59.             Debug.Log("Channel ID has been converted to int and channel ID has been set: " + channelID);
  60.             Debug.Log("sending to connection manager");
  61.             Start();
  62.  
  63.         }
  64.  
  65.         public void UserIDRetrieval_Error(string errorMessage)
  66.         {
  67.             Debug.LogError(errorMessage);
  68.  
  69.         }
  70.  
  71.         void OnConnect(bool result, string message)
  72.         {
  73.             Debug.Log("We made it!");
  74.  
  75.             if (result)
  76.             {
  77.                 // Make it possible to use UDP using a random port
  78.                 if (allowUDP) TNManager.StartUDP(Random.Range(10000, 50000));
  79.                 TNManager.JoinChannel(channelID, null, persistent, 10000, null);
  80.                 Debug.Log("Successfully connected to server with channel ID " + channelID);
  81.             }
  82.             else Debug.LogError(message);
  83.         }
  84.        
  85.  
  86.  
  87.     }
  88. }

Any ideas?
Title: Re: OnConnection not firing
Post by: ArenMook on January 05, 2017, 10:11:19 AM
So what's happening exactly? You connect, but don't get the OnConnect notification? How do you know that you're actually connected then?

Awake() is a bad place to do anything except initializing local variables and caching script references of scripts that already exist. OnEnable / Disable is also not a good place to subscribe/unsubscribe in this case. You have an instance check in Awake() -- so your subscription should be in the same "if" block to ensure that it happens only once. Furthermore, you need to destroy the game object if an instance is already exists -- and you aren't doing that. So in effect, what you have isn't really a singleton at all.

Remove your OnEnable/OnDisable. Move subscription to be within the same "if" statement as your singleton reference setting / DontDestroy call, and unsubscribe in OnDestroy -- and only if the singleton reference matches 'this'. Also I'd move your LoginPro call to be in Start() instead of Awake.
Title: Re: OnConnection not firing
Post by: Ludopathic on January 05, 2017, 10:59:20 AM
Just to clarify, the majority of the connection code in the script above is taken from the included Autojoin script included with tnet. I also have a game object in my hierarchy with TNManager attached to it as well as this script.

In the TNServer console I was getting "Connected" and "Disconnected" messages with local IP address and random port so I assumed it was connecting. Now that I move the LoginPro call to the start, no connection messages are being shown and neither any of the debug messages. I am currently now getting warning messages "Your game object is missing a TNObject script needed for network communication". This is evidently because the instance is never being created. Sorry to test your patience, I made the changes you recommended but I can't figure out how to start the instance :/ Do I need to call this script from something in my scene?

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TNet;
  4. using UnityEngine;
  5. using UnityTools = TNet.UnityTools;
  6.  
  7. namespace TNet
  8. {
  9.     public class Menu_Autojoin : MonoBehaviour
  10.     {
  11.  
  12.         static public Menu_Autojoin instance;
  13.  
  14.         public string serverAddress = "127.0.0.1";
  15.         public int serverPort = 5127;
  16.         public int channelID = 1;
  17.         public bool persistent = false;
  18.         public bool allowUDP = true;
  19.         public bool connectOnStart = true;
  20.         public string disconnectLevel;
  21.  
  22.         void Awake()
  23.         {
  24.            
  25.  
  26.             if (instance == null)
  27.             {
  28.                 instance = this;
  29.                 TNManager.onConnect += OnConnect;
  30.                 TNManager.onDisconnect += OnDisconnect;
  31.                 DontDestroyOnLoad(gameObject);
  32.             }
  33.         }
  34.  
  35.          void Start() {
  36.            
  37.             if (connectOnStart) Connect();
  38.            LoginPro.Manager.ExecuteOnServer("GetUserID", UserIDRetrieval_Success, UserIDRetrieval_Error, null);
  39.         }
  40.  
  41.  
  42.  
  43.         public void Connect()
  44.         {
  45.             // We don't want mobile devices to dim their screen and go to sleep while the app is running
  46.             Screen.sleepTimeout = SleepTimeout.NeverSleep;
  47.  
  48.             // Connect to the remote server
  49.             Debug.Log("Connection starting");
  50.             TNManager.Connect(serverAddress, serverPort);
  51.            
  52.              }
  53.  
  54.         void OnConnect(bool result, string message)
  55.         {
  56.             Debug.Log("We made it!");
  57.  
  58.             if (result)
  59.             {
  60.                 // Make it possible to use UDP using a random port
  61.                 if (allowUDP) TNManager.StartUDP(Random.Range(10000, 50000));
  62.                 TNManager.JoinChannel(channelID, null, persistent, 10000, null);
  63.                 Debug.Log("Successfully connected to server with channel ID " + channelID);
  64.             }
  65.             else Debug.LogError(message);
  66.         }
  67.  
  68.         void OnDisconnect()
  69.         {
  70. #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
  71.                 if (!string.IsNullOrEmpty(disconnectLevel) && Application.loadedLevelName != disconnectLevel)
  72.                         Application.LoadLevel(disconnectLevel);
  73. #else
  74.             if (!string.IsNullOrEmpty(disconnectLevel) &&
  75.                 UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != disconnectLevel)
  76.                 UnityEngine.SceneManagement.SceneManager.LoadScene(disconnectLevel);
  77. #endif
  78.         }
  79.  
  80.  
  81.         private void OnDestroy()
  82.         {
  83.             if (instance == this)
  84.             {
  85.                 TNManager.onConnect -= OnConnect;
  86.                 TNManager.onDisconnect -= OnDisconnect;
  87.  
  88.             }
  89.             TNManager.onConnect -= OnConnect;
  90.             TNManager.onDisconnect -= OnDisconnect;
  91.  
  92.         }
  93.  
  94.         //retrieve user ID and set the channel number to this. This is to create user specific rooms on login (empty party channels with only the user)
  95.         public void UserIDRetrieval_Success(string[] datas)
  96.         {
  97.             Debug.Log("User ID Successfully retrieved" + datas[0]);
  98.  
  99.             channelID = int.Parse(datas[0]);
  100.             Debug.Log("Channel ID has been converted to int and channel ID has been set: " + channelID);
  101.             Debug.Log("sending to connection manager");
  102.            
  103.  
  104.         }
  105.  
  106.         public void UserIDRetrieval_Error(string errorMessage)
  107.         {
  108.             Debug.LogError(errorMessage);
  109.  
  110.         }
  111.  
  112.      
  113.        
  114.  
  115.  
  116.     }
  117. }

Title: Re: OnConnection not firing
Post by: Ludopathic on January 05, 2017, 11:37:01 AM
UPDATE - Hi, I solved it. I was attaching the script to the same gameobject as TNmanager. Created Autojoin's own empty object and it works fine- ish now. I am however getting this weird error:

"Scene '1. RFCs' couldn't be loaded because it has not been added to the build setting or the AssetBundle has not been loaded."

NullReferenceException: Object reference not set to an instance of an object. Tnet.TNManager+<LoadLevelCoroutine>.....

Title: Re: OnConnection not firing
Post by: ArenMook on January 08, 2017, 02:49:09 PM
You need to add that scene to your build list.