Working with the TGE Example
From TDN
Contents |
Introduction
Now that you've downloaded the engine, played with the examples provided and poked around in the "example" folder - chances are you are a little lost. To help with your understanding, this article has 3 aims:
- To get you familiar with the scripting portion of your game.
- To understand what's given to you already.
- To understand what you need to work on and where to look.
These issues will be discussed in a non-technical way.
Although it's not technically a requirement, this article also assumes that you've already read the TorqueScript overview, or at least have a basic knowledge of the TorqueScript language.
What's in the script?
This is probably the first question that crosses the mind of a developer new to Torque. Virtually all game logic is in the script. This includes placement and orientation of all objects in the game, how fast your player moves, what happens when the player gets hit, and a long list of other gameplay features. Besides gameplay, included in the example are editors such as the Mission Editor, GUI Editor, Particle Editor, and other tools all written in script to help with your development.
Engine vs. TorqueScript
|
Some game logic falls into the actual code of the engine. Unfortunately, to get a good feel for what exactly is and isn't available in script you need to go into the example and find out yourself.
|
|
|||||||
Control of the Engine with TorqueScript
It's important in a scripting language for a engine to be able to communicate with the script and vice-versa. For this reason, a lot of important engine functionality can be found "in the script". The engine exposes parts of itself to TorqueScript to allow you to control a great deal. You help steer the engine with tools given to you such as calling engine functions, defining functions in script called by the engine, setting client and server preferences, and using special objects (RootGroup, ClientGroup), among other things.
As an advanced warning, because you have so much control over what the engine does you can muck things up a little. Problems could stem from improper use of "special" SimGroups (or $instantGroup confusion), mixing up Server and Client, etc. However, these topics are much more advanced and are out of the scope of this article.
What has already been done for me?
Everyone wants to make their game with as little effort as possible. This section will give you a good idea as to what you don't need to do for your game. The structure of the example will be discussed first, giving you a better idea what is where and how it all comes together.
Directory Structure
*Note: This directory structure does not correspond to the directory structure found in Torque 1.4. The "common" folder is divided into the folders "creator" and "common". The "creator" folder essentially contains all the "common" sub-folders relating to the tools used for your game. This picture is kept as a reference to those who are still developing with a 1.3 codebase.
Not all of the files in the example are listed here, and the diagram doesn't exactly match what's in the "example" folder. However, this diagram does demonstrate the file structure of a general "game". Throughout the rest of the article the "game" folder will refer the game you would be working on or playing with, such as "starter.fps". The diagram above also demonstrates another important point; to load any of the example "game"s in the folder, you need at least that game folder, and the common folder. The reason for this will become apparent shortly.
The Game folder refers to the current game you would be loading (e.g. starter.fps). When Torque starts up in your example folder only one of the "game" folders is chosen to start up. The Common folder contains script functions and the tools that would be used for all "game"s. This includes the editors, tools, and frequently used functions. Both the common and game folders contain their own main.cs file. Don't confuse these with the main.cs in the same directory as your executable. The main.cs files contained in the Common and Game folder contain functions that are packaged and then activated.
|
So what about all of those different file types? The three to pay the most attention to in terms of scripting are your .cs, .gui, and .mis files. These files are regular script files. Why have a different file extension for files that all contain script? Catagorization and organization of script is important - you'll tend to find mission related scripts in the .mis files, GUI (Graphical User Interface) related scripts in the .gui file, and all of the rest of the scripting in the .cs files. Each file can contain whatever scripting you want. You can, for example, contain all of your game scripting in your .gui files, and all of your GUI related scripts in your .cs files. You may find some GUI files may contain a function definition here or there, but it usually relates to the GUI itself. It's your own call when it comes to what goes where. But it helps to keep things organized as your project grows! |
|
|||||||
Main.cs
|
When you run your example, the executable searches for this file - arguably the most important file in TorqueScript. Located in the same directory as your Torque executable, this is the entry point for execution of your script, and therefore your game. In the example given, this script file's job is to load the appropriate folders and their resources into memory. This depends on which game you chose. You can choose your game either by using the command line or just loading the default game. This involves engine function calls (such as loading or "exec()"uting other script files) and other script function calls that initiate and drive the game. |
|
|||||||
The Game Folder
|
The game folder is the folder of the game you are currently loading up (such as starter.fps, or demo). In the example provided, main.cs decides which game to load by loading the appropriate folder and it's resources (via SetModsPath). The Game folder contains all of the game related scripting. When you're scripting for your game this is where you'll be spending most of your time editing, creating, and tweaking script.
|
|
|||||||
The Common Folder
The Common folder could be referred to as the "script engine" portion of your game. This folder was designed to be the multi-purpose tool for scripting for games of all kinds. It contains tons of scripting functions that are specifically designed to make the whole process of scripting easier. Just a brief look at the file structure can give you a good feel as to what has already been done.
The subfolders "Editor" and "Help" contain scripts and files for the Mission, GUI, and Particle editor and their appropriate help files (which can be loaded in game). The lighting folder contains commonly used bitmaps for lighting. The UI (User Interface) folder contains commonly used GUIs (including "MessageBox"es, and tons of GUI default definitions all of which you can find in the GUI editor). The Client and Server contain common routines used in your Client and Server portion of your "Game" scripts respectively. The discussion of the Server vs. Client can be found in this article.
A complete listing of what functions the common folder contains doesn't exist - yet. Some common functions used in the "common" folder are simple functions such as initClient(), initServer(), or createServer(). Take a while browsing through all of the stuff written for you, you'll be amazed at how much you don't need to do (or worry about).
Engine Functions
Remember, you do have the ability to make engine function calls from in-script. Your game will contain at least a few engine function calls.
The complete list of commands (or exposed engine functions) can be found here.
Another important way to interface with the engine is defining functions that are called (whether you like it or not) by the engine. The list of script functions called from within the engine is made up almost entirely of methods (member functions) and only a few function calls. A complete list of functions called into the script from engine doesn't exist yet.
For the more C++ inclined, to get a good idea of what is called by the engine, search around in the engine files for "executef()"; if there's an object as it's first parameter, it's a method, otherwise it's a straight up function call.
Where do I start?
So now that you have a base knowledge of the scripting language (and if you don't, start with the TorqueScript overview) and you have a good idea as to what you already have to work with, how do you start making your game? There are two typical approaches taken by the GarageGames community: Modifying the Game folder and building your Game folder from the ground up.
Modifying the Game Folder
|
This seems to be the more popular approach when it comes to first working with Torque, and the popular "game" folder to modify is starter.fps. For example this is how Marble Blast by GarageGames started. They modified the FPS starter kit (starter.fps) just like many other developers in the GarageGames community.
|
|
|||||||
Building from the Ground Up
With the excellent method shown above, why go through the bother of writing the script for your game from the ground up?
Building from the ground up provides an excellent way of learning all of the ins and outs of the scripting side of your game from the get go. All things being equal, using this approach will increase your knowledge of the scripting technicalities much faster than the previous approach. You'll be spending a great deal of your time looking at the other "game" folders given and looking for scripting examples. When you do manage to get your game up and running you'll have a completely stripped down version of a game and you will know exactly what is and isn't being put into your game - less cleaning up later. It will take noticibly longer for you to get to your prototyping stage, but it will also provide a more hands-on learning experience.
This approach is not as extreme as it may seem. If you choose to write from the ground up, you will want to consider (then reconsider, twice) using the common folder. It will save you tons of time in development and since it contains common routines for each game, you're better off using it. You shouldn't have to re-write the mission or GUI editors unless you have an exceptional reason. Remember - fast prototyping is key, getting your game up and running is first and foremost.
Is it worth the effort? The choice is yours.
What else should I know?
To understand and to develop with Torque properly and well, it's important to have at least a base knowledge of how Torque works. This means understanding the networking model, differences between Client and Server, various "Torque"ish terms used frequently like SimObjects, "Ghosting", and Control Objects. Read up on these topics and others on TDN.
Why would you want to learn how the engine works? If you look around in your example code you'll find funny sounding comments and function calls such as "setControlObject()" or stuff about "transmitting client ghosts". Understanding the terminology and the technical aspects of the engine gives you a much better clue as to how everything comes together in your game. Check out the "Essential TorqueScript Articles". Even if you're not experienced in a language like C/C++, it couldn't hurt to try and poke around in here and see if you can grasp some understanding.
Final Thoughts
You've now probably got some undestanding of what's given to you already and what you need to know and work on. One of the best ways to learn is by going into the script and working with it yourself as best as you can. You will get stumped, confused, and maybe completely lost. This is okay, here's how to deal with it.
- Learn from the "example" given - Just reading through the script gives you an idea of what's what. Use "Find in files" or "Find in Project" liberally. Try your best to investigate what you're looking for.
- Search and read articles here on TDN - A no-brainer. Chances are someone has already had the same problem you have, and they may have submitted an article about it here on TDN. This is definately your first stop when looking for any Torque related answers.
- Search the Forums - then use them - Forums. Search them first! Someone may have already asked your question. Save everyone some time: use the google miniapp and search. If you still can't find the answer to your problem, post it. Take your time when writing your question or describing your problem, often just writing out the problem can lead to sudden insights and figuring it out for yourself. If you do post, make sure you ask your questions the smart way. This will increase your chances of getting the answer you were looking for.
You have a lot of work to do! So get to it, make your game, and have fun!







