LSL Code Examples

By Xah Lee. Date: .

Simple Examples

The following examples are just a few lines each, and easy to understand. They show you how some functions are used, or how certain tasks are done.

// Description: float a text above the object.

default
{
    state_entry()
    {
        llSetText("I ♥ ya.", <0,1,0>, 1);
    }
}

// Note: deleting the script won't delete the floating text.  To
// delete the floating text, change the string to a empty string
// "". Compile the script. Then, you can delete the script.  This is
// so because floating text is considered a property of the prim.
// Once a prim has a property set, it stays.  Some functions, such as
// llSetText, is used to change the prim's property.

// There is no clear documentation on what exactly is considered as a
// prim's property. This example illustrate that LSL coding is very
// much a experience and trial'n'error activity.
// Description: touch to give a notecard.
// The object should have a item named “Store Notes”.

default
{
    touch_start(integer num_detected)
    {
        integer i;
        for(i = 0; i < num_detected; ++i)
        {
            llGiveInventory(llDetectedKey(i), "Store Notes");
        }
    }
}
// Description: This is a drop box.
// Good for comments, item transfers, etc.
default {
    state_entry() {
        llSetText("Drop a notecard on me.", <1,0,0>, 1);
        llAllowInventoryDrop(TRUE);
    }
}
// Description: When stepped on, IM the owner.
 default {
     state_entry()
     {
         llSetText("Step on me please.", <0,1,0>, 1);
     }
     collision_start(integer num_detected) 
     {
         llSay(0, llDetectedName(0) + " stepped on me!");
         llOwnerSay(llDetectedName(0) + " stepped on me!");
         llInstantMessage(llGetOwner(), llDetectedName(0) + " stepped on me!");
     }
 }
// Put this script in object, then, type “/8 die” will delete the object.
 default{
     state_entry()
     {
         llListen(8, "", llGetOwner(), "die");
     }

     listen(integer channel, string name, key id, string msg)
     {
         llDie();
     }
 }
// Description: sample script of using llRezObject
// put a object named myObj in the prim.
// Upon touch, it rezes myObj 2 meters above the original object.

 default {
     touch_start(integer total_number) {
         llRezObject("myObj", llGetPos()+<0,0,2>, ZERO_VECTOR, ZERO_ROTATION, 0);
     }
 }
// Description: sample use of the timer function.
// This script changes the color of the prim periodically

 default { state_entry() { llSetTimerEvent(3); }

     timer() {
         llSetColor(<llFrand(1),llFrand(1),llFrand(1)>,0);
         llSetColor(<llFrand(1),llFrand(1),llFrand(1)>,1);
         llSetColor(<llFrand(1),llFrand(1),llFrand(1)>,2);
         llSetColor(<llFrand(1),llFrand(1),llFrand(1)>,3);
         llSetColor(<llFrand(1),llFrand(1),llFrand(1)>,4);
         llSetColor(<llFrand(1),llFrand(1),llFrand(1)>,5);
     }
 }
// Description: make the obj rotate using Omega
// This means, the object visually rotates, but not physically

 default {
     state_entry() {
         // the parameters are: axis, spin rate (radians per second), “gain”
         llTargetOmega( llVecNorm(<0,0,1>), 2*PI/360*20 , 1.);
     }
 }
// Description: On touch, change the prim's texture by cycling thru
// the textures in the prim.

// the index to textureNames that is the current texture used.
integer ii=0; 

 default
 {
     touch_start(integer total_number)
     {
         integer numberOfTextures = llGetInventoryNumber(INVENTORY_TEXTURE);

         integer i = 0;
         for(i; i<total_number; ++i)
         {
             llSetTexture(llGetInventoryName(INVENTORY_TEXTURE, ii), ALL_SIDES);
             ii = ((ii+2) % numberOfTextures) - 1;
         }
     }
 }
// Upon touch, play a sound file. Touch again to stop.
// The sound file must be a “.wav” file and must be less than 10
// seconds in duration.

string soundFile = "Disdain Female"; // this is in your Library:Sound folder
float soundVolume = .5;

integer onoff = 1;

 default { 
     touch_start(integer num_detected)
     {
         integer i = 0;
         for(i; i<num_detected; ++i)
         {
             if (onoff == 1) {
                 llStopSound();
                 onoff = 0;
             } else {
                 llLoopSound(soundFile, soundVolume);
                 onoff = 1;
             }
         }
     }
 }

All scripts here Copyright © 2007 by Xah Lee. Permission is granted for use or modification, but please link back to this site.