Networking Guide

This ia brief guide of how to implement networking in a game.

Network Variables

In order to send and receive any data over the network you must first define some network variables. This can be done like this...

Network Global int networkInt

This defines an integer that is shared over the network. When the host computer changes the value of this variable, it is updated on all the client machines that are connected to the game.

If a client wants to change the value of the variable then it must send a message to the host telling it that it has done it.

Set Net Data networkInt,2

This asks the host to change the value of networkInt to the value 2. Once the host has done this, it will then be sent to all the other clients on the network.

These two things won't do anything however if you don't allow TNT Basic some time to update the network values. This can be done by calling the command Update Net Data.

Update Net Data

When this is called TNT Basic sends all the network messages it needs to update all the machines on the network.

Event Loop

Events tell you if any players were disconnected, left the game or the game was terminated by the host. Net events can be handled like this...

while More Net Events

if Net Event Type=NetGameTerminated

' Terminate the game

else if Net Event Type=NetPlayerLeft

' Inform player that someone has left the
' game. Net Event Content contains
' the player's id.

else if Net Event Type=NetPlayerDisconnected

' Inform player that someone has been
' disconnected from the game.
' Net Event Content contains
' the player's id.

end if

wend

Game Structure

The main loop for a networking game should be handled something like this...

while gameRunning

HandleControls()
HandleNetworking()
HandlePhysics()
UpdateGraphics()

wend

HandleControls should handle the input from the player on this machine.

HandleNetworking should contain all the code necessary to update net data and process events.

HandlePhysics should take all the game changes that occured inside HandleControls and HandleNetworking and apply the to the game.

UpdateGraphics should redraw the graphics for the player.


Table of contents