Monday, August 25, 2008

Starting with Torque: Variables

For a computer to make decisions it has to have data. This data could be about the number of Hull Points a Starcruiser has, or it could be the players name. For this data to be useful it has to be stored in a useful manner. That is to say, Torque must know where to find that information. The answer to this is variables.

Torque script is a little different than other languages. You do not have to define a variable before you use it. Additionally, there are only a few types of variables and they are not dependent on the type of information stored.

Examples:

$hullPoints = 100;
$playerName = "Lucifer";

The engine knows just how to treat each variable depending on the context.

You will notice that the variable name started with "$". This marks it as a Global Variable. You can use this variable in any function and its value will carry over into any other function. If the variable started with "%" it would be a Local Variable. The value stored for that variable could only be used inside of the function in which it is created.

Data will usually need to be manipulated in some way. Otherwise, why bother having it? Number can be added, subtracted, or any other standard form of calculation in the following way:

$hullPoints = $hullPoints - 1;

Another way to subtract (or decrement) by one is:

$hullPoints--;

You can also increment a variable by 1:

$hullPoints++;

You can also decrement, increment, divide, or multiple by arbitary numbers using the following formulas:

$hullPoints += 10; //increments by 10
$hullPoints *= 2; //multiplies by 2

(Side note: using "//" marks whatever follows it as a comment. Torque ignores anything after the // until the next line.)

Text variable work a little differently. After all, you cannot multiply or divide by a line of text. To put two text variable together we use the "@". Here is an example:

$firstName = "Tom";
$lastName = "Beauchamp";
$fullName = $firstName@" "@$lastName;

If we were to print out the value of $fullName it would give: Tom Beauchamp.

Since Torque recognizes the context that a variable is used in, we can also do this:

$firstName = "Tom";
$lastName = "Beauchamp";
$Age = 36;
$personalInfo = $fullName = $firstName@" "@$lastName@" is "@$Age"@" years old.";

If we were to print out the value of $personalInfo we would get: Tom Beauchamp is 36 years old.

Another way to determine a value is using a boolean expression. Boolean expressions always equal either True (1) or False (0). The number value is what will be stored in the variable.

Example:

$firstAge = 36;
$secondAge = 31;
$isolder = $firstAge > $secondAge;

This last example is the perfect time to talk about storing multiple pieces of the same type of data. What if we had 20 ages to store? Would we want to create 20 separate variable that we then have to keep track of?

No, and luckily we do not have to. This is why Torque Script allows the use of arrays. Instead of creating $firstAge through $twentieth age we can do the following:

$ages[0]=36;
...
$ages[19]=31;

One thing about arrays: The first index position in an array is always 0 and the last is always 1 number less than the total in the array. So if an array has 20 positions, then the last one is 19.

Arrays are very handy for storing large amounts of similar data. When we get to loops they will come in very handy and save a lot of coding time.

If you have any questions, or see any mistakes please let me know at we4ponsgr4de@gmail.com

No comments: