Linden sc<x>ripting Language Guide林登脚本编辑语言指南(中英文对照版)

Table of Contents 目录
1. Introduction 导论
2. Getting Started 入门须知
2.1. Hello Avatar 例程”你好,Avatar”
2.1.1. Creating the sc<x>ript 开始编程
2.1.2. Default State 默认状态
2.1.3. Functions 函数
2.1.4. Touch Event 事件驱动
2.1.5. Try it Out试用
2.2. Using The Built-In Editor建模固定编辑器试用办法
2.3. Using Alternative Editors其他编辑器使用办法
3. Basics 基础教程
3.1. Comments 注释
3.2. Arithmetic Operations 算法应用
3.2.1. Assignment 任务
3.2.2. Hexadecimal Entry 十六进制录入
3.2.3. Binary Arithmetic Operators 二进制算法设置
3.2.4. Boolean Operators 布尔数据设置
3.2.5. Bitwise Operators位操作符设置
3.3. Types 分类
3.3.1. Type Conversion类型转换
3.4. Global Functions 引用全局变量时的Global文件设置
3.5. Global Variables 全局变量
3.6. Local Variables Local变量
4. Flow Control 流程控制
4.1. Conditional Statements 条件语言
4.2. Loop Constructs 循环结构
4.2.1. for loop for循环
4.2.2. do-while loop do-while循环
4.2.3. while loop while循环
4.3. Jumps 跳转
4.4. State Change状态更改
5. States 状态
5.1. state_entry() 状态录入
5.2. state_exit() 状态导出
5.3. States vs. Global variables状态VS全局变量
6. Math 数学
6.1. Tables of Functions数学函数列表
7. Strings 线性代数
7.1. Tables of Functions线性代数函数列表
8. Lists 清单
8.1. Tables of Functions列表函数列表
9. Communication 通信
9.1. Tables of Functions通信函数列表
10. Inventory 清单
10.1. Tables of Functions函数列表
11. Vehicles 交通工具
11.1. Overview 总论
11.2. Warnings 警告
11.3. Definitions 定义
11.4. Setting the Vehicle Type 设定交通工具类
11.5. Linear and Angular Deflection 角、线偏转
11.6. Moving and Steering the Vehicle 交通工具的移动和操作
11.7. The Linear Motor 线发动机
11.8. The Angular Motor 角发动机
11.9. Using the Camera to Steer 视角显示的实现控制
11.10. The Vertical Attractor引力
11.11. Banking 倾斜
11.12. Friction Timescales 摩擦力时标
11.13. Buoyancy 漂浮
11.14. Hover 盘旋
11.15. Reference Frame参考坐标
A. Linden Library Functions 林登函数库函数

