Hosting and Joining
Network Global Variables
Leaving a Game
Before any networking commands can be used in TNT Basic networking must first be initialised, this is done by calling the command Initialise Networking. Once this is called it does not need to be called again.
Hosting and Joining
In any TNT Basic networked game there just be one host, the host is the person who creates the game. When the host creates a game, all other players can then join the game. Once the host has all the players he or she wants then they can start the game, this then starts the game for all the players connected.
To host a game in TNT Basic call the command Host Net Game.
int bool=Host Net Game 8,"Game Name","Player Name"
This announces a game on the network for a maximum of 8 players, the game is called "Game Name". It also displays the interface for the host to choose their name and game name themselves if they want to. The two strings passed are just default parameters that are displayed in the interface, the user is allowed to alter them (Note: if the "Player Name" string is left blank then TNT Basic gets the machine owner's name from the system).
This command returns true if the user got enough players and then started the game. Otherwise it returns false.
To join a game in TNT Basic call the command Join Net Game.
int bool=Join Net Game
This command displays the dialog which lets the user choose a game that is available on the network and then join it. It returns true if the user was gathered into a game and then the game started and it returns false otherwise.
Network Global Variables
Data is sent to and from machines on the network by updating something called "Network Global Variables". A network global variable is exactly the same as any other global variable except that instead of being global over just program, it is global over the entire network. This means the value of the variable is kept the same on all machines.
You can declare network global variables like this...
Network Global Int intVariable,intArray
Network Global Float floatVariable,floatArray
Network Global String stringVariable,stringArray
.. as you can see, network variables can be integers, floats or strings and you can also create arrays of them. There is a condition with using network arrays however, they must all be dimensioned before Initialise Networking is called, otherwise an error will be generated.
You can also define another kind of network variable called an important network variable. These have a higher priority than ordinary network variables and so are updated more often. You can define important network variables like this...
Important Network Global Int intVariable,intArray
Important Network Global Float floatVariable,floatArray
Important Network Global String stringVariable,stringArray
These should only be used for very important network data though, if you define too many of these then TNT Basic will be unable to keep them all updated regularly so you will lose the benefits of them. These should only be used for data that is important to be updated quickly and very regularly, this is usually only information about which controls the player is currently using.
In order to keep the network data updated across the network it is necessary to call the command Update Net Data periodically (ideally every frame). When this command is called on the host it does nothing but when a client calls this command then it copies the data from the host. That means that it will overwrite any data on the client with the host's version.
Using this system the clients can never become out of sync with the host for a significant amount of time, however, this also means that the clients can never change any data because the host will always eventually overwrite it. To get around this problem there is a function called Set Net Data. This function allows a client to send a request to the host to set a specified network variable to a specified value. This means that the client can then affect variables in the game, but it has to tell the host that it is doing so. The host will then update its version and then that will be distributed to all the clients in the game.
During the course of a network game several things can happen that affect the state of the network. The host can close the game, players can leave the game and players can become disconnected.
If any of these events happen then the TNT Basic program is informed automatically. Your program can then find out by checking its 'net events'. You can set up a net event loop that is similar to the net message loop.
while More Net Events
Each net event can then be dealt with in turn inside the body of the while loop.
A network event has two components: a type and a content. These can be extracted using the commands Net Event Type and Net Event Content. The type can be one of three values...
This means the host left or terminated the game. The content of one of these events is left blank. Usually, the program should stop the game and inform the user that the game has been terminated.
This means that a player has left the game. The content of the message is the ID of the player that has left. Usually, the program should continue the game but inform the other users that this player has left the game.
This means that the program has lost contact with a player in the game. The content of the message is the ID of the player that has become disconnected. Usually, the program should continue the game but inform the other users that this player has been disconnected.
Leaving a Game
The user may decide to leave a network game at any time, or they may be killed and forced to leave the game. Either way, your program needs to inform all the other network players that the user is leaving the game. This can be done quite easily in TNT Basic by using the command Leave Net Game.
Leave Net Game
This command informs all the other players that the user has left the game and stops communicating with them. Calling this command is necessary because otherwise other network players be sent messages saying that the user has become disconnected.