Chapter 3. Basics基础
Now that we have seen a very simple sc<x>ript in action, we need to look at the our toolchest for writing sc<x>ripts. The next set of tools we will consider are the basic building blocks for programming a sc<x>ript, and will be used in every non-trivial sc<x>ript you write.既然我们已经看见一个简单程序的运行了,现在就看看如何自己编程吧.下面这些工具用来构架基本程序块从而编程,并且会在实际中应用到.
________________________________________
3.1. Comments 注释
Commenting your sc<x>ripts is a good idea, and will help when you update and modify the sc<x>ript, or when you adapt parts of it into other sc<x>ripts. Unless the meaning is obvious, you should add comments:给你指令行添加注释是个好的习惯,会帮助你更新和更改指令行,尤其在你想吧某个部分添加到其它程序中去的时候(这个真的重要.因为自己编的程序.过段时间再看,自己不一定记得其功能的.) 除非这个意思非常明显,否则一定加注释:
• at the start of the sc<x>ript to explain the purpose of the sc<x>ript
• before every global variable to describe what it holds
• before every global function to describe what it does
• sprinkled through your sc<x>ript wherever the code solves a problem that took you more than a few minutes to figure out.
• 在指令行的开始解释这个程序段的目的
• 在全局变量嵌加描述指明这个全局变量针对哪一段程序是全局的
• 在全局函数前加描述,表明其针对功能
• 在你自己一眼看不出代码功能的地方,加些注释.
LSL uses Java/C++ st<x>yle single line comments. LSL和Java/C++一样,使用单行注释.
// This sc<x>ript toggles a the rotation of an ob<x>ject
//这个指令用来固定一个旋转的对象

// g_is_rotating stores the current state of the rotation. TRUE is
// rotating, FALSE otherwise.
//函数g_is_rotating 保存旋转现在的状态.在Ture的时候,是旋转,false时候则固定.
integer g_is_rotating = FALSE;
default
{
// toggle state during the touch handler
touch(integer num)
{
if(g_is_rotating)
{
// turn off rotation
llTargetOmega(<0>, 0, 0);
g_is_rotating = FALSE;
}
else
{
// rotate around the positive z axis - up.
llTargetOmega(<0>, 4, 1);
g_is_rotating = TRUE;
}
}
}

________________________________________
3.2. Arithmetic Operations 算术运算 这一节请参考任何一本参考C语言中文教材
Most of the common arithmetic operations are supported in lsl, and follow the C/Java syntax. 同C/Java一样,LSL语言支持大部分算术运算________________________________________
3.2.1. Assignment “并”
The most common arithmetic operation is assignment, denoted with the ‘=’ sign. Loosely translated, it means, take what you find on the right hand side of the equal sign and assign it to the left hand side. Any ex<x>pression that evaluates to a basic type can be used as the right hand side of an assignment, but the left hand side must be a normal variable.大部分算术运算都是”并”,用’='表示.简单地说,任何表达式产生一个基本类,把表达式右边的相同的标识赋予左边.而左边只能用普通变量.
All basic types support assignment ‘=’, equality ‘==’ and inequality ‘!=’ operators.所有基本类都支持这几个运算符号:用’='表示”并”, ‘==’ 表示“等于”;用’!=’表示”不等于”
// variables to hold a information about the target
//以下这3行是建立一个变量,在一个目标上,表达一定信息的变量
key g_target;
vector g_target_postion;
float g_target_distance;

// function that demonstrates assignment
//下面是一个执行”并”运算功能的函数
set_globals(key target, vector pos)
{
g_target = target;
g_target_position = pos;

// assignment from the return value of a function
vector my_pos = llGetPos();
g_target_distance = llVecDist(g_target_position, my_pos);
}

________________________________________
3.2.2. Hexadecimal Entry 十六进制输入法
Integers may be entered in hex form (e.g. 0xffff). For example:
整数会以十六进制形式输入,例如:
integer Mask = 0xff; // Equivalent to integer Mask = 255;
integer Bit = 0×0100 // Equivalent to integer Mask = 256;

