LSL可用的数据类型


Second Life中文指南 http://www.secondlifecn.com/ 第二生命 第二人生 Second Life&HiPiHi玩家基地 查看完全版 查看精简版
 提醒您:如果本页图片或Flash动画无法正常显示,请点击这里访问原帖。
LSL可用的数据类型 作者:Tony 时间:2007-1-17 17:31:49 1
Script Memory Usage Limit    //脚本内存空间限制A script can hold, at most, 16 KB (kilobytes) of data. That is 16,384 bytes. How big is that? An alphabetic character occupies one byte; a page of an online book, such as the one you are now reading, occupies about 2 KB.

//一个单独的脚本将最多拥有16K的内存使用空间,即大约8页代码


The 16 KB limit includes both data that exists when you reset the script and stuff typed in and stored during its lifetime. So, it's to your advantage to use variables efficiently.
[edit]
Data Types    //数据类型A variable can hold one or more values, depending on its data type.
In "RL" we can usually tell how to deal with a value just by looking at it. If it's preceded with a $ symbol, like $1.98, it's a monetary amount (in American dollars). If it's divided up with diagonal symbols, like 12/25/06, it's a date (mm/dd/yy). And we know better than to try to calculate a percentage of a date.
LSL decides how to deal with a literal value much like you do. LSL categorizes it as having a given data type, such as "integer," depending on how it's formatted and what special symbols it contains.
When you define a variable, you need to tell LSL its data type. After that, whenever you use the variable name, LSL will "remember" its data type.
[edit]
Declaring a Variable    //定义一个变量A variable is a named memory slot for data. To use a variable, you must first declare it by stating the variable's data type and name.
// If you don't declare a value, the variable will be assigned a default value.string name;// "string" is the type. "name" is the name.// "" is the value, because you didn't specify a value.integer num = 1; // You can also declare a value. This is not required in LSL.// If you don't declare a value, the variable will be assigned a default value.string name;// "string" is the type. "name" is the name.// "" is the value, because you didn't specify a value.integer num = 1; // You can also declare a value. This is not required in LSL.[edit]
Variable Data Types     //可用的数据类型[edit]
Integer     //整数
An integer is a 32-bit value with a range from -2147483648 to 2147483647. Interestingly, integers can only hold whole numbers, as their name implies.

Integer math is of note, because values are truncated, NOT rounded. As such, integer math looks something like this:


// Returns "3," NOT "3.5"integer test = 7 / 2;// Returns "3," NOT "3.5"integer test = 7 / 2;[edit]
Float     //小数
A float is a 32-bit floating point value. Unlike integers, floats can store decimals as well as whole numbers, at the expense of precision.

Float math is performed as anyone that lived through gradeschool may expect.


Q: If this is the case, why use integers? Shouldn't I just use floats for everything?

A: That totally depends on your coding style and the requirements for specific functions and events.


Q: What about doubles?     //注意,LSL中没有double这个数据类型

A: LSL doesn't have them.


[edit]
Vector   //向量
A vector is a series of three floats, named X, Y, and Z (in that order).

Vector math can be very tricky. Here's a resource that might help.

You can also treat parts of a vector as if they were floats. Here's an example:


vector this = <0.0,1.0,2.0>;this.x = 0.0; // The X component.this.y = 1.0; // The Y component.this.z = 2.0; // The Z component.vector this = <0.0,1.0,2.0>;this.x = 0.0; // The X component.this.y = 1.0; // The Y component.this.z = 2.0; // The Z component.[edit]
Rotation     //旋转
A rotation is a series of four floats, this time named X, Y, Z, and S (in that order).    //旋转是一组四个小数

Rotations are defined in quaternions, which for many coders is synonymous with "pain in the ass." //旋转用四元数来定义

For now, here's all you need to know:

- <0,0,0,1> is equal to no rotation.     // <0,0,0,1>表示不做任何旋转,而不是<0,0,0,0>
- If you are uncomfortable with quaternions, try eulers.
- llGetRot and llSetRot are your friends.

Here's a good resource if you want to learn quaternions.


[edit]
String     //字符串
A string is a series of characters, like "The sky is blue." No frills here.   //字符串就是一串字符

Strings can be added and subtracted from quite easily. See string.


[edit]
Key   //键
A key is a special type of string, used to store unique identifiers for the objects in Second Life. Also known as UUIDs, key variables function exactly like strings. Some functions even take strings and keys interchangably, like llStartAnimation, llLoopSound, and llSetTexture.              //Key用来存储UUID,即物品的识别码

For all intents and purposes, keys are strings in denial.

[edit]
List    //表单// List是LSL中独有的一种数据类型
A list is a series of one or more values. The data types of the values can differ. It may include any of the above types and be as short or long as you like. However, bear in mind that a list, as with all variables, is limited by the amount of free memory.
Lists on their own are worthy of long discussion. For now, here are the basics:
  • To add to a list, simply use "+" or "+=" like so:
    list this = [];this += 1; // Adds the integer "1" to this list.this += "2"; // Adds the string "2" to this list.this = [1,"2","x","The sky is blue."]; // You can also define it thusly// Each value in a list is separated by a commalist this = [];this += 1; // Adds the integer "1" to this list.this += "2"; // Adds the string "2" to this list.this = [1,"2","x","The sky is blue."]; // You can also define it thusly// Each value in a list is separated by a commaTo manipulate lists or see their contents, several functions exist. See lists.
    Q: What about arrays?       //LSL中没有队列这个类型

    A: No soup in LSL.


    Q: I'm getting a syntax error. What's going on?

    A: Make sure you defined your list properly and used commas between each entry.
    Also note that you can only type out lists to a certain number of entries (72 or so).
    While lists can store many times this number on their own, a user can manually enter only so many at a time.
    所属版面:制造&脚本 本主题第1页
    共有回复: 0 共有页数: 1 显示主题数:50     深圳玩具 U盘生产厂家