TGB/Getting Started/hellotgbnetwork

From TDN

--Tom Bentz 04:10, 10 December 2006 (PST)

//*************************
//Created by Tom Bentz
//DigitalBlur Entertainment
//games@digitalblur.com
//
//Minimal TGB network example - Hello TGB network
//*************************

NOTES!!!!!!

  • To test this on one computer you will need a debug version of TGB in order to run two instances on the same computer
  • I had problems with my firewall so I needed to disable it. You may also.


How to use this example
1. Download the source and add it to your server and client TGB instances
2. Launch the two instances of TGB and open this project on each
3. In the console on your server run 'startserver();'. You should see a connected message (the server computer connects as a client to itself).
4. In the console on your client run 'connect("SERVERIP");". Where SERVERIP is the ip address of the server. YOU NEED THE QUOTES AROUND THE IP ADDRESS. You should see a connected message.
5. In the console on the server run 'serversayhi("NAME")'. Where NAME is your name or whatever.
6. Check the console on the client and you should see "NAME (server) says hi!"
7. In the console on the client run 'clientsayhi("NAME")'. Where NAME is your name or whatever.
8. Check the console on the client and you should see "NAME (client) says hi!"

You can download the project source here.


Create a server on this computer

function startserver()
{
   echo("creating server");
   createServer(true);
}

Tell this client to connect to the server at the ip address given

function connect(%ip)
{  
   echo("connecting to server");
   connecttoserver(%ip);
}

The 'clientsayhi' is called on the client
The commandToServer appends the argument 'Hi' to 'serverCmd' to create a 'serverCmdHi' function AND executes the function on the server

function clientsayhi(%name)
{
   commandToServer('Hi', %name); 
}


The 'serverCmdHi' function executes on the server from the 'commandToServer' call from above
We take the 'commandToServer' 'Hi' argument from above and append it to 'serverCmd' to create this callback

function serverCmdHi(%client, %name)
{
   echo(%name SPC "(client) says hi!");
}


This executes on the server We take the 'commandToClient' 'Hi' argument and append it to the 'ClientCmd' to create the 'ClientCmdHi' callback and execute it
This loops through all the clients and tells each to execute the 'clientCmdHi' function with our name as an argument

function serversayhi(%name)
{
   %count = ClientGroup.getCount();
   for(%i = 0; %i < %count; %i++)
   {
      %recipient = ClientGroup.getObject(%i);
      //this converts to clientCmdHi function and executes on the client
      commandToClient(%recipient, 'Hi', %name);
   }
}


This executes on each client from the 'commandToClient' call above
We take the 'commandToClient' 'Hi' argument from above and append it to the 'ClientCmd' to create the 'ClientCmdHi' callback and execute it

function clientCmdHi(%name)
{
  echo(%name SPC "(server) says hi!");
}

This shows the basics of stock TGB networking calls. From the commandToClient and commandToServer calls you can create your own game specific network calls.