-
Notifications
You must be signed in to change notification settings - Fork 1
Math
<table bgcolor="#eeeeee" border="1" cellpadding="4" cellspacing="0" width="700">
<tbody>
<tr>
<th bgcolor="#eeeeee" width="15%"><b>
Operator
</b></th>
<th bgcolor="#eeeeee" width="20%"><b>
Name
</b></th>
<th bgcolor="#eeeeee" width="20%"><b>
Example
</b></th>
<th bgcolor="#eeeeee" width="45%"><b>
Explanation
</b></th>
</tr>
<tr>
<td colspan="4" bgcolor="#6699ff"><b>Arithmetic Operators</b>
</td>
</tr>
<tr>
<td>
*
</td>
<td>
multiplication
</td>
<td>
$a * $b
</td>
<td>
Multiply $a and $b.
</td>
</tr>
<tr>
<td>
/
</td>
<td>
division
</td>
<td>
$a / $b
</td>
<td>
Divide $a by $b.
</td>
</tr>
<tr>
<td>
%
</td>
<td>
modulo
</td>
<td>
$a % $b
</td>
<td>
Remainder of $a divided by $b.
</td>
</tr>
<tr>
<td>
+
</td>
<td>
addition
</td>
<td>
$a + $b
</td>
<td>
Add $a and $b.
</td>
</tr>
<tr>
<td>
-
</td>
<td>
subtraction
</td>
<td>
$a - $b
</td>
<td>
Subtract $b from $a.
</td>
</tr>
<tr>
<td>
++
</td>
<td>
auto-increment<br />
(post-fix only)
</td>
<td>
$a++
</td>
<td>
Increment $a.
<strong>Note 1:</strong> ++$a is illegal.
<b>Note 2:</b> the value of $a++ is that of the incremented variable: auto-increment
is post-fix in syntax, but pre-increment in sematics (the variable is
incremented, <i>before</i> the return value is calculated). This behavior is unlike that of C and C++.
</td>
</tr>
<tr>
<td>
- -
</td>
<td>
auto-decrement<br />
(post-fix only)
</td>
<td>
$b--
</td>
<td>
Decrement $b.
<strong>Note 1:</strong> --$b is illegal.
<b>Note 2:</b> the value of $a-- is that of the decremented variable: auto-decrement
is post-fix in syntax, but pre-decrement in sematics (the variable is
decremented, <i>before</i> the return value is calculated). This behavior is unlike that of C and C++.
</td>
</tr>
<tr>
<td colspan="4" bgcolor="#6699ff"><b>Relations (Arithmetic, Logical, and String)</b>
</td>
</tr>
<tr>
<td>
<
</td>
<td>
Less than
</td>
<td>
$a < $b
</td>
<td>
1 if $a is less than % b (0 otherwise.)
</td>
</tr>
<tr>
<td>
>
</td>
<td>
More than
</td>
<td>
$a > $b
</td>
<td>
1 if $a is greater than % b (0 otherwise.)
</td>
</tr>
<tr>
<td>
<=
</td>
<td>
Less than or Equal to
</td>
<td>
$a <= $b
</td>
<td>
1 if $a is less than or equal to % b (0 otherwise.)
</td>
</tr>
<tr>
<td>
>=
</td>
<td>
More than or Equal to
</td>
<td>
$a >= $b
</td>
<td>
1 if $a is greater than or equal to % b (0 otherwise.)
</td>
</tr>
<tr>
<td>
==
</td>
<td>
Equal to
</td>
<td>
$a == $b
</td>
<td>
1 if $a is equal to % b (0 otherwise.)
</td>
</tr>
<tr>
<td>
!=
</td>
<td>
Not equal to
</td>
<td>
$a != $b
</td>
<td>
1 if $a is not equal to % b (0 otherwise.)
</td>
</tr>
<tr>
<td>
!
</td>
<td>
Logical NOT
</td>
<td>
!$a
</td>
<td>
1 if $a is 0 (0 otherwise.)
</td>
</tr>
<tr>
<td>
&&
</td>
<td>
Logical AND
</td>
<td>
$a && $b
</td>
<td>
1 if $a and $b are both non-zero (0 otherwise.)
</td>
</tr>
<tr>
<td>
||
</td>
<td>
Logical OR
</td>
<td>
$a || $b
</td>
<td>
1 if either $a or $b is non-zero (0 otherwise.)
</td>
</tr>
<tr>
<td>
$=
</td>
<td>
String equal to
</td>
<td>
$c $= $d
</td>
<td>
1 if $c equal to $d .
</td>
</tr>
<tr>
<td>
!$=
</td>
<td>
String not equal to
</td>
<td>
$c !$= $d
</td>
<td>
1 if $c not equal to $d.
</td>
</tr>
<tr>
<td colspan="4" bgcolor="#6699ff"><b>Assignment and Assignment Operators</b>
</td>
</tr>
<tr>
<td>
=
</td>
<td>
Assignment
</td>
<td>
$a = $b;
</td>
<td>
Assign value of $b to $a.
<b>Note</b>: the value of an assignment is the value being assigned, so $a = $b = $c is legal.
</td>
</tr>
<tr>
<td>
op=
</td>
<td>
Assignment Operators
</td>
<td>
$a <i>op</i>= $b;
</td>
<td>
Equivalent to $a = $a <i>op</i> $b, where <i>op</i> can be any of:<br />
* / % + - & | ^ << >>
<br /><br />
For example: $a *= $b; is the same as $a = $a * $b;
</td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="700">
<tbody>
<tr><br /><br />
<td width="700"><a name="Creating_the_Script" id="Creating_the_Script"></a>
<h2> <span class="mw-headline">Creating the Script</span></h2>
<p>First, we need to create a new script: </p>
<ol>
<li>Navigate to your project's <b>game/scripts/client</b> directory. </li>
<li>Create a new script file called "maths". In Torsion, right
click on the directory, click the "New Script" option, then name your
script. On Windows or OS X, create a new text file and change the
extension to .cs. </li>
<li>Open your new script using a text editor or Torsion. </li>
</ol>
<p><br />
Before writing any actual script code, we should go ahead and tell the game it should load the script. Open <b>game/scripts/client/init.cs</b>. Scroll down to the <i><b>initClient</b></i> function. Under the // Client scripts section, add the following: </p>
<p><br />
<b>Execute our new script</b> </p>
<pre>exec("./maths.cs");
Standard arithmetic operators are the easiest to script. Start by adding this function to your new script:
// Print the sum of %a and %b function addValues(%a, %b) { %sum = %a + %b;echo("Sum of " @ %a @ " + " @ %b @ ": ", %sum); }
This simple function takes in two numerical arguments. A new
variable, %sum, holds the result of adding the two arguments
together. Finally, an echo(...) statement is formatted to print the
original values (%a and %b) and the sum (%sum of the two).
To test your new script:
- Save the script
- Run your game
- Open the console by pressing the tilde (~) key
- Type the following, pressing enter after each line:
addValues(1,1); addValues(2,3); addValues(-3,2);
Your console output should look like this:
Sum of 1 + 1: 2 Sum of 2 + 3: 5 Sum of -3 + 2: -1
As you can see, you can use positive or negative numbers. You
can also use floating point (decimal) values if you wish. Add the
following script code to test the other basic arithmetic operations:
// Print the difference between %a and %b function subtractValues(%a, %b) { %difference = %a - %b;echo("Difference between " @ %a @ " - " @ %b @ ": ", %difference); }
// Print the product of %a and %b function multiplyValues(%a, %b) { %product = %a * %b;
echo("Product of " @ %a @ " * " @ %b @ ": ", %product); }
// Print the quotient of %a and %b function divideValues(%a, %b) { %quotient = %a / %b;
echo("Quotient of " @ %a @ " / " @ %b @ ": ", %quotient); }
// Print remainder of %a divided by %b function moduloValue(%a, %b) { %remainder = %a % %b;
echo("Remainder of " @ %a @ " % " @ %b @ ": ", %remainder); }
You will use the same process of scripting, saving, running the
game, and calling the functions via the console that has been previously discussed above. Another way of
manipulating values involves more complex operators. Standard
additions, subtraction, etc, use two operators: assignment (=) and
arithmetic (+,-,*,etc).
You can increase or decrease the value of a variable by
using the auto-increment and auto-decrement operators. As soon as the
operation completes, the variable is permanently changed. You do not
need to use an assignment operator in this case. Use the following
script code to test it out:
// Print the increment of %a function incrementValue(%a) { %original = %a; %a++;echo("Single increment of " @ %original @ ": ", %a); }
// Print the decrement of %a function decrementValue(%a) { %original = %a; %a--;
echo("Single decrement of " @ %original @ ": ", %a); }
As you can see, the original value of %a had to be stored
before the increment/decrement operation was applied. The ++ and --
automatically adjust the variable for you. Another non-basic
manipulation involves combining the assignment operator with an
arithmetic operator:
// Print the result of a+=b function addToValue(%a, %b) { %original = %a; %a += %b;echo("Sum of " @ %original @ " += " @ %b @ ": ", %a); }
In the above example, the + and
= are combined together for a single
operation. In simple terms, %a += %b can be verbalized as
"A equals itself plus B." Unlike the addValue(...) function written
earlier, a third variable is not used in this equation. This operation
can be applied to the other arithmetic operators.
The last topic we will cover in this guide is comparison
operators. As the name implies, these operators will compare two values
together and produce a boolean (1 or 0) based on the results. Add the
following function to see the first example:
// Compare %a to %b, then print the relation function compareValues(%a, %b) { if(%a > %b) echo("A is greater than B"); }
The above code is very straight forward. The values of %a
and %b are compared to each other to see which is higher. Test the
comparison code in the console using the following:
compareValues(2,1); compareValues(3,2); compareValues(1,2); compareValues(0,0);
The output should be the following:
A is greater than B A is greater than B <no output> <no output>
The first two calls will prove the comparison as "true", and
print out the message. The comparison results to false on the last two
calls, so nothing will be printed. The rest of the function showing off
the comparison operators can be copied over what you currently have:
// Compare %a to %b, then print the relation function compareValues(%a, %b) { // Printing symbols just as a decorator // Makes it easier to isolate the print out echo("\n====================================");// Print out the value of %a and %b echo("\nValue of A: ", %a); echo("Value of B: ", %b);
if(!%a) echo("\nA is a zero value\n"); else echo("\nA is a non-zero value\n");
if(!%b) echo("B is a zero value\n"); else echo("B is a non-zero value\n");
if(%a && %b) echo("Both A and B are non-zero values\n");
if(%a || %b) echo("Either A or B is a non-zero value\n");
if(%a == %b) echo("A is exactly equal to B\n");
if(%a != %b) echo("A is not equal to B\n");
if(%a < %b) echo("A is less than B"); else if(%a <= %b) echo("A is less than or equal to B");
if(%a > %b) echo("A is greater than B"); else if(%a >= %b) echo("A is greater than or equal to B");
// Printing symbols just as a decorator // Makes it easier to isolate the print out
echo("\n===================================="); }
I have added "decorator text" to help separate console output
and make the output easier to read. Notice that each operation uses an
if(...) statement to compare. Remember, the if(...) code is based on
checking for a 1 (true) or 0 (false) value. This is all a comparison
operation will return.
The guide covers the basic arithmetic operators you will use in TorqueScript. For now, read back over the script code you have been provided with. Study the comments and echo(...) commands, and feel free to test out new operators.
You can download the entire script from this lesson by
CLICKING HERE. Save the script as you would any other text file from a
website.
</td>
</tr>
</tbody>
</table>