________________________________________
3.2.3. Binary Arithmetic Operators 二进制算术运算符号
Binary arithmetic operators behave like a function call that accepts two parameters of the same type, and then return that type; however, the syntax is slightly different. 二进制算术运算类似调用同一个类的两个参数,然后返回那个类,但是,语法有些不同.
Table 3-1. Binary Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo (remainder)
^ Exclusive OR
<<Shift>> Shift Right 右移一位
Where noted, each type may have a special interpretation of a binary arithmetic operator. See the lsl typessection for more details.
每一个类可能有一个具体的二进制运算符号.请参照类lsl
________________________________________
3.2.4. Boolean Operators 布尔数据设置
Table 3-2. Boolean Operators布尔运算符号
Operator Meaning
<Operator> Operator returns TRUE if the left hand side is greater than the right hand side.左边的数大于右边的数,运算结果为真
<Operator>= Operator returns TRUE if the left hand side is greater than or equal to the right hand side.
&& Operator returns TRUE if the left hand side and right hand side are both true. 左右两个结果都是真的时候,这个运算结果为真
|| Operator returns TRUE if either the left hand or right hand side are true. 或运算.左或者右,任一为真.则为真.
! Unary operator returns the logical negation of the ex<x>pression to the right.
________________________________________
3.2.5. Bitwise Operators 位操作符号
Table 3-3. Bitwise Operators
Operator Meaning
& Returns the bitwise and of the left and right hand side.两个bit相”与”
| Returns the bitwise or of the left and right hand side.左右两个bit相”或”
~ Unary operator returns the bitwise complement of the ex<x>pression to the right.这个结果返回表达式右边bit的”补”
________________________________________
3.3. Types 类
Variables, return values, and parameters have type information. LSL provides a small set of basic types that are used throughout the language.
变量,返回值以及参数,都是类.LSL提供一些基本类.
LSL Types
integer 整量
A signed, 32-bit integer value with valid range from -2147483648 to 2147483647.
float 浮量 参考C语言中文教材
An IEEE 32-bit floating point value with values ranging from 1.175494351E-38 to 3.402823466E+38.
key 键
A unique identifier that can be used to reference ob<x>jects and agents in Second Life.
vector 矢量
3 floats that are used together as a single item. A vector can be used to represent a 3 dimensional position, direction, velocity, force, impulse, or a color. Each component can be accessed via ‘.x’, ‘.y’, and ‘.z’.
Table 3-4. Vector Arithmetic Operators 算术矢量
Operator Meaning
+ Add two vectors together把两个矢量相加
- Subtract one vector from another两个矢量相减
* Vector dot product 点矢量
% Vector cross product 相交矢量
rotation
4 floats that are used together as a single item to represent a rotation. This data is interpreted as a quaternion. Each component can be accessed via ‘.x’, ‘.y’, ‘.z’, and ‘.s’. 4个浮变量一起可以代表一个旋转.这个数据通过一个4元数组表示.每个元素通过’.x’, ‘.y’, ‘.z’, and ‘.s’来访问.
Table 3-5. Rotation Arithmetic Operators 旋转算术运算
Operator Meaning
+ Add two rotations together 两个旋转量相加
- Subtract one rotation from another两个旋转量相减
* Rotate the first rotation by the second把第一个旋转矢量选择第二个的”度”
/ Rotate the first rotation by the inverse of the second,把第一个选择量反向旋转第二个矢量的”度”
list
A heterogeneous list of the other data types. Lists are created via comma separated values of the other data types enclosed by ‘[’ and ‘]’. 其它类的异类历表,历表内部逗号隔开.见例子:
string StringVar = "Hello, Carbon Unit";
list MyList = [ 1234, ZERO_ROTATION, StringVar ];

Yields the list: [ 1234, <0>, "Hello, Carbon Unit" ]
Lists can be combined with other lists. For example:
历表之间可以捆绑.如:
MyList = 3.14159 + MyList;

Yields the list: [ 3.14159, 1234, <0>, "Hello, Carbon Unit" ]
得到历表: [ 3.14159, 1234, <0>, "Hello, Carbon Unit" ]
And similarly, 同样,
MyList = MyList + MyList;

Yields: [ 3.14159, 1234, <0>, "Hello, Carbon Unit", 3.14159, 1234, <0>, "Hello, Carbon Unit" ]
Library functions exist used to copy data from lists, sort lists, copy/remove sublists.
________________________________________
3.3.1. Type Conversion 类的转换
Type conversion can either occur implicitly or explicitly. Explicit type casts are accomplished using C syntax:
类可以明转换或者暗转换.明转换方法类似C 语法:
float foo_float = 1.0;
integer foo_int = (integer)foo_float;

________________________________________
3.3.1.1. Implicit Casting 暗转换
LSL only supports two implicit type casts: integer to float and string to key. Thus, any place you see a float specified you can supply an integer, and any place you see a key specified, you can supply a string.
LSL 语言只支持两种暗转换: 整量转浮量和串转键. 这样,当你在任何地方看到一个特定的浮量的时候,你可以提供一个整量.(因为你的整量会自动转为浮量,和你看到的目标自动匹配.所以叫暗转换,不需要编程实现.);同样,当你看到一个指定的”键”时候,你可以提供一个”串”.
________________________________________
3.3.1.2. Explicit Casting 明转换
LSL supports the following explicit casts:
LSL支持下列转换方式:
• Integer to String
• Float to Integer
• Float to String
• Vector to String
• Rotation to String
• Integer to List
• Float to List
• Key to List
• String to List
• Vector to List
• Rotation to List
• String to Integer
• String to Float
• String to Vector
• String to Rotation
________________________________________
3.4. Global Functions 全局函数
Global functions are also declared much like Java/C, with the exception that no ‘void’ return value exists. Instead, if no return value is needed, just don’t specify one: 全局函数声明更加象Java/C,没有”空”返回值.但是,如果”无返回值”需要时候,不要定义
make_physical_and_spin(vector torque)
{
// double the torque
vector double_torque = 2.0*torque;
llSetStatus(STATUS_PHYSICS, TRUE);
llApplyTorque(double_torque);
}

________________________________________
3.5. Global Variables全局变量
Global variables and functions are accessible from anywhere in the file. Global variables are declared much like Java or C, although only one declaration may be made per line:
全局变量和函数在文件中随处可见.全局变量声明也很象Java/C,虽然每一行只可以声明一个.(C 一行可以声明多个)
比如下面绿色字体声明一个矢量全局变量:
vector gStartPosition;

Global variables may also be initialized if desired, although uninitialized global and local variables are initialized to legal zero values:
全局变量可以按照需要初始化,而没有初始化的全局和本地变量会自动设置为0.
vector gStartPosition = <10>

________________________________________
3.6. Local Variables本地变量
Local variables are scoped below their declaration within the block of code they are declared in and may be declared within any block of code. Thus the following code is legal and will work like C:
本地变量在其声明的代码块内定义.可以在代码块任何部分声明.下面这段合法代码就类似C:
integer test_function()
{
// Test vector that we can use anywhere in the function
// 注释:测试我们在函数中任何地方可以用的矢量, vector test = <1>;是一个本地变量,对一下括号内所有代码都有效
vector test = <1>;
integer j;
for (j = 0; j < 10; j++)
{
// This vector is a different variable than the one declared above
// This IS NOT good coding practice
vector test = <j>;
}
// this test fails
if (test == <9>)
{
// never reached
}
}