Linden sc<x>ripting Language Guide林登脚本编辑语言指南(中英文对照版)
2. Getting Started 入门须知
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里