Chapter 4. Flow Control 流控制
LSL comes with a complete complement of constructs meant to deal with conditional processing, looping, as well as simply jumping to a different point in the sc<x>ript. LSL在程序中,可以条件处理,环,以及直接跳到另一个点.
________________________________________
4.1. Conditional Statements 条件语句
The ‘if’ statement operates and has the same syntax as the Java/C version.
If条件语句和Java/C语句语法意思一样.这个不翻译了.找本C 基础看.
check_message(string message)
{
if(message == "open")
{
open();
}
else if(message == "close")
{
close();
}
else
{
llSay(0, "Unknown command: " + message);
}
}

The statements between the open and close curly brace are performed if the conditional inside the parentheses evaluates to a non-zero integer. Once a conditional is determined to be true (non-zero), no further processing of ‘else’ conditionals will be considered. The NULL_KEYconstant is counted as FALSE by conditional ex<x>pressions.
There can be zero or more ‘else if’ statements, and an optional final ‘else’ to handle the case when none of the if statements evaluate to a non-zero integer.
The usual set of integer arithmetic and comparison operators are available.
// a function that accepts some information about its environment and
// determines the ‘best’ next step. This kind of code might be
// part of a simple box meant to move close to an agent and attach to
// them once near. This code sample relies on the standard linden
// library functions as well as two other methods not defined here.
assess_next_step(integer perm, integer attached, integer balance, float dist)
{
string msg;
if(!attached)
{
if((perm & PERMISSION_ATTACH) && (dist <10> 10.0) || ((dist > 20.0) && (balance > 1000)))
{
move_closer();
}
else
{
llRequestPermissions(llGetOwner(), PERMISSION_ATTACH);
}
}
}

________________________________________
4.2. Loop Constructs 这个和C 完全相同,不翻译了.
Loops are a basic building block of most useful programming languages, and LSL offers the same loop constructs as found in Java or C.
________________________________________
4.2.1. for loop
A for loop is most useful for when you know how many times you need to iterate over an operation. Just like a Java or C for loop, the parentheses have three parts, the initializer, the continuation condition, and the increment. The loop continues while the middle term evaluates to true, and the increment step is performed at the end of every loop.
// move a non-physical block smoothly upward (positive z) the total
// distance specified divided into steps discrete moves.
move_up(float distance, integer steps)
{
float step_distance = distance / (float)steps;
vector offset = <0>;
vector ba<x>se_pos = llGetPos();
integer i;
for(i = 0; i <steps> 10.0)
{
jump early_exit;
}

// make sure we’re roughly pointed toward the target.
// the calculation of max_cos_theta could be precomputed
// as a constant, but is manually computed here to
// illustrate the math.
float max_cos_theta = llCos(PI / 4.0);
vector toward_target = llVecNorm(target_pos - pos);
rotation rot = llGetRot();
vector fwd = llRot2Fwd(rot);
float cos_theta = toward_target * fwd;
if(cos_theta > max_cos_theta)
{
jump early_exit;
}

// at this point, we’ve done all the checks.
attach();

@early_exit;
}

________________________________________
4.4. State Change 状态改变 (此处恐龙不是很懂,需要进行程序调试几次实验后才可以明白.)
State change allow you to move through the lsl virtual machine’s flexible state machine by transitioning your sc<x>ript to and from user defined states and the default state. You can define your own sc<x>ript state by placing the keyword ’state’ before its name and enclosing the event handlers with open and close curly braces (’{’ and ‘}’.) You can invoke the transition to a new state by calling it with the syntax: ’state <statename>’.
状态改变允许你在LSL虚拟机中保持可变状态,把指令在定义状态和默认状态间切换.把’state’这个词,放在程序段前,然后用(’{’ and ‘}’.)符号把事件指针括起来.然后通过语法: ’state <statename>’赋予一种新的状态来进行调用.
default
{
state_entry()
{
llSay(0, "I am in the default state");
llSetTimer(1.0);
}
//下面4行就是 ’state’,放程序段前,用(’{’ and ‘}’.)符号把事件指针括起来
timer()
{
state SpinState;
}
}
//例: 通过语法: ’state <statename>’赋予一种新的状态来进行调用.
state SpinState
{
state_entry()
{
llSay(0, "I am in SpinState!");
llTargetOmega(<0>, 4, 1.0);
llSetTimer(2.0);
}

timer()
{
state default;
}

state_exit()
{
llTargetOmega(<0>, 0, 0.0);
}
}