Reader small image

You're reading from  Torque 3D Game Development Cookbook

Product typeBook
Published inJan 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781849693547
Edition1st Edition
Languages
Right arrow
Author (1)
DAVID WYAND
DAVID WYAND
author image
DAVID WYAND

David Wyand has been using GarageGames' Torque Game Engine for the past 10 years. Among his other interestes are 3D graphics applications, computer networking and Artificial Intelligence for computer games.
Read more about DAVID WYAND

Right arrow

Chapter 9. Importance of Networking

In this chapter, we will cover the following topics:

  • Sending a network event from the client to the server

  • Sending a network event from the server to the client

  • Connecting as a TCP client

  • Setting up a TCP server

  • Connecting as an HTTP client

  • Using an RSS feed for game news, message of the day, or other client messages

  • How to activate, deactivate, and use Telnet for console access

Introduction


Communicating between a Torque 3D game and an external network service could provide leader board support, news to be displayed within the game client, or a login service provider. And if we are building a multiplayer game, then game-specific communication between the client and server is a must.

Fortunately, Torque 3D provides the tools for both internal client/server network communication as well as external network service communication. In this chapter, we will discover how to set up Torque 3D for both types of network communication through various examples.

Sending a network event from the client to the server


Over the course of a networked game, it is often necessary to send an event from the client to the server. This event may also contain data for the server to process. In this recipe, we will learn how to send a network event from the client to the server, and how to have the server process this custom event.

Getting ready

We will be using a project based on the Torque 3D's Full template, using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template; it will be found under the My Projects directory.

How to do it...

In the following steps, we will send a command from the client to the server to kill the player:

  1. Start our Full template.

  2. Once the main menu is displayed, click on the Play button. This will open the Choose Level window.

  3. Choose the Empty Terrain level and click on the Go! button.

  4. When the game has loaded, press Tab to go into third-person...

Sending a network event from the server to the client


Over the course of a networked game, it is often necessary to send an event from the server to the client. This event may also contain data for the client to process. In this recipe, we will learn how to send a network event from the server to the client, and how to have the client process this custom event.

Getting ready

We will be making TorqueScript changes in a project based on the Torque 3D's Full template, using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template; it will be found under the My Projects directory. After that, start up your favorite script editor, such as Torsion, and let's get going!

How to do it...

In the following steps, we will send a command from the server to all the connected clients that will pop up a message dialog and display our custom message:

  1. Open the scripts/server/game.cs file in your text editor, add the following...

Connecting as a TCP client


Communicating with other network-based services is important for some game types, to retrieve or store external data. In this recipe, we will learn how to have Torque 3D connect with a TCP server and communicate with it.

Getting ready

We will be using a project based on the Torque 3D's Full template, using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template; it will be found under the My Projects directory. After that, start up your favorite script editor, such as Torsion, and let's get going!

How to do it...

In the following steps, we will build an object that will handle communications with an external server:

  1. Open the scripts/server/game.cs file in your text editor, add the following code to the bottom of the file, and save:

    // Callback: Cannot resolve name
    function TimeCheck::onDNSFailed(%this)
    {
      echo("TimeCheck: DNS Failed");
    }
    
    // Callback: Connection to service has...

Setting up a TCP server


Allowing other network-based services to communicate with Torque 3D is important for some game types to pass external data to the game server. In this recipe, we will learn how to have Torque 3D act as a server and listen for connections.

Getting ready

We will be using a project based on the Torque 3D's Full template, using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template; it will be found under the My Projects directory. After that, start your favorite script editor, such as Torsion, and let's get going!

How to do it...

In the following steps, we will build an object that will listen for external network connections on a particular TCP port and act as an echo server:

  1. Open the scripts/server/game.cs file in your text editor, add the following code to the bottom of the file, and save:

    function EchoServiceListener::
        onConnectionRequest(%this, %address, %ID)
    {
      echo("EchoServiceListener...

Connecting as an HTTP client


Communicating with an HTTP server can be quite useful for a game. An HTTP server could provide a login service or storage for a game's high scores.

In this recipe, we will learn how to have Torque 3D retrieve data from an HTTP service that returns our external network IP address.

Getting ready

We will be making TorqueScript changes in a project based on the Torque 3D's Full template, using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template; it will be found under the My Projects directory. After that, start your favorite script editor, such as Torsion, and let's get going!

How to do it...

In the following steps, we will connect with an HTTP server that will give us our network IP address:

  1. Open the scripts/server/game.cs file in your text editor, add the following code to the bottom of the file, and save:

    // Callback: Cannot resolve name
    function IPCheck::onDNSFailed(%this...

Using an RSS feed for game news, message of the day, or other client messages


One or more RSS (Rich Site Summary) feeds can be quite useful for a game. They could provide game news directly to the game client, a message of the day published to the chat window, or a summary of the current leader board.

An RSS feed is essentially a stream of data in an XML format, supplied by an HTTP server that we may parse for our own needs. In this recipe, we will learn how to retrieve an RSS feed from a server and parse its results. Our example will be the community blog feed from the GarageGames.com site.

Getting ready

We will be making TorqueScript changes in a project based on the Torque 3D's Full template, using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template; it will be found under the My Projects directory. After that, start your favorite script editor, such as Torsion, and let's get going!

How to do...

How to activate, deactivate, and use Telnet for console access


Torque 3D's console is useful for debugging your game, as well as for altering a running game. While the console is available from both the client and the dedicated server's windows, sometimes we need to access the console remotely. This can be especially useful when a dedicated server has been started as either a Windows service or scheduled task, and there is no window available to interact with.

For these circumstances, Torque 3D provides an internal Telnet server that may be used for remote console access. In this recipe, we will learn how to set up and activate console access through Telnet.

Getting ready

We will be using a project based on the Torque 3D's Full template, using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template; it will be found under the My Projects directory.

How to do it...

In the following steps, we will activate...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Torque 3D Game Development Cookbook
Published in: Jan 2013Publisher: PacktISBN-13: 9781849693547
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
DAVID WYAND

David Wyand has been using GarageGames' Torque Game Engine for the past 10 years. Among his other interestes are 3D graphics applications, computer networking and Artificial Intelligence for computer games.
Read more about DAVID WYAND