Chapter 1. Introduction
The Linden sc<x>ripting Language (LSL) is a simple, powerful language used to attach behaviors to the ob<x>jects found in Second Life. It follows the familiar syntax of a c/Java st<x>yle language, with an implicit state machine for every sc<x>ript.
Multiple sc<x>ripts may also be attached to the same ob<x>ject, allowing a st<x>yle of small, single-function sc<x>ripts to evolve. This leads to sc<x>ripts that perform specific functions ("hover", "follow", etc.) and allows them to be combined to form new behaviors.
The text of the sc<x>ript is compiled into an executable byte code, much like Java. This byte code is then run within a virtual machine on the simulator. Each sc<x>ript receives a time slice of the total simulator time allocated to sc<x>ripts, so a simulator with many sc<x>ripts would allow each individual sc<x>ript less time rather than degrading its own performance. In addition, each sc<x>ript executes within its own chunk of memory, preventing sc<x>ripts from writing into protected simulator memory or into other sc<x>ripts, making it much harder for sc<x>ripts to crash the simulator.
This tutorial introduces the reader to the basic features of LSL, how to edit and apply your sc<x>ripts, and a complete reference for standard linden constants, events, and library functions.
LSL语言简单有效,易学,功能强大.给第二生命中的对象赋予生命。
它与C/Java类型语言语法相似, 每个语法对应一个隐型状态机。
多重脚本可以同时赋予一个,支持功能单一、没有风格的脚本的扩展,从而一段语法执行一个具体的函数,也允许函数绑定实现新的行为.
LSL语言程序段是可执行代码,类似Java,直接在虚拟机或者仿真机上运行.每一个程序段接收分配到仿真机全部时间上的一个时间片. 因此带有多条执行指令的一个仿真机单条指令执行时间更短,而不会减低性能.而且,每条指令在其内存块内执行,防止指令写入被保护的模拟内存或者其它程序段内,使程序段很难进入仿真机.
这个教程给出了LSL的基本功能,比如如何编辑和应用指令,如何为一个标准的linden常量,事件以及函数建立参数等.
Chapter 2. Getting Started
You’re probably wondering what you can do with LSL, and how quickly you can do it. We’ll start with some simple examples, dissect them, and introduce you the sc<x>ript development process while we’re at it.
你可能在想要多久才会用这个LSL,我们从最简单例子开始吧,分解它们,从而介绍如何编程.
________________________________________
2.1. Hello Avatar 致虚拟公民
Continuing a long tradition of getting started by looking at a sc<x>ript that says "Hello", we’ll do just that. Though obviously not a particularly useful example on it’s own, this example will introduce us to:
• Creating a basic sc<x>ript
• sc<x>ript states
• Calling functions
• sc<x>ript events
• Applying a sc<x>ript to an ob<x>ject
我们都知道任何程序语言开始都是”hello”.我们这里也是如此.虽然这个”Hello”的小程序不是特别有用,但是我们可以知道:
创建一条指令
指令状态
调用函数
指令事件
把指令应用到对象上
________________________________________
2.1.1. Creating the sc<x>ript创建一条指令
Start by opening your inventory and selecting ‘Create|New sc<x>ript’ from the inventory pull down menu. This will create an empty sc<x>ript called ‘New sc<x>ript’ in your ’sc<x>ripts’ folder. Double click on the text or icon of the sc<x>ript to open the sc<x>ript in the built in editor. When you open the sc<x>ript, the viewer will automatically insert a basic skeleton for lsl. It should look like: 在” inventory”下拉菜单中,选择” ‘Create|New sc<x>ript’”,这样我们就已经创建了一条新的空指令: ‘New sc<x>ript’在我们的” ’sc<x>ripts”—程序文件夹里. 在编辑器中, 双击文本,或者是指令按钮,以打开程序,视图器就会自动插入一个基本的框架如下:
default
{
state_entry()
{
llSay(0, "Hello, Avatar!");
}

touch_start(integer total_number)
{
llSay(0, "Touched.");
}
}

A casual inspection of this sc<x>ript reveals that this sc<x>ript probably says ‘Hello, Avatar!’ when it enters some state, and it says ‘Touched.’ when it is touched. But since this is also probably the first time you have seen a sc<x>ript we’ll dissect this short listing, explaining each segment individually.
这个小程序的级联格式会在某种状态下,在屏幕显示’Hello, Avatar!’,当触摸它的时候,屏幕显示’Touched.’.因为这也许是你第一次看到一个程序指令,我们来分解这段小指令,一个小段一个小段的解释.
________________________________________
2.1.2. Default State
default
{

}

All LSL sc<x>ripts have a simple implicit state machine with one or more states. All sc<x>ripts must have a default state, so if there is only one state, it will be the ‘default’ state. When a sc<x>ript is first started or reset, it will start out in the default state.每段LSL指令都是一个带有一到多种状态的简单隐型状态机.每一个程序都有默认状态,如果只有一种状态的话,那就一定是默认状态. 当一个程序第一次打开或者reset后,它都会是默认状态.
The default state is declared by placing the default at the root level of the document, and marking the beginning with an open brace ‘{’ and ending with a close brace ‘}’. Because of it’s privileged status, you do not declare that it is fact a state like you normally would with other states.这种默认状态取代跟目录文件下的状态,并且在程序段开始以
{

}
形式标明.这和其它状态下必须特别指明.
Every time you enter a state, the sc<x>ript engine will automatically call the state_entry() event and execute the code found there. On state exit, the sc<x>ript engine will automatically call the state_exit() event before calling the next state’s state_entry handler. In our example, we call the llSay() function in state_entry() and do not bother to define a state_exit() handler. the state entry and exit handlers are a convenient place to initialize state data and clean up state specific data such as listen event callback.每次输入一种状态,程序引擎会自动调用状态输入函数state_entry()并执行指令. 一个状态存在, ,程序引擎会在调用一下一个状态输入函数state_entry之前,自动调用状态存在处理函数state_exit(). 在例子中,我们在状态输入函数state_entry()中调用函数llSay(), 而不会去定义状态存在处理函数state_exit(). 状态输入器和存在处理函数可以非常方便初始化状态以及清除具体的状态数据,比如事件”听”的撤回.
You can read more about the default state, and how to create and utilize other states in the states chapter. 在”状态”一章中,你可以了解更多自动状态以及创建和应用其它状态.
________________________________________
2.1.3. Functions 函数
The language comes with well over 200 built in functionswhich allow sc<x>ripts and ob<x>jects to interact with their environment. All of the built in functions start with ‘ll’. LSL语言有200中内置函数,允许指令和对象在环境中互相影响. 所有这些内置函数都以’ll’开始.比如调用函数llSay()两次,就可以在特定的信道中,调出文本.
The example calls the llSay() function twice, which is used to emit text on the specified channel.
指令如下:
llSay( integer channel string text );
Say text on channel. Channel 0 is the public chat channel that all avatars see as chat text. Channels 1 to 2,147,483,648 are private channels that aren’t sent to avatars but other sc<x>ripts can listen for. 以文本信道为例.信道0是公民聊天的文本框,信道1,2.147,483到648是私人信道,不对公民开放,但是其它程序段可以听取.

