Author Topic: Send RFC Error on manuel added TNObject  (Read 4519 times)

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Send RFC Error on manuel added TNObject
« on: October 16, 2016, 10:07:09 AM »
I have one gameobject which has an manuel added TNObject.
And when i send a RFC the first RFC give's me an Exception.
Every other RFC works perfectly without Errors.

Here is the Error Code:

  1. 1 0 SendChat
  2. Object reference not set to an instance of an object
  3.   at TNet.TNObject.RebuildMethodList () [0x00034] in D:\Unity\CartoonTown\CartoonTown_source\Assets\Extras\TNet\Client\TNObject.cs:637
  4.   at TNet.TNObject.Execute (System.String funcName, System.Object[] parameters) [0x0002a] in D:\Unity\CartoonTown\CartoonTown_source\Assets\Extras\TNet\Client\TNObject.cs:541
  5.   at TNet.TNObject.FindAndExecute (Int32 channelID, UInt32 objID, System.String funcName, System.Object[] parameters) [0x00014] in D:\Unity\CartoonTown\CartoonTown_source\Assets\Extras\TNet\Client\TNObject.cs:603
  6.   at TNet.TNManager.OnForwardedPacket (Int32 channelID, System.IO.BinaryReader reader) [0x00022] in D:\Unity\CartoonTown\CartoonTown_source\Assets\Extras\TNet\Client\TNManager.cs:1809
  7. UnityEngine.Debug:LogError(Object)
  8. TNet.TNManager:OnForwardedPacket(Int32, BinaryReader) (at Assets/Extras/TNet/Client/TNManager.cs:1813)
  9. TNet.GameClient:ProcessPacket(Buffer, IPEndPoint) (at Assets/Extras/TNet/Client/TNGameClient.cs:984)
  10. TNet.GameClient:ProcessPackets() (at Assets/Extras/TNet/Client/TNGameClient.cs:918)
  11. TNet.TNManager:<ProcessPackets>m__9() (at Assets/Extras/TNet/Client/TNManager.cs:528)
  12. TNet.TNManager:Update() (at Assets/Extras/TNet/Client/TNManager.cs:1823)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #1 on: October 17, 2016, 10:30:03 PM »
What's a manually added TNObject? If you mean you're adding a TNObject at run-time, you shouldn't be doing that. TNManager always provides you with a valid TNObject to work with. Even if you pass an empty string as the prefab's name to instantiate, TNManager will call your RCC function with a valid (dummy) game object that will have a TNObject script attached. You can then go.Instantiate() this object to create a clone to work with.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #2 on: October 18, 2016, 01:22:18 AM »
What i mean with "manually added TNObject" is,  i have an GameObject in the Editor with all my UI. And there is also the ingame chat.
So i put on the parent an TNObject on it and my MenuManager script in the Awake Method i have this:

  1. tno.channelID = 50;
  2. tno.uid = 51;

I made it a little bit like the chat example. And i thought to use two Channels.
One Random, where is my "Game"(Pos sync etc) Channel.
And an another Channel what is my Data(Chat, GameManager RFCs, etc) Channel.
« Last Edit: October 18, 2016, 01:36:10 AM by Rexima »

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #3 on: October 18, 2016, 01:58:02 AM »
You shouldn't change those values at runtime. You can add the TNObject in the editor and assign its ID field in the Inspector. This will in turn change the uid as well (the uid is just a getter/setter for the ID). I don't know about the channelID, but I imagine there's some function to change channels instead of modifying the channelID directly.

It's interesting that it only errors out the first time, though. From what I can tell, it shouldn't be able to get past the TNObject.Find call within TNObject.FindAndExecute but it does. Can't see how a null object reference could be thrown in RebuildMethodList() either. Only reference that could be null would be mRFCs and that's instantiated at declaration.

Anyway, change those fields within the Unity editor (not at runtime) and you should be good to go.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #4 on: October 18, 2016, 02:20:07 AM »
If i dont change the values on TNObject at runtime and join the channel "0" i got the same problem.
My first RFC doesnt work, but others does. The problem seems not to be solved.

//EDIT: I tried to Add the TNObject in live ingame and set the uid. The TNObject is now in my "Game" Channel, but if i try to send an RFC i got the same problem, first RFC fails.
Example Code i used:
  1. void OnEnable()
  2.     {
  3.         TNManager.onJoinChannel += OnJoinChannel;
  4.     }
  5.  
  6.     void OnDisable()
  7.     {
  8.         TNManager.onJoinChannel -= OnJoinChannel;
  9.     }
  10.  
  11.     private void OnJoinChannel(int channelID, bool success, string msg)
  12.     {
  13.         if(channelID>0)
  14.         {
  15.             gameObject.AddComponent<TNObject>().uid = 55;
  16.         }
  17.     }

To explain what i want. I want to have an Channel for all non game related RFC's like Chat and so one. How do i manage this?

