Torque2d MIT study notes (4) -- script syntax (2)

Source: Internet
Author: User
Document directory
  • Number
  • String
  • String Operators
  • Boolean
  • Array
  • Vector
  • Operator
Number

Torquescript supports four numeric types:

123 // integer 1.23 // floating point 123e-4 // scientific count 0 xabcd // hexadecimal
String

Text, such as names or phrases, can be stored as strings.

Numbers can also be stored in string format.

The standard string is stored in the double quotation mark area. For example:

"123abc"

The markup string is stored in the single quotation mark area, for example:

'123abc'

In torquescript, tag strings are specially treated. They not only have their own string data, but also have a special numeric tag associated with it.

A tag string is usually used for sending network data. No matter how many times the string is actually sent, its string data is sent only once, and other times are sent tag values.

When using the detag command to parse the content, we usually do not use the flag string unless many strings need to be transmitted over the network, such as the chat system:

$a = 'This is a tagged string';echo("  Tagged string: ", $a);echo("Detagged string: ", detag('$a'));

Output result:

Tagged string: soh1
Detagged string:

The second act is null unless the string is transmitted to the client over the network.

 

String Operators

Basic Syntax: "string1" operation "string2"

Like using mathematical operators, torquescript provides four operators for programmers.

@ Connect two string tabs use tabs to connect two strings SPC use spaces to connect two strings NL new lines such as ECHO ("hello" @ "world "); echo ("hello" tab "world"); echo ("hello" SPC "world"); echo ("hello" NL "world"); output: helloworldhello worldhello worldhelloworld
Boolean

True (1)

False (0)

Like many other programming languages, 0 and false are considered false, true and non-zero are considered true, and are often used as the condition judgment switch, as follows:

$lightsOn = true;if($lightsOn)  echo("Lights are turned on");
Array

The array structure is used to store consistent data of the same type, for example:

$ Testarray [N] (one-dimensional array) $ testarray [M, N] (two-dimensional array) $ testarray [M_n] (two-dimensional array)

PS: array subscript starts from 0.

For example:

$userNames[0] = "Heather";$userNames[1] = "Nikki";$userNames[2] = "Mich";echo($userNames[0]);echo($userNames[1]);echo($userNames[2]);

 

Vector

In torquescript, "vector" (Composite data) is a very important type. For example, if sceneobject has many variables, it is a combination of two or three data types, such as location, vector, color.

One-time value assignment is a string. A single data is separated by spaces, for example:

%position = "25 32";%firstColor = "100 100 100 1.0";echo(%firstColor);

Combination of script variables:

%red = 128;%blue = 255;%green = 64;%alpha = 1.0;%secondColor = %red SPC %blue SPC %green SPC %alpha;echo(%secondColor);

 

Operator

Arithmetic Operator:

Operator Name Example Explanation
* Multiplication $a * $b Multiply $ A and $ B.
/ Division $a / $b Divide $ A by $ B.
% Modulo $a % $b Remainder of $ A divided by $ B.
+ Addition $a + $b Add $ A and $ B.
- Subtraction $a - $b Subtract $ B from $.
++ Auto-increment (post-fix only) $ A ++ Increment $. 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, before the return value is calculated ). this behavior is unlike that of C and C ++.
-- Auto-decrement (post-fix only) $ B -- Decrement $ 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, before the return value is calculated ). this behavior is unlike that of C and C ++.

 

Relational operators:

Operator Name Example Explanation
'< Less $a < $b 1 If $ A is less than % B (0 otherwise .)
'> More $a > $b 1 If $ A is greater than % B (0 otherwise .)
'<= Less than or equal $a <= $b 1 If $ A is less than or equal to % B (0 otherwise .)
'> = More than or equal $a >= $b 1 If $ A is greater than or equal to % B (0 otherwise .)
'= Equal $a == $b 1 If $ A is equal to % B (0 otherwise .)
'! = Not equal $a != $b 1 If $ A is not equal to % B (0 otherwise .)
'! Logical not !$a 1 If $ A is 0 (0 otherwise .)
'&& Logical and $a && $b 1 If $ A and $ B are both non-zero (0 otherwise .)
$= String equal $c $= $d 1 If $ C equal to $ D.
!$= String not equal $c !$= $d 1 If $ C not equal to $ D.

There is also an additional logic or operation, |, such:

$a || $b

If either of them is non-zero, the result is 1; otherwise, the result is 0;

 

Bit operation

Operator Name Example Explanation
~ Bitwise Complement ~ $ Flip bits 1 to 0 and 0 to 1. (I. e .~ 10b = 01b)
& Bitwise AND $ A & $ B Composite of elements where bits in same position are 1. (I. e. 1b & 1B = 1b)
^ Bitwise XOR $ A ^ $ B Composite of elements where bits in same position are opposite. (I. e. 100b & 101b = 001b)
< Left Shift $ A <3 Element shifted left by 3 and padded with zeros. (I. e. 11B <3D = 11000b)
> Right Shift $ A> 3 Element shifted right by 3 and padded with zeros. (I. e. 11010b> 3D = 00011b)

There is also an extra bit or operation, |, such:

$a | $b

Assignment

Operator Name Example Explanation
Assignment $ A = $ B; Assign value of $ B to $. Note: The value of an assignment is the value being assigned, so $ A = $ B = $ C is legal.
OP = Assignment operators $ A op = $ B; Equivalent to $ A = $ A op $ B, where op can be any:
*/% + -& ^ <>  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.