TNet: Documentation

 

Looking for a video tutorial or information on how to get started? Go here instead.

TNManager

This script must be present in the main menu of your game or application (or the first scene all clients are going to load, not counting the splash screen). This script is where you define what objects can be created by the network by adding them to the “Objects” array. The game object containing this script will be automatically marked as “don’t destroy on load”, so it will persist through scene load. Using TNManager, you can:

  • Check to see the current connection status via TNManager.isConnected.
  • Determine if the player is hosting the game (authoritative state) via TNManager.isHosting.
  • Find out if the player is currently in some channel via TNManager.isInChannel.
  • Connect to a remote server via TNManager.Connect and disconnect via TNManager.Disconnect.
  • Join new channels via TNManager.JoinChannel and leave via TNManager.LeaveChannel.
  • Create new game objects via TNManager.Create (instead of using Object.Instantiate)
  • You can pass parameters to object creation function — TNManager.Create — by adding your own custom Remote Creation Call handlers (RCCs)
  • Destroy game objects via TNManager.Destroy (instead of using Object.Destroy)
  • Load new levels via TNManager.LoadLevel (instead of Application.LoadLevel)
  • Allow using UDP for frequent packets via TNManager.StartUDP, and disallow via TNManager.StopUDP.
  • Manually send custom packets via TNManager.BeginSend / EndSend.
  • Add custom packet handlers via TNManager.SetPacketHandler.
  • You can check the current ping by accessing TNManager.ping.
  • You can get the list of players in the current channel by checking TNManager.players (this list only includes players other than yourself)
  • You can get the server time in milliseconds using TNManager.serverTime.

If you have a game where you have AI controlling units, make sure that only the channel’s host (think: IRC channel operator) is actually sending out the updates, and everyone else simply listens in and reacts to them.

For example: unit’s AI script checks — is the player hosting? No? Exit. If yes, continue with the AI logic. Should the unit move somewhere? Calculate where, then send an RFC call to everyone telling everyone that the unit should now move to XYZ. Then everyone gets the same target position set and proceeds to move the unit, even though the “what to do” logic was executed only on one PC.

TNObject

Tasharen Network Object is a script containing a unique identifier making it possible to locate a specific game object on the network. Unity networking calls this type of script a “Network View”. Static objects, such as walls or various props don’t need to have this script. You should only add this script to objects that will need to communicate something across the network, such as position changes, or call remote functions. Using TNObject you can:

  • Check if you’re the one who is responsible for this object (for example you’ve created it) by calling TNObject’s isMine.
  • Call remote functions via TNObject’s Send (TCP) and SendQuickly (UDP) functions.
  • Remove previously called RFCs marked as “saved” by using the Remove function.
  • Broadcast remote function calls to an entire LAN via BroadcastToLAN function. All clients listening to the specified port will receive the packets sent this way, even if they are not currently connected to the server. This is useful if you want a server to announce some information to the local area network.

TNServerInstance and the Lobby Server

You can easily create a local server right from within Unity by using the Server Instance script. You don’t need to add it to your scene.

  • TNServerInstance.Start will start the server, TNServerInstance.Stop will stop it.
  • TNServerInstance.MakePrivate will make your server private, preventing others from joining (or even discovering it).

Want to have a central lobby server where game servers can register themselves, and clients can get a list of active game servers? That’s done by using an appropriate Lobby Server.

By default, TNServerInstance’s Start function can start a lobby server for you if you specify the right set of parameters. For example TNServerInstance.Start(5127, 5128, 5129, “server.dat”) will start a UDP-based lobby server on port 5129. If clients have an appropriate lobby client script running (in this case — TNUdpLobbyClient) with the matching port (5129), they will have access to the list of known servers: TNLobbyClient.knownServers.

How to register a game server with a remotely hosted TCP-based lobby? Like this:

TNServerInstance.Start(5127, 5128, “server.dat”, TNServerInstance.Type.Tcp, Tools.ResolveEndPoint(“some.address.com”, 5129));

Built-In Notifications

All network notifications begin with the OnNetwork prefix and are sent via a broadcast message. You can tap into them by simply creating the appropriate function in a script of your choice.

  • OnNetworkError (string) — Triggered when there is an error.
  • OnNetworkConnect (bool success, string message) — Called with the connection result, on both success and failure. If failed, ‘message’ contains the error message.
  • OnNetworkDisconnect — Self-explanatory. :)
  • OnNetworkJoinChannel (bool success, string message) — Just like connecting to a server, but this time containing the result of an attempt to join a channel. Reason for failure can mean a wrong password or a closed channel, for example — and is explained by the “message” parameter.
  • OnNetworkLeaveChannel — Notification sent when the player leaves the channel for any reason. Also sent just prior to the disconnect notification for consistency’s sake.
  • OnNetworkPlayerJoin (Player) — Happens when another player joins the channel the player is in.
  • OnNetworkPlayerLeave (Player) — The opposite — sent when some player leaves the channel the player is in.
  • OnNetworkPlayerRenamed — Notification of some player changing their name (including the current player).

Useful Tips

  • Packets are generally divided into Request and Response type packets. Requests are sent to the server. Response type packets are sent by the server.
  • You can save a file on the server by creating a RequestSaveFile packet, then load it via RequestLoadFile and delete it via RequestDeleteFile. You can use this to store player avatars or save player inventory, for example. Note that all players will be able to load these files, however (if they know the name of the file).
  • You can get a list of channels on the server (once connected) by sending a RequestChannelList packet to the server. You will get a ResponseChannelList back.
  • When joining a channel you can specify what level will be loaded on success, and what password is required for all future clients in order to join. All subsequent TNManager.LoadLevel calls overwrite the loaded level name specified by the first client.
  • You can close a channel at any time by sending a RequestCloseChannel packet. This will prevent others from joining, and the channel will no longer show up on the list when queried, but all those inside will be able to continue playing until the last person leaves. Once the last person departs, the channel will be destroyed and all of its RFCs will be gone as well.
  • You can add your own custom packets if you like — and there is a video tutorial that explains how (although in most cases you won’t need to).

Getting Started

Ready to jump in and get started now that you have TNet? You can read more here.

 Posted by at 3:12 PM on December 23, 2012