Author Topic: [TNet 2] FAQ & Useful Info  (Read 12711 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
[TNet 2] FAQ & Useful Info
« on: November 07, 2013, 05:23:51 AM »
TNet Class Documentation

Various useful posts
Q: How to set up a remote lobby server where users can register their own servers?

1. Start the TNServer as a lobby server on a remote PC. Note that it has to be a remote PC as this is a lobby server, not a game server. I started it on my Amazon EC2 micro instance, for example.

Windows:
  1. TNServer.exe -name "Remote Lobby" -tcpLobby 5129
OSX & Linux:
  1. mono TNServer.exe -name "Remote Lobby" -tcpLobby 5129
2. Create a new scene, add a game object, add a new script to this game object:
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class Test : TNBehaviour
  5. {
  6.         void Start ()
  7.         {
  8.                 TNServerInstance.serverName = "My Server";
  9.                 TNServerInstance.Start(5127, 0, null, TNServerInstance.Type.Tcp,
  10.                         Tools.ResolveEndPoint("your.lobbyServer.com:5129"));
  11.         }
  12. }
3. Assuming you changed "your.lobbyServer.com" to be your remote server's address, hit Play. You should now see the game server get added to your lobby server and stay on the list.
« Last Edit: February 24, 2016, 01:20:38 AM by ArenMook »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: FAQ & Useful Info
« Reply #1 on: November 07, 2013, 05:30:42 AM »
Q: Is it possible to exchange RFCs without establishing a connection?

Yes, but only to your local area network (LAN).
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class Test : TNBehaviour
  5. {
  6.     // Make sure we're listening for UDP traffic
  7.     void Start () { TNManager.StartUDP(5128); }
  8.  
  9.     // Broadcast to the entire LAN using UDP
  10.     void OnClick () { tno.BroadcastToLAN(5128, "MyFunc", "Hello world!"); }
  11.  
  12.     // Function that gets called when a "MyFunc" broadcast gets received
  13.     [RFC] void MyFunc (string text) { Debug.Log(text); }
  14. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: FAQ & Useful Info
« Reply #2 on: November 07, 2013, 05:30:52 AM »
Q: Is it possible to broadcast a message to everyone connected to the server (ex: global chat)?

Yes.
  1.         void OnClick () { tno.Send("MyFunc", Target.Broadcast, "Hello World!"); }
  2.  
  3.         [RFC] void MyFunc (string text) { Debug.Log(text); }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: FAQ & Useful Info
« Reply #3 on: November 09, 2013, 08:05:47 AM »
Q: How to retrieve a list of channels from the server?

1. First, register a handler for the packet list:
  1. TNManager.client.packetHandlers[(byte)Packet.ResponseChannelList] = OnChannelList;

2. Next, create a function that will handle receiving your channel list:
  1.         void OnChannelList (Packet response, BinaryReader reader, IPEndPoint source)
  2.         {
  3.                 // The first integer represents the number of entries to follow
  4.                 int channels = reader.ReadInt32();
  5.  
  6.                 // Read all incoming channel data
  7.                 for (int i = 0; i < channels; ++i)
  8.                 {
  9.                         int id          = reader.ReadInt32();
  10.                         int players             = reader.ReadUInt16();
  11.                         int limit               = reader.ReadUInt16();
  12.                         bool pass               = reader.ReadBoolean();
  13.                         bool persistent = reader.ReadBoolean();
  14.                         string level    = reader.ReadString();
  15.                         string data             = reader.ReadString();
  16.  
  17.                         // TODO: Do something with this data here
  18.                 }
  19.         }

3. The last step is to actually request the channel list from the server:
  1. TNManager.client.BeginSend(Packet.RequestChannelList);
  2. TNManager.client.EndSend();

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: FAQ & Useful Info
« Reply #4 on: December 10, 2013, 01:24:42 AM »
Q: How to get smooth jitter-less movement?

Structure your moving objects like so:

Quote
Rigidbody, TNObject, any sync script you need (such as TNSyncRigidbody)
- Collider 1
- Collider 2
- GameObject with LagPosition (and possibly LagRotation) on it
-- Renderer

Note that the renderer is not on the same object as the rigidbody.

This will mean that the rigidbody and colliders will always be using the latest data, while the renderer will trail behind your rigidbody (due to the Lag scripts which do smooth interpolation), thus resulting in smooth looking movement.

Using this method I also suggest greatly reducing the frequency of your sync updates. In the Multi-Platform Game Starter Kit I sync'd the rigidbody only 4 times per second, and even that was too much. The trick is to instead immediately sync it after something important happens, such as an explosion -- and also to sync whatever causes it to move as soon as it happens (such as a key being pressed).

So TL;DR: Let your clients do their own movement logic. Correct them only periodically.
« Last Edit: December 10, 2013, 01:31:23 AM by ArenMook »

FrankYan

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 2
  • Posts: 18
    • View Profile
Re: FAQ & Useful Info
« Reply #5 on: July 23, 2014, 09:18:49 AM »
"Let your clients do their own movement logic. Correct them only periodically." which side should send the correction message? (the server or the Remote Player?)

For example I have a remote player In my scene, who should update it's logic ?(the logic server or the who control the remote player in my scene?)
Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: FAQ & Useful Info
« Reply #6 on: July 23, 2014, 09:32:24 PM »
Your channel's Host player will be sending out the "correct" data. In MPGSK I send such updates 4 times per second for example. The rest of the time physics calculations are done based on last sync'd values.

FrankYan

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 2
  • Posts: 18
    • View Profile
Re: FAQ & Useful Info
« Reply #7 on: July 23, 2014, 10:48:28 PM »
”Your channel's Host player“ means?

1、If I have two game clients auto join the independent server's same channel  ,"your channel's host player" mean the independent server not these two client?
2、If I have one game client(A) also host one channel and other game client join in A, "your channel's host player" menu the A?

May be I misunderstanding the relationship between "channel" and "Server", or the "Your channel's Host Player" ,can you give some example?
Thanks!

FrankYan

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 2
  • Posts: 18
    • View Profile
Re: FAQ & Useful Info
« Reply #8 on: July 23, 2014, 10:50:47 PM »
I See TNManager can also create a new channel.... :-[

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: FAQ & Useful Info
« Reply #9 on: July 25, 2014, 12:58:27 AM »
The first person to join a channel is designated as that channel's host. Think "Operator" in IRC. Same concept. Only one player can be a channel's "host" and when the host player leaves, another one is automatically chosen.