TNet: Networking Your First Game


So you are ready to network your first game. Great! You can watch and follow the video, or read the steps below it.

Step by Step Video Tutorial

Writeup (for those that like to read instead!)

  1. Edit -> Project Settings -> Player -> Resolution and Presentation -> Run In Background must be checked.
  2. Add a new game object to your first actual scene (generally your main menu) and attach TNManager to it.
  3. If you have game objects that you are going to be instantiating across the network, add them to TNManager’s Object array.
  4. Add a “Host New Server” button. When clicked, make it call TNServerInstance.Start(12345), where “12345” is the port you want your game to use.
    • You can also specify which file to load and which port to use for UDP as well, if you want.
  5. Add a text field where clients can type in an address of the remote server.
    • If your game’s server is centralized (for example one server hosted on, you can skip this step.
  6. Add a “Join” button that will call TNManager.Connect(address), where “address” comes from your text field.
  7. Create a new script attached to the same game object as your TNManager and add a function to it: “OnNetworkConnect (bool success, string message)“.
  8. Inside the function created in step 6, check — successful? If so, TNManager.JoinChannel(123, “FirstLevelName”), where FirstLevelName is the name of the game scene to load (for example Level1).

Your game clients should now be able to set up a server, connect, and are ready to communicate with each other by sending remote function calls. You will want to replace all instances of GameObject.Instantiate calls with TNManager.Create instead, and only run the game logic on the channel’s host by checking TNManager.isHosting.

For example, if before you had this:

And you wanted to make it so that only the hosting player can set the the ‘mTarget’ value, with everyone else simply seeing the results, you will want to change it to:

Be sure to attach a TNObject script to the game object with this script though!

In essence, you will want to structure your game’s code so that instead of simply setting values (such as mTarget above), you will instead call a remote function to set the value(s). This way the function calls can be properly synchronized across all connected clients.

And that’s pretty much all there is to it! If you have any questions or aren’t sure of how to best turn a piece of code into something that’s network-friendly, feel free to drop by the forums and post a question, or stop by this page for detailed explanation of the framework’s functionality.

 Posted by at 4:16 PM on December 23, 2012