You can define your own functions as long as the name does not conflict with a reserved word, built in constant, or built in function.你可以定义你函数长度,只要名字不是LSL语言中专用函数语言以及内置常量,嵌入函数
________________________________________
2.1.4. Touch Event 触摸事件
touch_start(integer total_number)
{
llSay(0, "Touched.");
}

There are many eventsthat can be detected in your sc<x>ripts by declaring a handler. The touch_start() event is raised when a user touches the ob<x>ject through the user interface.许多事件可以通过你的程序宣布一个处理器来监测.在通过用户界面触摸到对象的时候, 函数touch_start()会执行.
________________________________________
2.1.5. Try it Out 测试
Now that we have seen the default sc<x>ript, and examined it in some detail, it is time to see the sc<x>ript in action. Save the sc<x>ript by clicking on Save. During the save process, the editor will save the text of the sc<x>ript and compile the sc<x>ript into bytecode and then save that. When you see message ‘Compile successful!’ in the preview window, you know the compile and save is done.
我们已经知道默认程序指令.现在让我们看看指令运行.点击”save”保存程序.在保存过程中,编辑器会保存程序文本并编译程序指令到执行指令中保存.当你在预览窗口,看到” Compile successful!”,编译和保存工作都做好了.
To test the sc<x>ript you will have to apply it to an ob<x>ject in the world. Create a new ob<x>ject in the world by context clickingin the main world view and selecting Create. When the wand appears, you can create a simple primitive by clickingin the world. Once the ob<x>ject appears, you can drag your newly created sc<x>ript onto the ob<x>ject to start the sc<x>ript.要测试程序,你需要在把它应用到一个对象中 . 在world里,在主视窗里,点击”create”. 当那个小棒棒”wand”出现时,可以点击world,而创建一个小的原始世界.在这个新的原始世界里,一个对象出现时,你可以拖到你新创建的程序到这个对象身上,这样我们创建的程序就可以运行了.
Soon after dragging the sc<x>ript onto the ob<x>ject, you will see the message ob<x>ject: Hello Avatar! 当你把程序拖到对象身上,你可以看到对象说: Hello Avatar!
Make sure the touch event is working by clickingon the ob<x>ject. You should see the message Touched printed into the chat history.当在点击对象时候,在聊天历史记录信道里,会出现” Touched”
________________________________________
2.2. Using The Built-In Editor 使用内置编辑器
The built in editor comes with most of the typical features you would expect from a basic text editor. Highlight text with the mouse, or by holding down the shift key while using the arrow keys. You can cut, copy, paste, and delete your selection using the ‘Edit’ pull down menu or by pressing the usual shortcut key.在内置编辑器中,有一个基本的文本编辑器拥有的大部分功能.用鼠标选中文本,或者在利用方向键+”shift”来选中文本,然后 Ctrl+X, Ctrl+C,Ctrl+V,用其它的通常快捷键,或者在’Edit’下拉菜单中, 你可以剪切.拷贝.粘贴和删除你所选的文本.________________________________________
2.3. Using Alternative Editors 使用可选文本
Since the built-in editor supports pasting text from the clipboard, you can employ a different editor to edit your sc<x>ripts, copying them into Second Life when you’re ready to save them. 内置编辑器支持从粘贴板粘贴文本功能,我们就可以利用其它编辑器(如office系列word excel之类)来编辑指令,然后把他们拷贝到secondlife world里.