//EDIT 2: I made a little quick fix.
I changed the RebuildMethodList() Method in TNObject to public.
And after line 636
  1. MonoBehaviour mb = mbs[i];
I added this
  1. if (mb == null)
  2. return;

And on my MenuManager i changed this:
  1. void OnEnable()
  2.     {
  3.         TNManager.onJoinChannel += OnJoinChannel;
  4.     }
  5.  
  6.     void OnDisable()
  7.     {
  8.         TNManager.onJoinChannel -= OnJoinChannel;
  9.     }
  10.  
  11.     private void OnJoinChannel(int channelID, bool success, string msg)
  12.     {
  13.         if (channelID > 0) //DO WE JOINED THE WORLD CHANNEL?
  14.         {            
  15.             tno.RebuildMethodList();
  16.         }
  17.     }

Now, if i send an RFC, after i joined, i got no more Exceptions. Seems to be a fix, but a dirty one  ;D
« Last Edit: October 18, 2016, 02:46:57 AM by Rexima »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #5 on: October 18, 2016, 05:16:40 AM »
Never, ever set UID yourself at run time. TNet assigns it for you, and it should never change.

Either assign the UID at edit time on an object that's a part of the scene that you join, or use TNManager.Instantiate to create an object, which will let TNet assign the UID automatically.

Assigning the UID yourself at run time is plain wrong, and will cause all kinds of issues because the server side won't know anything about this UID.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #6 on: October 18, 2016, 08:56:29 AM »
TNManager.Instantiate will instantiate it on all clients, and all incoming clients. Or not?
So i cannot Instantiate my whole menu ui on all clients, it should stay local.

How can i send Chat messages without instantiating an own chat ui prefab.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #7 on: October 18, 2016, 09:12:47 AM »
Yes, it will instantiate it on all clients -- that's kind of the point.

If you need something to be local, then don't use TNObjects. UI should be local, for example. Why would you instantiate something locally, only to assign a TNObject? Either something is local or it's networked. Pick one.

For chat, simply have your chat be a part of the scene already. In your scene you can place a TNObject there, and give it a static ID. Since this TNObject is a part of the scene that you will load, it will work fine for communication as all clients will have this object, with the same ID. No instantiation necessary.

In W2 (my current game project) I actually split this into 2 parts -- a generic chat object that gets instantiated in a channel by the first player to join it (and this channel is one that everyone joins on connect). This generic object simply has a script that handles chat messages -- sending and receiving. It has subscribable delegates that my UI script (the second part) listens to. So when messages arrive, they arrive on the common game object. The UI is notified of the messages arriving, and displays them.

Nice advantage of this approach is I can switch scenes where UI is, or load an entirely different UI, without having to lose my ability to send or receive chat messages.

It's always a good idea to separate your visualization from your data, whenever possible.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #8 on: October 18, 2016, 11:57:07 AM »
Thank you very much for your explanation.

I put now my ChatUI on the main game scene. And this works now like a charm.
But if i switch the channel, for example if i switch from the main world to an dungeon, the TNO doesnt work anymore.
Must i put in every scene the ChatUI? Can't i transfer the tno to my new channel?

This is my test script to transfer the tnobject to my new channel.
  1.     public int lastChannelID = 0;
  2.     protected override void OnEnable()
  3.     {
  4.         base.OnEnable();
  5.  
  6.         TNManager.onJoinChannel += OnJoinChannel;
  7.     }
  8.  
  9.     void OnDisable()
  10.     {
  11.         TNManager.onJoinChannel -= OnJoinChannel;
  12.     }
  13.  
  14.     private void OnJoinChannel(int channelID, bool success, string msg)
  15.     {
  16.         if(lastChannelID == 0) //configure our first world channel
  17.         {
  18.             Debug.Log("no Transfer");
  19.             lastChannelID = channelID;
  20.         }
  21.         else if (lastChannelID > 0 && lastChannelID != channelID && tno.isMine) //we dont transfer because tno is not mine!
  22.         {
  23.             Debug.Log("Transfer");
  24.             tno.TransferToChannel(channelID);
  25.             lastChannelID = channelID;
  26.            
  27.         }
  28.  
  29.         Debug.Log("tno.isMine: " + tno.isMine); //tno is not mine!
  30.     }
  31.  
  32.     void Awake ()
  33.     {
  34.         DontDestroyOnLoad(gameObject);
  35.         }

If you see in the commands, in "OnJoinChannel" im not the owner of the current channel, so i can't transfer it.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #9 on: October 18, 2016, 12:16:21 PM »
Okay, found the problem. I can transfer the channel when its dynamically created.
I will split up my code like you explained it. Thank you very much.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Send RFC Error on manuel added TNObject
« Reply #10 on: October 19, 2016, 06:32:25 PM »
In W2 I actually stay in several channels at once, including one used by chat. This makes it possible to have a global chat. It's generally a better approach to take -- one channel for chat, another for the game.