Author Topic: Channel data at creation  (Read 6751 times)

Noi14

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 57
    • View Profile
Channel data at creation
« on: March 04, 2014, 10:49:07 AM »
Hi,
I'm making a part of code in order to create channel and lobby where there is a list of existing channel.
My problem is that I can't to set data of channel using TNManager.channelData at the moment to create channel it self.


CREATE:
  1. TNManager.JoinChannel(channelIDFirstAvailable, "myscene", false, 999, "");
  2. TNManager.channelData = json_stanza;

LOBBY:
  1. void OnChannelList (Packet response, BinaryReader reader, IPEndPoint source)
  2. {
  3.         // Read all incoming channel data
  4.         for (int i = 0; i < channels; ++i)
  5.         {
  6.                         ChannelEntry _newChannelEntry = new ChannelEntry();
  7.  
  8.                         _newChannelEntry.id             = reader.ReadInt32();
  9.                         _newChannelEntry.players        = reader.ReadUInt16();
  10.                         _newChannelEntry.limit          = reader.ReadUInt16();
  11.                         _newChannelEntry.password       = reader.ReadBoolean();
  12.                         _newChannelEntry.persistent     = reader.ReadBoolean();
  13.                         _newChannelEntry.level          = reader.ReadString();
  14.                         _newChannelEntry.data           = reader.ReadString();
  15.  
  16.                         Debug.Log("_newChannelEntry.data === "+_newChannelEntry.data); // Empty data
  17.         }
  18. }

If I'm doing in this mode:
  1. TNManager.JoinChannel(channelIDFirstAvailable, "myscene", false, 999, "");
  2. TNManager.client.BeginSend(Packet.RequestSetChannelData).Write(json_stanza);
  3. TNManager.client.EndSend();

It will work perfectly but I don't understand the reason why I don't have to use TNManager.channelData

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Channel data at creation
« Reply #1 on: March 05, 2014, 08:38:54 AM »
TNManager.JoinChannel only starts the join operation.

You need to wait until you receive OnNetworkJoinChannel before doing anything channel-related.

Noi14

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: Channel data at creation
« Reply #2 on: March 05, 2014, 09:08:39 AM »
I try to add:
  1. void OnNetworkJoinChannel(bool success, string message)
  2.         {
  3.                 Debug.Log ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  4.                 if (success)
  5.                 {
  6.                         TNManager.channelData = json_stanza;
  7.                         Debug.Log("TNManager.channelData"+TNManager.channelData);
  8.                 }
  9.         }
  10.  

But It never been launched.
Full script is this:
  1. public class MySubmit : TNBehaviour {
  2.         private JsonData datijson;
  3.         private string json_stanza;
  4.  
  5.         public void SubmitExec()
  6.         {
  7.                 // Channel structure
  8.                 ChannelData stanza = new ChannelData();
  9.                 stanza.Name = nameInput.value;
  10.                 json_stanza = JsonMapper.ToJson(stanza);
  11.  
  12.  
  13.                 // Create Channel
  14.                 TNManager.JoinChannel(channelsupdater.channelFirstAvailable, "soccer", false, 999, "");
  15.         }
  16.         void OnNetworkJoinChannel(bool success, string message)
  17.         {
  18.                 Debug.Log ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  19.                 if (success)
  20.                 {
  21.                         TNManager.channelData = json_stanza;
  22.                         Debug.Log("TNManager.channelData"+TNManager.channelData);
  23.                 }
  24.         }
  25. }
  26.  
I also added TNObject in the enter button of NGUI, and it still has problems.

GantZ_Yaka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 21
    • View Profile
Re: Channel data at creation
« Reply #3 on: March 06, 2014, 12:28:54 AM »
I decided the same problem using Coroutine. Something like this:
  1. void JoinClient()
  2. {
  3. TNManager.JoinChannel(myChid, null);                             StartCoroutine(Coroutine());                                            
  4. }      
  5.        
  6. public IEnumerator Coroutine()
  7. {
  8.         if(!TNManager.isInChannel)
  9.         {
  10.                 yield return new WaitForSeconds(0.5f);
  11.                 StartCoroutine(Coroutine());
  12.         }
  13.         else
  14.         {                                      
  15.              tno.Send(....);                                                                                   
  16.         }
  17. }

Noi14

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: Channel data at creation
« Reply #4 on: March 06, 2014, 05:23:57 AM »
Thank you but I think it isn't correct as system. It's a workaround.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Channel data at creation
« Reply #5 on: March 06, 2014, 12:28:40 PM »
Why are you calling TNManager.JoinChannel from an object that has a TNObject attached? TNManager.JoinChannel should be called from somewhere, but it doesn't need to be something with a TNObject. TNObjects are for network sync, and since you are not yet in a channel, there is nothing to sync. The correct order of things is to call TNManager.JoinChannel from either your Start() function or as an On Click notification, and it should not be in a script derived from TNBehaviour.

When you join a channel, OnNetworkJoinChannel is always called -- on all scripts. Have a look at TNAutoJoin script to see what it does inside. It's a script that makes it possible to automatically connect to a remote server and join a channel as soon as it starts.

Noi14

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: Channel data at creation
« Reply #6 on: March 06, 2014, 12:38:08 PM »
I delete TNObject and edit script so:
  1. public class MySubmit : MonoBehaviour {
  2.     private JsonData datijson;
  3.     private string json_stanza;
  4.  
  5.     public void SubmitExec()
  6.     {
  7.         // Channel structure
  8.         ChannelData stanza = new ChannelData();
  9.         stanza.Name = nameInput.value;
  10.         json_stanza = JsonMapper.ToJson(stanza);
  11.  
  12.  
  13.         // Create Channel
  14.         TNManager.JoinChannel(channelsupdater.channelFirstAvailable, "soccer", false, 999, "");
  15.     }
  16.     void OnNetworkJoinChannel(bool success, string message)
  17.     {
  18.         Debug.Log ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  19.         if (success)
  20.         {
  21.             TNManager.channelData = json_stanza;
  22.             Debug.Log("TNManager.channelData"+TNManager.channelData);
  23.         }
  24.     }
  25. }

But the function OnNetworkJoinChannel it never been called.


I use NGUI to show a field to create a new channel and assign at each channel a name. So I need that when I press the button it create the channel and set the field "data".

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Channel data at creation
« Reply #7 on: March 07, 2014, 11:00:14 AM »
Unless you mark this script as persistent via DontDestroyOnLoad, this script will be gone as soon as your "soccer" level starts loading, so there won't be anything left to receive your OnNetworkJoinChannel script.

Have a look at http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html