Game Development Community

dev|Pro Game Development Curriculum

Short guides to Torque3D

by Lukas Joergensen · 04/22/2014 (9:48 am) · 2 comments

Shameless self-plug

All these guides are taken from the tutorial at my website.
On my website the syntax highlighting is better and fonts are more readable imo.

Relative paths in TorqueScript

"./" refers to the location of the file calling the function.
"~/" refers to the root-child the file lives in ("Game/Scripts/Client/somefile.cs" becomes "Game/Scripts")
"/" or nothing refers to the location of the executable.

Variables in TorqueScript

Variables should always start with either a % or $.
% means the variable is a local variable and cannot be referenced outside of the function which defined it.
$ means the variable is a global variable and can be accessed from anywhere.
Globals defined on the client side is not defined on the server side. They are only accessible from the current instance!
Arrays in TorqueScript can be faked, meaning that $array[0] is the same as $array0.
If you want a true array, you should use the ArrayObject class.

Commands between server and client

The functions commandToClient and commandtoServer sends a command to the client or the server.
commandToClient takes 2+ parameters. The first one being the id of the client, the second is the tag of the command the client should execute. All following parameters is the arguments passed
to the command.
commandToServer is similar, except it doesn't need an ID so it takes 1+ parameters.
To define a function which can be called by a network command, it has to follow a special naming convention.
clientCmdtheCommandName(args);
Example:
// Client:
commandToServer('Foo');
// ..
// Server:
function serverCmdFoo()
{
   // Do something here
}

String concatenation

@ - Concatenates two strings.
TAB - Concatenates two strings with a tab between them.
SPC - Concatenates two strings with a space between them.
NL - Concatenates two strings with a newline between them.
Example:
%concat = "a" @ "b";
echo(%concat); // Outputs "ab"
%concat = "a" TAB "b";
echo(%concat); // Outputs "atb"
%concat = "a" SPC "b";
echo(%concat); // Outputs "a b"
%concat = "a" NL "b";
echo(%concat); // Outputs "anb"

Folders in T3D

Art
Contains all your art les (obviously), datablocks, GUI, fonts. Everything non-game logic related should be located here
Core
Contains alot of core functionality and functions. You can look at this as a "framework" for your game logic.
Scripts
Contains all of your script les in 3 speci c folders. The client scripts in the client folder, the server scripts in the server folder, the GUI scripts in the GUI folder
Levels
Contains your level les (your terrains go here when you create them)
Tools
The world tools, this only contains the tools for editing the world and can be deleted upon release
Shaders
Contains your shaders.

If statements in TorqueScript

Everything in TorqueScript is strings!
This is important to remember with if statements, because in if statements you can either compare by int or by string.
To compare by string you should write $= instead of ==.
Example:
if("asd" == "asd") // This is true
if("asd" == "def") // This is true as well
if("asd" $= "asd") // This is true
if("asd" $= "def") // This is false

Fields and words

There is some pretty cool functions called getWord, setWord, getField, setField.
These functions lets you edit a string at an index based on the number of either spaces or tabs. getField, is based on tabs. getWord is based on spaces.
Examples:
%val = "1 2 3 4";
echo(getWord(%val, 2)); //Echoes 3
echo(getWordCount(%val)); //Echoes 4
echo(setWord(%val, 2, "10")); //Echoes "1 10 3 4"
echo(%val); //Still echoes "1 2 3 4"
%val = setWord(%val,2,"10");
echo(%val); //Echoes "1 10 3 4"
%val = 1 TAB 2 TAB 3 TAB 4;
echo(getField(%val, 2)); //Echoes 3

DefaultDatablocks

The DefaultDatablocks we inherit our datablocks from are simply some datablocks which is defined in the Core folder which allows us for fast prototyping of new emitters.

Troubleshooting

I just downloaded the T3D OS repo and I can't move stuff around in the WorldEditor!
This is probably due to the grid settings. See the magnets at the top of the window?
Try turning them on and off. This should help. Make sure they are o . Especially the magnet with a grid.

#1
04/23/2014 (1:28 pm)
DefaultDatablocks: To elaborate further on that, datablock definitions of the form

datablock XXXData(foo : bar)

means take some old, already defined datablock of type XXXData, and copy every entry from bar over to foo, then replace any of the old data with what is enclosed in the {};. This includes script-only entries you've added on yourself to the bar class, callbacks, ect.
#2
05/02/2014 (5:10 pm)
This sounds helpful, we need more of this. It is hard to understand for newbies how this all is setup in Torque.