Author Topic: Questions on channels, SetHost and SetServerData  (Read 3125 times)

mythikos

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 18
    • View Profile
Questions on channels, SetHost and SetServerData
« on: April 14, 2017, 06:29:12 AM »
First question:
I want to be able to create a channel that is password protected that hosts the multiplayer lobby once a user creates a server. However, for other reasons, I also need the channel's id so I can assign it to my network manager script. So with that in mind, is there a way to get the channels id after creating it? And when should the channel be created? In the OnConnect event? - (cause if so, it doesn't appear to be working.)

Second question:
I was under the impression that once a connection has been established (the OnConnect event) you can start using the TNManager.SetHost and TNManager.SetServerData functionality however this doesn't appear to be the case.

My OnConnect method:
  1.     protected override void OnConnect(bool success, string message)
  2.     {
  3.         Debug.Log("Connected to " + ServerInfo.ExternalAddress + ": " + success + " " + message + " (Player ID #" + TNManager.playerID + ")");
  4.  
  5.         if (ServerInfo.IsHost == true)
  6.         {
  7.             TNManager.CreateChannel(null, false, MaxPlayers, (string.IsNullOrEmpty(ServerInfo.Password)) ? null : ServerInfo.Password, true);
  8.  
  9.             TNManager.SetHost(ChannelId, TNManager.GetPlayer(TNManager.playerID));
  10.             TNManager.SetServerData("serverIsPasswordProtected", (string.IsNullOrEmpty(ServerInfo.Password)) ? false : true);
  11.             TNManager.SetServerData("serverHost", TNManager.GetHost(ChannelId));
  12.         }
  13.  
  14.         if (TNManager.GetServerData<bool>("serverIsPasswordProtected") == true)
  15.             UIPasswordWindow.Show(ChannelId, null);
  16.         else
  17.             TNManager.JoinChannel(ChannelId, null, false, MaxPlayers, null);
  18.     }

but my watch window is giving me:
  1.                 TNManager.channels.size 0       System.Int32
  2.                 TNManager.GetHost(ChannelId)    null    System.Object
  3.                 TNManager.GetServerData("serverIsPasswordProtected")    null    System.Object
  4.                 TNManager.GetServerData("serverHost");  null    System.Object
  5.  

As far as I can tell, the the values are never being stored. So how would someone use these methods?

Side note, the 'ServerInfo' class is a static class used by the network manager to track certain bits of information, including the IsHost flag which is only set to true for the person that creates the server and also has a property for IsGuest for people that join the server. Thank you for any and all help!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Questions on channels, SetHost and SetServerData
« Reply #1 on: April 14, 2017, 05:07:33 PM »
TNManager.channels gives you a list of channels you're currently in, including their IDs. And of course OnJoinChannel notification has the ID as well.

Note that most TNet calls are delayed, not immediate. A call to create / join channel is not going to execute immediately as the packet needs to travel to the server before a response can be received. You get OnJoinChannel after it completes.

Also note that CreateChannel makes a new channel and joins it. You don't need to call JoinChannel at the end.

In short, call CreateChannel then exit. Do the rest in OnJoinChannel.

mythikos

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 18
    • View Profile
Re: Questions on channels, SetHost and SetServerData
« Reply #2 on: April 14, 2017, 11:48:34 PM »
The part where they joined the channel was remnant from before I tried to add a password to the channels. I took your advice and created the channel from the OnConnect event and moved everything else to the OnJoinChannel event, but TNManager is still not setting the host and is still not setting server data. Take my example below, after OnConnect, I tried to get my value from 'testValue', it is null everywhere I try.

  1.     protected override void OnJoinChannel(int channelID, bool success, string msg)
  2.     {
  3.         Debug.Log("Joined channel #" + channelID + " " + success + " " + msg);
  4.  
  5.         // Set this as the network manager's channel id
  6.         NetworkManager.ChannelId = channelID;
  7.  
  8.  
  9.         if (ServerInfo.IsHost == true)
  10.         {
  11.             TNManager.SetHost(channelID, TNManager.GetPlayer(TNManager.playerID));
  12.             TNManager.SetServerData("serverIsPasswordProtected", (string.IsNullOrEmpty(ServerInfo.Password)) ? false : true);
  13.             TNManager.SetServerData("serverHost", TNManager.GetHost(channelID));
  14.             TNManager.SetServerData("lobbyChannel", channelID);
  15.         }
  16.  
  17.         TNManager.GetServerData<string>("testValue"); // This line has a breakpoint
  18.     }
  19.  
  20.     protected override void OnConnect(bool success, string message)
  21.     {
  22.         Debug.Log("Connected to " + ServerInfo.ExternalAddress + ": " + success + " " + message + " (Player ID #" + TNManager.playerID + ")");
  23.  
  24.         TNManager.SetServerData("testValue", "babba gump");
  25.  
  26.         if (ServerInfo.IsHost == true)
  27.             TNManager.CreateChannel(null, false, MaxPlayers, (string.IsNullOrEmpty(ServerInfo.Password)) ? null : ServerInfo.Password, true);
  28.         else if (TNManager.GetServerData<bool>("serverIsPasswordProtected") == true)
  29.             UIPasswordWindow.Show(TNManager.GetServerData<int>("lobbyChannel"), null);
  30.         else
  31.             TNManager.JoinChannel(TNManager.GetServerData<int>("lobbyChannel"), null, false, MaxPlayers, null);
  32.     }

Maybe I am doing it wrong but that is what I took from your reply. Am I using the SetServerData and GetServerData correctly or would you implement this another way. I am trying to avoid sending the password to the lobby server to authenticate OnConnect however it doesnt appear I am finding an elegant way around it

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Questions on channels, SetHost and SetServerData
« Reply #3 on: April 22, 2017, 11:56:19 AM »
SetServerData can only be done by the server's administrator. You need to SetAdmin("password") before you use those calls.

SetHost will only work if you're the channel's current host. Did you check what TNManager.IsHosting(channelID) and TNManager.GetHost(channelID) tell you?

If you are trying to set channel data, use TNManager.SetChannelData instead. The channel's host can do that, you don't need to be its admin.