I've wanted to program games since I first played Santa Paravia on a TRS-80 model 1 in 1980. I started with some simple "Choose your path" style adventures in BASIC. Then, with the Commodore Amiga and AMOS, I made some simple ShadowRun decking adventures. But all the work I've done has only really been playing at it, I've never really had the time, or made the time, to learn fully what I need to know to do what I really want to do.
Recently I came across the book 3D Game Programming All in One.
The book really isn't what the title states, it's more like "Create a Torque Game Engine Game using Scripting," but what it
did do is introduce me to the Torque Game Engine. With the game engine already built, a good engine, used in games like
Tribes and Tribes 2, all I had to do would be modify the scripts, create my own models and textures, and I'd finally have
a game I'd be happy with.
I've tried following the book, I've tried reading tutorials all over the internet, but most of them are really focused toward scripters. Basically they provide scripts, show you how to tweak and modify those scripts, and that's all. There's no why, no in depth coverage of what the engine is really capable of, just a simple brushing over it to help scripters modify an existing game and "make" their own.
Having coded in languages as diverse as BASIC for the TRS-80 to 68k ASM for the Commodore Amiga, I was looking for a more in depth level of knowledge and control, and it pretty much looked like I had to work it out myself.
I like to get the most out my learning, so sharing it with others makes the time invested multiply, and helps me reinforce what I learn. So here are my Torque tutorials, designed to take the TGE from the ground up.
Requirements
You must have a copy of the Torque Game Engine (TGE) for these scripts to work. You can download a free demo from
Garage Games at http://www.garagegames.com. If you plan on creating the
"Next Big Thing," be sure to buy the license for the full version. It's only $100 and it comes with full souce code
so you can compile it yourself and modify it. These scripts only require the TGE from the demo. After installing the
demo, the TGE executable is in the Torque Game Engine Demo directory, named demo.exe. Copy the file to a
new directory to prevent accidently modifying the demo game.
To run each of these scripts, simply open a command window and run the demo application. All examples will be on a Windows computer, I don't have a Mac and I don't use Linux for anything but servers. Everything should be the same anyway, but if you have a problem, please go to the Garage Games site, they have a very helpful group of people using the forums there.
Getting Started
The TGE uses simple text files for scripting, compiles these text files, then executes them on the current platform. This is one of the things that makes it so cross-platform capable. Creating a text file can be done with Notepad, or more advanced text editors like TextPad or UltraEdit32, however, unless you know how to force Microsoft Word to save in Text Only mode, don't use it.
TGE script is very similar in syntax to C, C++, Java and JavaScript, so if you've worked with any of these languages before, a lot of TGE script will be familiar to you. It is, however, a type-less language, meaning that there is no specific type of variable, all variables are strings in one manner or another, but we'll get to that later.
What is minimally required to get the TGE to actually DO something? Not a question often answered, probably because for a scripter, a script that does a little something isn't as interesting as one that does a whole game, even if someone else built that game and you are just tweaking it. The first script is VERY simple, straight forward, and works nicely. Typical of the programming world, the first script outputs the string "Hello, World!"
The TGE requires by default, the primary script to be named main.cs. The cs extension is standard
for TGE scripts, except where noted later on. Open your favored text editor and enter in the following code, or you can
copy and paste it directly from here.
All scripts have been tested to work, if you have problems, please let me know.
// main.cs - 7 APR 05
function onExit() {
}
enableWinConsole(true);
echo("Hello, world!");
quit();
The first line is a comment, this means it is text written to help the programmer, but ignored by the TGE. A good first line comment is the file name, date it was created or modified, and maybe the author. Comments are not required, but can be immeasurably helpful when you go back to modify your code a week, month, or year later and can't remember how or why something works. Comments start with the double slash, and everything after that is ignored by the game. They can stand alone on their own line, or be at the end of a statement as well.
The next line is a function declaration. Functions are processes that are called by the TGE or by your script. The onExit
function is called by the TGE and even though it doesn't do anything, if you removed it, the program would still run in this case,
but the TGE would give you an error that it can't find it: "onExit: Unknown command."
Functions and variables are not case-sensitive. That means you could call your function ONEXIT if you wanted to. However, for readability sake, camel-case, capitalizing the first letter of each word after the first, is probably the best way to write things.
The keyword function identifies that you are declaring a new function, the can be called on to do work. After the
keyword is the actual name of the function. This is how the function would be called. The parentheses are used to enclose any
arguments, or variables sent to the function to work with. In our case, the onExit function has no arguments so
the parentheses are empty. And lastly are the curly braces, used to enclose the statements that do the work. Since there are
no statements within the function, we have an empty function, that when called, does nothing but return back to where it was
called. Empty functions can be common when working with the TGE.
The next line is an example of calling a function. The function enableWinConsole tells the TGE whether or not we
want to display the console window. The value true within the parentheses means we do want to open the console
window so we can see any output. The semi-colon at the end identifies the end of the statement.
The following line also calls a function, echo. The echo function echoes the argument to the console.
The argument in this case is the string "Hello, world!" and it is sent to the echo function for display.
And the last line calls the function quit. This function is obvious in what it does, it ends the program.
Save your script in the same directory as your TGE, remembering to name it main.cs, then run the TGE and observe
your output. If you don't see Hello, world! in the console, then you did something wrong. Go back to your script and check
it over.

