Linden Script Language - LSL                                                by                        Stefano Cacciaguerra...
Outline  I.     Basic Script Concept  II.    Constants, Variables and Data Type  III. Flow-Control Statement  IV. States  ...
I. What is LSL?  LSL is language used to attach behaviors to the objects  It has syntax similar to Java, with an implicit ...
What is LSL?   The script is compiled into an executable byte code.   This byte code is then run within a virtual machine ...
Creating the Hello Script default {                                              This script says “Hello,   state_entry() ...
Default State default { ... } All scripts have a simple implicit state machine with one or more states. They must have a d...
Default State Every time you enter a state, the script engine calls the state_entry() event and execute the code found the...
Functions   The language has over 200 built in functions which allow   scripts and objects to interact with their environm...
Functions  llSay( integer channel, string text );  Say text on channel.  Channel 0 is the public chat channel that all ava...
Touch Event   touch_start(integer total_number)   {     llSay(0, "Touched.");   }   There are many events that can be dete...
Save  Save the script by clicking on Save.  During the saving process, the editor will       •    save the text of the scr...
Test the script  Apply it to an object in the world.  Create a new object in the world by context clicking in the  main wo...
II. Comments  Comment your scripts:  •    at the start of the script to explain the purpose of the script  •    before eve...
Example// g_is_rotating stores the current state of rotation. TRUE is rotating else FALSE.integer g_is_rotating = FALSE;de...
Arithmetic Operations    // variables to hold a information about the target    key g_target;    vector g_target_postion; ...
Hexadecimal Entry  Integers may be entered in hex form (e.g. 0xffff) starting it  with “0x”:  integer Mask = 0xff; // Equi...
Binary Arithmetic Operators      Operator                             Meaning      +                                    Ad...
Boolean OperatorsOperator Operator returns<               True if the LX is less than the DX>               True if the LX...
Bitwise Operators  A bitwise operation operates on one or two bit patterns or  binary numerals at the level of their indiv...
LSL Types  LSL provides a small set of basic types simple:  integer: A signed, 32-bit integer value with valid range from ...
Simple: Key key: A unique identifier can be used to reference objects and avatars in SL. Known as UUID (Universal Unique I...
Composite: Vector  vector: 3 floats that are used together as a single item. Each  component can be accessed via .x, .y, a...
Example       vector p = llGetPos(); // Get the object’s position in the sim       v1= < x1, y1, z1> and v2= < x2, y2, z2>...
Composite: Rotation  rotation: A quaternion, 4 floats that are used together as a  single item to represent a rotation. Ea...
Example      Rotation zero = ZERO_ROTATION      zero= <0.0, 0.0, 0.0, 1.0> // to represent no rotation      vector r = llG...
Composite: List  list: A heterogeneous list of the other data types. Lists are  created via comma separated values of the ...
Examples MyList = 3.14159 + MyList; => [ 3.14159, 1234, <0,0,0,1>, "Hello, Carbon Unit" ] MyList = MyList + MyList; => [ 3...
Accessing Item Inside a List  Items are accessed by llList2Integer, llList2String, llList2Float  and so on.  Items in the ...
Accessing Item Inside a List  Lists are immutables, once created the value in the list is not  possible to change it.  It ...
Composite: String  A string could hold text enclosed by “ and ”.                   Operator Operator returns              ...
Type Conversion  implicit type casts:  •    integer to float  •    string to key.  explicit type casts:  •    integer to s...
Global Functions Global functions are also declared much like Java/C, with the exception that no void return value exists:...
Global Variables Global variables and functions are accessible from anywhere in the file. Only one declaration may be made...
Local Variables    Local variables are scoped below their declaration within    the block of code they are declared in    ...
Local Variables integer test_function() {    // Test vector that we can use anywhere in the function    vector test = <1,2...
III. Conditional StatementsThe if statement as the Java/C version.check_message(string message){  if(message == "open")   ...
Conditional StatementsThe NULL_KEY constant counts as FALSE.There can be zero or more else if statements, and anoptional f...
Conditional Statementsassess_next_step(integer perm, integer attached, integer balance, float dist){  string msg;  if(!att...
for loopA for loop is most useful for when you know how many timesyou need to iterate over an operation.Just like a Java o...
for loop    // move a non-physical block smoothly upward (positive z) the total    // distance specified divided into step...
do-               do-while loopThe do-while loop construct is most useful when you are surethat you want to perform an ope...
do-              do-while loop    talk_about_inventory(integer type)    {       string name;       integer i = 0;       in...
while loop The while loop allows you to exit the loop without doing a single iteration inside. mention_inventory_type(inte...
State Change State change allow you to move through the LSL virtual machines flexible state machine by transitioning your ...
State Change         default         {           state_entry()           {              llSay(0, "I am in the default stat...
State Change         state SpinState         {            state_entry()            {               llSay(0, "I am in SpinS...
IV. States All scripts must have a default state, which is the first state entered when the script starts. States contain ...
state_entry()The state_entry event occurs whenever a new state is entered:• including program start,• and is always the fi...
Warning: state_entry()   It is a mistake to assume that the state_entry() callback is   called when you rez an object out ...
Example     // global initialization function.     init()     {         // Set up a listen callback for whoever owns this ...
Example     default     {       state_entry()        { init(); }         on_rez(integer start_param)         { init(); }  ...
state_exit()                  state_exit()Provide a state_exit() if you need to clean up any eventsrequested in the curren...
state_exit()                  state_exit()          state TimerState          {             state_entry()               { ...
state_exit()                  state_exit()    integer g_listen_control;    state ListenState    {       state_entry()     ...
V. Math Functions   Trigonometry            Trigonometry         Vector      Rotation   llAbs                   llFrand   ...
String and List Functions    String                            List                 List    llBase64ToString              ...
Communication                In World                                                Messaging                llListen    ...
Inventory Functions        Inventory                               Inventory        llAllowInventoryDrop                  ...
Fortune-    VI. Tutorial: The Fortune-    Telling Magic Answer Ball   How about a bell that responds to yes or no question...
Picking a Random AnswerIt contains the bare minimum functionality: generating answer.It has a global list of answers and a...
Provide a Random          Answer When Touched// The global variable holding all the answerslist answers = [   // positive ...
Provide a Random          Answer When Touched // pick a random answer from the list of answers and return it string genera...
Provide a Random          Answer When Touched default {   touch_start(integer total_number)   {     llOwnerSay(generateAns...
Listening to ResidentsIt listens for messages on the public channel and generatesanswers in response.In the state_entry ev...
Provide a Random Personalized   Answer on the Public Channel default {   state_entry()   {     // listen on the public cha...
Cleaning up and PolishingIt adds two new global variables, a question prefix, and somehelpful instructions.Two new functio...
Provide a Random Personalized   Answer on the Public Channel    // Script will only answer qestions prefixed by this strin...
Provide a Random Personalized   Answer on the Public Channel  // return TRUE if str starts with prefix.  integer startsWit...
Provide a Random Personalized   Answer on the Public Channel// Return TRUE if candidate is a question, which is to say, if...
Provide a Random Personalized   Answer on the Public Channeldefault{  state_entry()  {    // listen on the public chat cha...
Provide a Random Personalized     Answer on the Public Channel    …    listen(integer channel, string name, key id, string...
Webgrafia   Linden Script Language Tutorial (2007).   http://xahlee.org/sl/ls.html   The Second Life LSL Portal (2008).   ...
Bibliografia “Creating your world: the official guide to advanced content creation for Second Life” By A. Weber, K. Rufer-...
Upcoming SlideShare
Loading in …5
×

Lsl scripts

626 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
626
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lsl scripts

  1. 1. Linden Script Language - LSL by Stefano CacciaguerraStefano Cacciaguerra Ph.D in Computer Science 1
  2. 2. Outline I. Basic Script Concept II. Constants, Variables and Data Type III. Flow-Control Statement IV. States V. Functions VI. Events Handler VII. TutorialStefano Cacciaguerra Ph.D in Computer Science 2
  3. 3. I. What is LSL? LSL is language used to attach behaviors to the objects It has syntax similar to Java, with an implicit state machine for every script. Multiple scripts may also be attached to the same object, allowing a style of small, single-function scripts to evolve. This leads to scripts that perform specific functions and allows them to be combined to form new behaviors.Stefano Cacciaguerra Ph.D in Computer Science 3
  4. 4. What is LSL? The script is compiled into an executable byte code. This byte code is then run within a virtual machine on the simulator. Each script receives a time slice of the total simulator time allocated to scripts in order to guarantee fairness. Each script executes within its own chunk of memory, preventing scripts from writing into other protected memory.Stefano Cacciaguerra Ph.D in Computer Science 4
  5. 5. Creating the Hello Script default { This script says “Hello, state_entry() Avatar!” when it enters { some state, and it says llSay(0, "Hello, Avatar!"); “Touched.” when it is } touched. touch_start(integer total_number) { llSay(0, "Touched."); } }Stefano Cacciaguerra Ph.D in Computer Science 5
  6. 6. Default State default { ... } All scripts have a simple implicit state machine with one or more states. They must have a default state. When a script is first started or reset, it will start out in the default state.Stefano Cacciaguerra Ph.D in Computer Science 6
  7. 7. Default State Every time you enter a state, the script engine calls the state_entry() event and execute the code found there. On state exit, the script engine calls the state_exit() event. In our example, we call the llSay() function in state_entry(). The state entry and exit handlers are a convenient place to initialize state data and clean up state specific data.Stefano Cacciaguerra Ph.D in Computer Science 7
  8. 8. Functions The language has over 200 built in functions which allow scripts and objects to interact with their environment. All of the built in functions start with ll. The example calls the llSay() function twice, which is used to emit text on the specified channel. llSay( integer channel, string text );Stefano Cacciaguerra Ph.D in Computer Science 8
  9. 9. Functions 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 are not sent to avatars but other scripts can listen for.Stefano Cacciaguerra Ph.D in Computer Science 9
  10. 10. Touch Event touch_start(integer total_number) { llSay(0, "Touched."); } There are many events that can be detected in your scripts by declaring a handler. The touch_start() event is raised when a user touches the object through the user interface.Stefano Cacciaguerra Ph.D in Computer Science 10
  11. 11. Save Save the script by clicking on Save. During the saving process, the editor will • save the text of the script • compile the script into bytecode • and then save that. When you see message “Compile successful!” in the preview window, you know the compile and save is done.Stefano Cacciaguerra Ph.D in Computer Science 11
  12. 12. Test the script Apply it to an object in the world. Create a new object in the world by context clicking in the main world view and selecting Create. Create a simple primitive by clicking in the world. Once the object appears, you can drag your newly created script onto the object to start the script. Soon, you will see the message Object: Hello Avatar!Stefano Cacciaguerra Ph.D in Computer Science 12
  13. 13. II. Comments Comment your scripts: • at the start of the script to explain the purpose of the script • before every global variable to describe what it holds • before every global function to describe what it does • wherever the code solves a specific problem. LSL uses Java/C++ style single line comments. // This script toggles a the rotation of an objectStefano Cacciaguerra Ph.D in Computer Science 13
  14. 14. Example// g_is_rotating stores the current state of rotation. TRUE is rotating else 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,1>, 0, 0); g_is_rotating = FALSE; } else { // rotate around the positive z axis - up. llTargetOmega(<0,0,1>, 4, 1); g_is_rotating = TRUE; } }}Stefano Cacciaguerra Ph.D in Computer Science 14
  15. 15. Arithmetic Operations // variables to hold a information about the target key g_target; vector g_target_postion; Assignment = float g_target_distance; // function that demonstrates assignment equality == set_globals(key target, vector pos) { inequality !=‘ 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); }Stefano Cacciaguerra Ph.D in Computer Science 15
  16. 16. Hexadecimal Entry Integers may be entered in hex form (e.g. 0xffff) starting it with “0x”: integer Mask = 0xff; // Equivalent to integer Mask = 255 integer Bit = 0x0100; // Equivalent to integer Mask = 256Stefano Cacciaguerra Ph.D in Computer Science 16
  17. 17. Binary Arithmetic Operators Operator Meaning + Addition - Subtraction * Multiplication / Division % Modulo (remainder) ^ Exclusive OR << Shift Left >> Shift Right ++ and -- Increment and DecrementStefano Cacciaguerra Ph.D in Computer Science 17
  18. 18. Boolean OperatorsOperator Operator returns< True if the LX is less than the DX> True if the LX is greater than the DX<= True if the LX is less than or equal to the DX>= True if the LX is greater than or equal to the DX&& True if the LX and DX hand side are both true Operator returns True if either the LX or DX are True|| Unary operator returns the logical negation of the! expression to the DXStefano Cacciaguerra Ph.D in Computer Science 18
  19. 19. Bitwise Operators A bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits integer mask = AGENT | SCRIPTED | ACTIVE; Operator Operator returns & Returns the bitwise and of the LX and DX. | Returns the bitwise or of the LX and DX. Unary operator returns the bitwise complement of ~ the expression to the DX.Stefano Cacciaguerra Ph.D in Computer Science 19
  20. 20. LSL Types LSL provides a small set of basic types simple: integer: A signed, 32-bit integer value with valid range from -2147483648 to 2147483647. float: An IEEE 32-bit floating point value with values ranging from 1.175494351E-38 to 3.402823466E+38.Stefano Cacciaguerra Ph.D in Computer Science 20
  21. 21. Simple: Key key: A unique identifier can be used to reference objects and avatars in SL. Known as UUID (Universal Unique IDentifier). The special constant NULL_KEY can be used to represent no key, which is to say there is no object. It is impossible for a script to change an item’s key key nokey = NULL_KEY; key owner = llGetOwner();Stefano Cacciaguerra Ph.D in Computer Science 21
  22. 22. Composite: Vector vector: 3 floats that are used together as a single item. Each component can be accessed via .x, .y, and .z. ZERO_VECTOR is equal to <0.0, 0.0, 0.0> Operator Operator returns + Add two vectors together - Subtract one vector from another * Vector dot product (scalar product) % Vector cross product (vector product)Stefano Cacciaguerra Ph.D in Computer Science 22
  23. 23. Example vector p = llGetPos(); // Get the object’s position in the sim v1= < x1, y1, z1> and v2= < x2, y2, z2> v3 = v1 + v2 = <x1+x2, y1+y2, z1+z2> similarly for “–” a1 = v1 * v2 = x1*x2 + y1*y2 + z1*z2 is the angle between v1 and v2 v5 = v1 % v2 = <y1z2 - z1y2, z1x2 - x1z2, x1y2 - y1x2> is the vector perpendicular to both v1 and v2 v6 = a * v1 = <a*x1, a*y1, a*z1>Stefano Cacciaguerra Ph.D in Computer Science 23
  24. 24. Composite: Rotation rotation: A quaternion, 4 floats that are used together as a single item to represent a rotation. Each component can be accessed via .x, .y, and .z and .s. 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 secondStefano Cacciaguerra Ph.D in Computer Science 24
  25. 25. Example Rotation zero = ZERO_ROTATION zero= <0.0, 0.0, 0.0, 1.0> // to represent no rotation vector r = llGetRot(); // Get the object’s rotationStefano Cacciaguerra Ph.D in Computer Science 25
  26. 26. Composite: List list: A heterogeneous list of the other data types. Lists are created via comma separated values of the other data types enclosed by [ and ]. The empty list is represente by [] string StringVar = "Hello, Carbon Unit"; rotation ZERO_ROTATION = <0,0,0,1>; list MyList = [ 1234, ZERO_ROTATION, StringVar ]; => [ 1234, <0,0,0,1>, "Hello, Carbon Unit" ]Stefano Cacciaguerra Ph.D in Computer Science 26
  27. 27. Examples MyList = 3.14159 + MyList; => [ 3.14159, 1234, <0,0,0,1>, "Hello, Carbon Unit" ] MyList = MyList + MyList; => [ 3.14159, 1234, <0,0,0,1>, "Hello, Carbon Unit", 3.14159, 1234, <0,0,0,1>, "Hello, Carbon Unit" ]Stefano Cacciaguerra Ph.D in Computer Science 27
  28. 28. Accessing Item Inside a List Items are accessed by llList2Integer, llList2String, llList2Float and so on. Items in the lists are indexed starting at 0 list clothes = [“shirt”, “hat”, “coat”, “pants”, “socks”] string s; s = llList2String(clothes, 0) // s is the shirt s = llList2String(clothes, 4) // s is the coatStefano Cacciaguerra Ph.D in Computer Science 28
  29. 29. Accessing Item Inside a List Lists are immutables, once created the value in the list is not possible to change it. It is possible to build a new list with llList2List. It takes three arguments the list, a start index and an end index list clothes = [“shirt”, “hat”, “coat”, “pants”, “socks”] clothes = llList2List(clothes, 0, 0) + [“belt”] + llList2List(clothes, 2, 4) clothes = [“shirt”, “belt”, “coat”, “pants”, “socks”]Stefano Cacciaguerra Ph.D in Computer Science 29
  30. 30. Composite: String A string could hold text enclosed by “ and ”. Operator Operator returns + Concatenation == Equality != InequalityStefano Cacciaguerra Ph.D in Computer Science 30
  31. 31. Type Conversion implicit type casts: • integer to float • string to key. explicit type casts: • integer to string • float to List • string to float • float to Integer • Key to List • string to vector • float to string • string to List • string to rotation • vector to string • vector to List • rotation to string • rotation to List • Integer to List • string to IntegerStefano Cacciaguerra Ph.D in Computer Science 31
  32. 32. Global Functions Global functions are also declared much like Java/C, with the exception that no void return value exists: make_physical_and_spin(vector torque) { // double the torque vector double_torque = 2.0*torque; llSetStatus(STATUS_PHYSICS, TRUE); llApplyTorque(double_torque); }Stefano Cacciaguerra Ph.D in Computer Science 32
  33. 33. Global Variables Global variables and functions are accessible from anywhere in the file. Only one declaration may be made per line: vector gStartPosition; Global variables may also be initialized if desired, although uninitialized global and local variables are initialized to legal zero values: vector gStartPosition = <10.0,10.0,10.0>Stefano Cacciaguerra Ph.D in Computer Science 33
  34. 34. Local Variables Local variables are scoped below their declaration within the block of code they are declared in Local variables may be declared within any block of code.Stefano Cacciaguerra Ph.D in Computer Science 34
  35. 35. Local Variables integer test_function() { // Test vector that we can use anywhere in the function vector test = <1,2,3>; 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, j, j>; } // this test fails if (test == <9,9,9>) { // never reached} }Stefano Cacciaguerra Ph.D in Computer Science 35
  36. 36. III. Conditional StatementsThe if statement as the Java/C version.check_message(string message){ if(message == "open") { open(); } else if(message == "close") { close(); } else { llSay(0, "Unknown command: " + message); }}Stefano Cacciaguerra Ph.D in Computer Science 36
  37. 37. Conditional StatementsThe NULL_KEY constant counts as FALSE.There can be zero or more else if statements, and anoptional final else.The usual set of integer arithmetic and comparison operators .// 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.Stefano Cacciaguerra Ph.D in Computer Science 37
  38. 38. Conditional Statementsassess_next_step(integer perm, integer attached, integer balance, float dist){ string msg; if(!attached) { if((perm & PERMISSION_ATTACH) && (dist < 10.0)) { attach(); } else if((dist > 10.0) || ((dist > 20.0) && (balance > 1000))) { move_closer(); } else { llRequestPermissions(llGetOwner(), PERMISSION_ATTACH); } }}Stefano Cacciaguerra Ph.D in Computer Science 38
  39. 39. for loopA for loop is most useful for when you know how many timesyou 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, andthe increment step is performed at the end of every loop.Stefano Cacciaguerra Ph.D in Computer Science 39
  40. 40. for 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.0, 0.0, step_distance>; vector base_pos = llGetPos(); integer i; for(i = 0; i <= steps; ++i) { llSetPos(base_pos + i * offset); llSleep(0.1); } }Stefano Cacciaguerra Ph.D in Computer Science 40
  41. 41. do- do-while loopThe do-while loop construct is most useful when you are surethat you want to perform an operation at least once, but you arenot sure how many times you want to loop. The syntax is the same as Java and C program.A simple English translation would be do the code inside thecurly braces and continue doing it if the statement after thewhile is true.// output the name of all inventory items attached to this objectStefano Cacciaguerra Ph.D in Computer Science 41
  42. 42. do- do-while loop talk_about_inventory(integer type) { string name; integer i = 0; integer continue = TRUE; do { name = llGetInventoryName(type, i); if (llStringLength(name) > 0) { llSay(0, "Inventory " + (string)i + ": " + name); } else { llSay(0, "No more inventory items"); continue = FALSE; } i++; } while(continue); }Stefano Cacciaguerra Ph.D in Computer Science 42
  43. 43. while loop The while loop allows you to exit the loop without doing a single iteration inside. mention_inventory_type(integer type) { integer i = llGetInventoryNumber(type); while(i--) { llSay(0, "item: " + llGetInventory(i)); } }Stefano Cacciaguerra Ph.D in Computer Science 43
  44. 44. State Change State change allow you to move through the LSL virtual machines flexible state machine by transitioning your script to and from user defined states and the default state. You can define your own script state by placing the keyword state before its name and enclosing the event handlers with open and close curly braces ({ and }.) Invoke the transition to a new state by calling it with the syntax: state <statename>.Stefano Cacciaguerra Ph.D in Computer Science 44
  45. 45. State Change default { state_entry() { llSay(0, "I am in the default state"); llSetTimer(1.0); } timer() { state SpinState; } }Stefano Cacciaguerra Ph.D in Computer Science 45
  46. 46. State Change state SpinState { state_entry() { llSay(0, "I am in SpinState!"); llTargetOmega(<0,0,1>, 4, 1.0); llSetTimer(2.0); } timer() { state default; } state_exit() { llTargetOmega(<0,0,1>, 0, 0.0); } }Stefano Cacciaguerra Ph.D in Computer Science 46
  47. 47. IV. States All scripts must have a default state, which is the first state entered when the script starts. States contain event handlers that are triggered by the LSL virtual machine. All states must supply at least one event handler! When state changes: • all callback settings are retained • and all pending events are cleared.Stefano Cacciaguerra Ph.D in Computer Science 47
  48. 48. state_entry()The state_entry event occurs whenever a new state is entered:• including program start,• and is always the first event handled.No data is passed to this event handler.Stefano Cacciaguerra Ph.D in Computer Science 48
  49. 49. Warning: state_entry() It is a mistake to assume that the state_entry() callback is called when you rez an object out of your inventory. When you derez an object into your inventory the current state of the script is saved, so there will not be a call to state_entry() during the rez. If you need to provide startup code every time an object is created, you should create a global function and call it from both state_entry() and the on_rez() callbacks.Stefano Cacciaguerra Ph.D in Computer Science 49
  50. 50. Example // global initialization function. init() { // Set up a listen callback for whoever owns this object. key owner = llGetOwner(); llListen(0, "", owner, ""); }Stefano Cacciaguerra Ph.D in Computer Science 50
  51. 51. Example default { state_entry() { init(); } on_rez(integer start_param) { init(); } listen(integer channel, string name, key id, string message) { llSay(0, "Hi " + name + "! You own me."); } }Stefano Cacciaguerra Ph.D in Computer Science 51
  52. 52. state_exit() state_exit()Provide a state_exit() if you need to clean up any eventsrequested in the current state, but do not expect in the next statedefault{ state_entry() { state TimerState;}}The state_exit() handler is not called when an object is beingdeleted - all callbacks, handlers, sounds, etc, will be cleaned upautomatically for you.Stefano Cacciaguerra Ph.D in Computer Science 52
  53. 53. state_exit() state_exit() state TimerState { state_entry() { // set a timer event for 5 seconds in the future. llSetTimerEvent(5.0); } timer() { llSay(0, "timer"); state ListenState; } state_exit() { // turn off future timer events. llSetTimerEvent(0.0); } }Stefano Cacciaguerra Ph.D in Computer Science 53
  54. 54. state_exit() state_exit() integer g_listen_control; state ListenState { state_entry() { // listen for anything on the public channel g_listen_control = llListen(0, "", NULL_KEY, ""); } listen(integer channel, string name, key id, string message) { llSay(0, "listen"); state TimerState; } state_exit() { // turn off the listener llListenRemove(g_listen_control); } }Stefano Cacciaguerra Ph.D in Computer Science 54
  55. 55. V. Math Functions Trigonometry Trigonometry Vector Rotation llAbs llFrand llVecDist llAngleBetween llAcos llPow llVecMag llAxes2Rot llAsin llRound llVecNorm llAxisAngle2Rot llAtan2 llSin llEuler2Rot llCeil llSqrt llRot2Angle llCos llTan llRot2Axis llFabs llRot2Euler llFloor llRot2Fwd llRot2Left llRot2Up llRotBetweenStefano Cacciaguerra Ph.D in Computer Science 55
  56. 56. String and List Functions String List List llBase64ToString llCSV2List llList2String llDeleteSubString llDeleteSubList llList2Vector llGetSubString llGetListEntryType llListFindList llInsertString llGetListLength llListInsertList llMD5String llList2CSV llListRandomize llStringLength llList2Float llListSort llSubStringIndex llList2Integer llParseString2List llStringToBase64 llList2Key llToLower llList2List llToUpper llList2ListStrided llXorBase64Strings llList2RotStefano Cacciaguerra Ph.D in Computer Science 56
  57. 57. Communication In World Messaging llListen llEmail llListenControl llGetNextEmail llListenRemove llInstantMessage llSay llShout llWhisperStefano Cacciaguerra Ph.D in Computer Science 57
  58. 58. Inventory Functions Inventory Inventory llAllowInventoryDrop llGetNotecardLine llGetInventoryCreator llGiveInventory llGetInventoryKey llGiveInventoryList llGetInventoryName llRemoveInventory llGetInventoryType llRequestInventoryData llGetInventoryNumber llRezObject llGetInventoryPermMask llRezAtRootStefano Cacciaguerra Ph.D in Computer Science 58
  59. 59. Fortune- VI. Tutorial: The Fortune- Telling Magic Answer Ball How about a bell that responds to yes or no questions spoken on the public chat channel? Versions: I Picking a Random Answer II Listening to Residents III Cleaning up and PolishingStefano Cacciaguerra Ph.D in Computer Science 59
  60. 60. Picking a Random AnswerIt contains the bare minimum functionality: generating answer.It has a global list of answers and a function that picks arandom entry from the list and returns it, on touched eventgenerationStefano Cacciaguerra Ph.D in Computer Science 60
  61. 61. Provide a Random Answer When Touched// The global variable holding all the answerslist answers = [ // positive answers "Yes.", "It is certain.", "All signs point to yes.", "You may rely on it", "Without a doubt.", // negative answers "No.", "Absolutely not.", "Doubtful.", "Most assuredly not.", "My sources say no.", //non commital answers "Outlook hazy.", "Uncertain.", "The answer is unknowable.", "Id better not tell you now.", "Cannot predict that at this time."];Stefano Cacciaguerra Ph.D in Computer Science 61
  62. 62. Provide a Random Answer When Touched // pick a random answer from the list of answers and return it string generateAnswer() { // generate a random number between 0 and the length of the // list (but not exactly the length of the list), then cast it // to an integer. This will give us a random number betwen 0 // and the length of the list - 1. integer answerIndex = (integer)llFrand(llGetListLength(answers)); // return the answerIndexth item of the list answers return llList2String(answers, answerIndex); }Stefano Cacciaguerra Ph.D in Computer Science 62
  63. 63. Provide a Random Answer When Touched default { touch_start(integer total_number) { llOwnerSay(generateAnswer()); } }Stefano Cacciaguerra Ph.D in Computer Science 63
  64. 64. Listening to ResidentsIt listens for messages on the public channel and generatesanswers in response.In the state_entry event, the script calls the built-in functionllListen and asks for a listen event to be raised wheneverthere is a message spoken on channel 0.In the listen event, the script uses the name parameter topersonalize its response.Stefano Cacciaguerra Ph.D in Computer Science 64
  65. 65. Provide a Random Personalized Answer on the Public Channel default { state_entry() { // listen on the public chat channel for any text from // anyone or anything. llListen(0, "", NULL_KEY, ""); } listen(integer channel, string name, key id, string message) { // script heard something, answer llSay(0, generateAnswer() + name); } }Stefano Cacciaguerra Ph.D in Computer Science 65
  66. 66. Cleaning up and PolishingIt adds two new global variables, a question prefix, and somehelpful instructions.Two new functions:• isQuestion which will return true if it’s argument is a question directed at the script• startsWith a utility function that takes two arguments and return true if the first argument starts with the second.• In this code we add a new touch_start event handler that displays the help text when the object is touched.Stefano Cacciaguerra Ph.D in Computer Science 66
  67. 67. Provide a Random Personalized Answer on the Public Channel // Script will only answer qestions prefixed by this string string questionPrefix = "Magic Ball, "; // provide these instructions with touched // script will add the questionPrefix string to this string string instructions = "Ask me a yes or no question starting with "; // The global variable holding all the answers list answers = [ … ];Stefano Cacciaguerra Ph.D in Computer Science 67
  68. 68. Provide a Random Personalized Answer on the Public Channel // return TRUE if str starts with prefix. integer startsWith(string str, string prefix) { // get the substring of the string, from 0 to the length // of the prefix, and compare this substring to prefix return llGetSubString(str, 0, llStringLength(prefix) - 1) == prefix; }Stefano Cacciaguerra Ph.D in Computer Science 68
  69. 69. Provide a Random Personalized Answer on the Public Channel// Return TRUE if candidate is a question, which is to say, if// the candidate begins with the question prefix. This function// is case-insensitive (it ignores differences between upper and// lower case in the candidate and prefix)integer isAQuestion(string candidate){ return startsWith(llToUpper(candidate), llToUpper(questionPrefix));}Stefano Cacciaguerra Ph.D in Computer Science 69
  70. 70. Provide a Random Personalized Answer on the Public Channeldefault{ state_entry() { // listen on the public chat channel for any text // from anyone or anything. llListen(0, "", NULL_KEY, ""); } touch_start(integer total_number) { // provide instructions llSay(0, instructions + """ + questionPrefix + """); } …Stefano Cacciaguerra Ph.D in Computer Science 70
  71. 71. Provide a Random Personalized Answer on the Public Channel … listen(integer channel, string name, key id, string message) { // script heard something, if its a question for // this script, answer it. if (isAQuestion(message)) { llSay(0, generateAnswer() + name); } }}Stefano Cacciaguerra Ph.D in Computer Science 71
  72. 72. Webgrafia Linden Script Language Tutorial (2007). http://xahlee.org/sl/ls.html The Second Life LSL Portal (2008). http://wiki.secondlife.com/wiki/LSL_Portal Kan-ed – Using the Linden Script language (2005) http://www.kan-ed.org/second-life/using-LSL.html Linden Scripting Language Guide (2008) Help Dialog in SL Client: Scripting LanguageStefano Cacciaguerra Ph.D in Computer Science 72
  73. 73. Bibliografia “Creating your world: the official guide to advanced content creation for Second Life” By A. Weber, K. Rufer-Bach and R. Platel, Wiley editorStefano Cacciaguerra Ph.D in Computer Science 73

×