User:Fred Gandt/Tuition

From Second Life Wiki
< User:Fred Gandt
Revision as of 01:09, 14 February 2010 by Fred Gandt (talk | contribs) (Added example for demonstrating scopes and variables)
Jump to navigation Jump to search
FG jpg.jpg

Some Complete Scripts for Use and Abuse

Free Scripts (content constantly updating)

Various Scripting Examples for Tuition

Over time I will be creating a full course starting here to teach the basics of LSL scripting. I will be accompanying the posted examples with text explanation, videos and in-world tuition. The course will take quite some time to produce fully since I enjoy scripting for my own amusement most of the time I am logged in so, unless I can invent an LSL powered time machine I will be creating this course in between projects.

User Created Functions

LSL allows us to create functions to run by calling those functions within the script runtime. They are an extremely useful way to help organize a script, keep the memory use down and simplify code. Some simple ways to create and use this ability are shown below...

THIS IS NOT A SINGLE SCRIPT.

<lsl>// This first example shows the most basic syntax used to create a function.

Function() // This is the name we choose for our function. We use the name to call its use. {

   ;

}

default {

   state_entry()
   {
       Function(); // This is how we call for the function to run.
   }
   touch_start(integer nd)
   {
       Function(); // From anywhere the function can be called.
   }

}

// So we add the actions we desire within the function braces.

Function() {

   llOwnerSay("I worked!!");

}

default {

   state_entry()
   {
       Function();
   }
   touch_start(integer nd)
   {
       Function();
   }

}

// We can add conditions inside the function and pass to it any type of information.

Function(integer i) // Now the function carries information to be processed. {

   if(i) // We can check the information as the function runs.
   llOwnerSay("I worked!!");
   else
   llOwnerSay("I still worked!!");

}

default {

   state_entry()
   {
       Function(TRUE); // Now when we call the function we MUST provide the information the function NEEDS to run
   }                                              // (even if the function doesn't always use it).
   touch_start(integer nd)
   {
       Function(FALSE);
   }

}

// We can give the function a type and have it return the result dependent on what we feed it.

string Function(integer i, integer q) {

   integer r = (i + q);
   return ((string)r); // This style MUST return the same type of info as the function type.

}

default {

   state_entry()
   {  // Now the function can be used in-line as it returns the type of info we want to use directly to where it is called.
       llOwnerSay(Function(5, 5));
   }
   touch_start(integer nd)
   {
       integer i = 5;
       integer q = (i*5);
       llOwnerSay(Function(i, q));
   }

}</lsl>

Scopes and Variables

This is to demonstrate where a variable may and may not be referenced within a script. It also demonstrates a basic view of what dictates a scope. A scope is an area within a script that is a parent and/or a child of another area. There will be more in-depth explanation as time goes on.

<lsl>integer global_variable = TRUE; // This variable can be referenced throughout the script (in any state).

default { // No variable can be created to exist throughout the state (use a global variable).

   state_entry()
   {
       integer event_variable = TRUE; // Events can have local variables.
       if(global_variable) // A global variable can be referenced in any child scope in the script.
       if(event_variable) // The event variable can be referenced anywhere within the event it is created in.
       {
           integer conditional_variable_1 = TRUE; // If the event has child scopes new local variables can be created for them.
           if(global_variable)
           if(event_variable)
           if(conditional_variable_1)
           {
               integer conditional_variable_2 = TRUE; // Each local scope can have its own unique variables.
               if(global_variable)
               if(event_variable)
               if(conditional_variable_1)
               if(conditional_variable_2)
               {
                   integer conditional_variable_3 = TRUE;
                   if(global_variable)
                   if(event_variable)
                   if(conditional_variable_1)
                   if(conditional_variable_2)
                   if(conditional_variable_3)
                   {
                       integer conditional_variable_4 = TRUE;
                       if(global_variable)
                       if(event_variable)
                       if(conditional_variable_1)
                       if(conditional_variable_2)
                       if(conditional_variable_3)
                       if(conditional_variable_4)
                       llOwnerSay("Yikes!"); // We made it through!!
                   }
               }
           }
       } // No variable created within a child scope can be referenced outside that scope.
   }

}</lsl>