Author Topic: Suggestions for improvement  (Read 6476 times)

NachoMan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Suggestions for improvement
« on: December 15, 2014, 10:37:16 AM »
Hello, I've got two Ideas to improve TNet.
The first one is a new Target Class. Ive written my own:
  1. public class NetworkTarget
  2. {
  3.         public TNet.Player player;
  4.         public TNet.Target target;
  5.         TNObject tno;
  6.        
  7.         public NetworkTarget(TNet.Player player, TNObject tno)
  8.         {
  9.                 this.player = player;
  10.                 this.tno = tno;
  11.         }
  12.        
  13.         public NetworkTarget(TNet.Target target, TNObject tno)
  14.         {
  15.                 this.target = target;
  16.                 this.tno = tno;
  17.         }
  18.        
  19.         public void Send(byte rfcID, params object[] objs)
  20.         {
  21.                 if(player != null)
  22.                 {
  23.                         tno.Send(rfcID, player, objs);
  24.                 }
  25.                 else
  26.                 {
  27.                         tno.Send(rfcID, target, objs);
  28.                 }
  29.         }
  30.        
  31.         public void Send(byte rfcID, TNObject tno, params object[] objs)
  32.         {
  33.                 this.tno = tno;
  34.                 Send(rfcID, objs);
  35.         }
  36.  
  37.         public void Send(string rfcName, params object[] objs)
  38.         {
  39.                 if(player != null)
  40.                 {
  41.                         tno.Send(rfcName, player, objs);
  42.                 }
  43.                 else
  44.                 {
  45.                         tno.Send(rfcName, target, objs);
  46.                 }
  47.         }
  48.  
  49.         public void Send(string rfcName, TNObject tno, params object[] objs)
  50.         {
  51.                 this.tno = tno;
  52.                 Send(rfcName, objs);
  53.         }
  54.        
  55.         public void SendQuickly(byte rfcID, params object[] objs)
  56.         {
  57.                 if(player != null)
  58.                 {
  59.                         tno.SendQuickly(rfcID, player, objs);
  60.                 }
  61.                 else
  62.                 {
  63.                         tno.SendQuickly(rfcID, target, objs);
  64.                 }
  65.         }
  66.  
  67.         public void SendQuickly(string rfcID, TNObject tno, params object[] objs)
  68.         {
  69.                 this.tno = tno;
  70.                 SendQuickly(rfcID, objs);
  71.         }
  72.  
  73.         public void SendQuickly(string rfcName, params object[] objs)
  74.         {
  75.                 if(player != null)
  76.                 {
  77.                         tno.SendQuickly(rfcName, player, objs);
  78.                 }
  79.                 else
  80.                 {
  81.                         tno.SendQuickly(rfcName, target, objs);
  82.                 }
  83.         }
  84.        
  85.         public void SendQuickly(byte rfcName, TNObject tno, params object[] objs)
  86.         {
  87.                 this.tno = tno;
  88.                 SendQuickly(rfcName, objs);
  89.         }
  90. }
I hope its explaining itself.
Until I've written this class, i had to define 2 methods for every possable message. Now I'm able to use the same code for different types of targets. If you like this, you can use my code for free.

The other idea is to create players with an abstract factory, so that users can inherit from TN-Player class instead of changing TNet code.

I'm sorry for my bad English. I hope you understood me :D
Greetings from Germany. Please forgive me my bad English. I do my best. :D

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: Suggestions for improvement
« Reply #1 on: December 15, 2014, 11:16:31 AM »
I like it. I'll try it out later today!  :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Suggestions for improvement
« Reply #2 on: December 15, 2014, 01:19:53 PM »
2 methods for every possible message? I don't quite follow... why? When you want to send an RFC to a player (private message), you do just that. When you want to send an RFC to TNet.Target instead, you do something else. In either case they are completely different calls, even if using the same tno.Send function, and with completely different results (one sends a private message, another have a set of possible recipients). So what's the purpose of your class?

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: Suggestions for improvement
« Reply #3 on: December 15, 2014, 01:47:55 PM »
Actually, yeah, I don't get it. At first glance I thought it was something else.

NachoMan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Suggestions for improvement
« Reply #4 on: December 16, 2014, 05:29:17 AM »
I've written a small example.
  1. public class TargetExample : TNBehaviour
  2. {
  3.         int syncInteger = 10;
  4.         float syncTimer = 0f;
  5.         float syncIntervalInSeconds = 5f;
  6.  
  7.         Queue<NetworkTarget> syncRequests = new Queue<NetworkTarget>();
  8.  
  9.         void Update()
  10.         {
  11.                 if(TNManager.isHosting)
  12.                 {
  13.                         syncTimer += Time.deltaTime;
  14.                         if(syncTimer > syncIntervalInSeconds)
  15.                         {
  16.                                 syncTimer -= syncIntervalInSeconds;
  17.                                 SyncTo(new NetworkTarget(Target.Others, tno));
  18.                         }
  19.                         while(syncRequests.Count > 0)
  20.                         {
  21.                                 SyncTo(syncRequests.Dequeue());
  22.                         }
  23.                 }
  24.         }
  25.  
  26.         void OnNetworkPlayerJoin(Player player)
  27.         {
  28.                 if(TNManager.isHosting)
  29.                 {
  30.                         SyncTo(new NetworkTarget(player, tno));
  31.                 }
  32.         }
  33.  
  34.         void SyncTo(NetworkTarget target)
  35.         {
  36.                 target.Send ("OnCompleteSync", tno, syncInteger);
  37.         }
  38.        
  39.         [RFC]
  40.         void OnCompleteSync(int newValue)
  41.         {
  42.                 if(!TNManager.isHosting)
  43.                 {
  44.                         syncInteger = newValue;
  45.                 }
  46.         }
  47.  
  48.         [RFC]
  49.         void RequestSync(Player target)
  50.         {
  51.                 if(TNManager.isHosting)
  52.                 {
  53.                         syncRequests.Enqueue(new NetworkTarget(target, tno));
  54.                 }
  55.         }
  56. }
In this case i could use tno.Send directly, but this means redundant code and as soon as it become a bigger class, i had to change every call. This would be a source of errors if i forget something.

I hope again you understood me. >.<

Edit: What do you say about the abstract factory for player creation?

So what's the purpose of your class?
The purpose is to be able to handle both types of targets in the same way. This helps to reduce redundant code and im able to buffer targets like i did in the example class.
« Last Edit: December 17, 2014, 11:44:16 AM by NachoMan »
Greetings from Germany. Please forgive me my bad English. I do my best. :D

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Suggestions for improvement
« Reply #5 on: December 17, 2014, 03:41:15 PM »
Uh. To sync something with newly joined players, all you need to do is call your RFCs with the "saved" target option. Newly joined players will automatically receive all the saved calls before they finish joining the channel.

There is absolutely no reason to send things to specific players, unless it's a private chat message.