TGB/Networking

From TDN

This page is a Work In Progress by Seth Willits, dated May 15, 2008.

Sections


Connections:


Communication:


Misc:


Reference:





Introduction


TorqueNet Lite
TorqueNet Lite
"Based on award-winning network functionality, TorqueNet Lite
makes implementing turn-based multiplayer options a breeze."


Torque Game Builder includes a subset of the networking features found in Torque Game Engine and dubs them "TorqueNet Lite". While TorqueNet Lite certainly does make implementing turn-based multiplayer games a breeze, it is in no way limited to turn-based games, or even focused on making them! While TorqueNet Lite doesn't support "real-time networking" out-of-the-box like its big brother, with TorqueNet Lite, you can still create exciting and addictive multiplayer games.


Creating a multiplayer game from scratch involves a lot of work. TorqueNet Lite takes care of the tedious work of handling the level work to make and accept connections and actually sending and receiving raw data, leaving you as a game creator to focus on actually dealing with the parts specific to your game.




Understanding Server / Client Architecture

Before embarking on creating a networked multiplayer game with Torque Game Builder, you should first be familiar with the concepts of a client and a server, and the roles each should play. This article is aimed at answering what a “server” and a “client” are, the differences between the two, and how to write your code and script appropriately.


For an overview of the low level aspects of Torque's networking system, see the Torque Game Engine networking documentation. There is also a great wealth of conceptual information available in the Torque Networking section of the TDN Wiki.




Turning on Networking

In commonConfig.xml in the common folder, change <UsesNetwork>0</UsesNetwork> to <UsesNetwork>1</UsesNetwork>. This property will set $Game::UsesNetwork to true, allowing common.cs to automatically call initBaseClient() and initBaseServer(), which will execute the needed code to setup the client and server.




Things to Think About

Making a turn-based game with Torque Game Builder is a breeze, but what about a more complex game such as a networked multiplayer version of Space Invaders or 1942? Stephen Zepp points out some things you should think about:

-- How many objects do you expect to network? Obviously, the objects the player controls, and probably the enemies, and the bullets--but depending on your game expectations, that could be 10 objects, or 1,000.


-- Are your objects deterministic in their physics? In other words, do your bullets and enemies fly paths that depend on the simulation itself, or can you reasonably base all of the object's paths and effects on a combination of time passing and network events arriving?


Example:
-- If during a level, a wave of creatures always follows the exact same path regardless of what the player does, then you don't need to network those objects at all--all you need to network is the time at which the level should start. From there, each client simulation can deterministically duplicate the locations of each of the enemies with no networking at all.


-- You can do the same thing with bullets--instead of trying to network each and every bullet, take a half step back and network "startFiring Player1" and "stopFiring Player1"--then, each client can simply create bullets locally, and approach synchronization relatively realistically.


-- From there, you can start adding in corrections to the simulation (otherwise known as synchronization)--when an enemy is destroyed on player A's machine, send an event to the other players in the simulation that destroys the enemy. Once you've identified the key objects that actually do need to be updated and maintain tight synchronization, you can start expanding this concept of corrections with things like "Player1Ship PositionUpdate 12.6 32.2", and use interpolation on the receiver's side to re-synchronize if needed.


Finally, and this is something that can be hard to recognize: No network simulation, ever, is completely synchronized--it's impossible, because there is no such thing as 0 latency (data always takes at least some time to transfer from one computer to another). With that truism in mind, you now need to decide just how synchronized you must be for reasonable game play...and that will drive your implementation decisions.








Server Preferences

// Server
$pref::Server::Name
$pref::Server::Password
$pref::Server::Info
$pref::Player::Name // The default name for the hosting server's client
$pref::Server::port
$pref::Server::MaxPlayers

// Master Server Related:
$pref::Server::RegionMask = 2;
$pref::Net::RegionMask = 2;
$pref::Master0 = "2:master.garagegames.com:28002";

// Chat Related:
$Pref::Server::FloodProtectionEnabled
$Pref::Server::BanTime
$Pref::Server::KickBanTime




Global Variables on the Server


  • $serverCreated: Whether or not the game is running a server.
  • $serverLocal: Logically identical to $serverCreated.




Global Variables on the Client

  • $serverConnected: Whether or not the client is connected to the server.




Commands (such as 'ClientJoined' in clientConnection.cs --> GameConnection::onConnect)

message.cs (messageAllExcept, messageTeam, etc)

http://tdn.garagegames.com/wiki/Torque_2D/RealTimeNetworking