Archive for the Category »programming «

Scripting in Second Life

I’d like to post a little about scripting and making objects react to touching. Would be very helpful if you did some programming in another programming language first. But I know many people who are scripting in second life without previous experience.

Here is a link to the main LSL scripting wiki where you can learn stuff on your own! You can also attend one of the Teazers classes on scripting to understand the basics too. I also found a text editor for LSL when combined with lslint it checks for any errors in the code.

The first thing to understand is how the state system is used. A state makes it easy for a script to do different things in different situations. A script for a chair can be in a state called empty when no one is sitting in it. If someone sits in it then it can change to an occupied state. In the following example there is one state called default. Every script starts in the default state. I have placed remarks in the example. The remarks start with // and end at the end of the line. The function llSay() sends a string to a channel. Channel 0 is the main chat you see all the time. Don’t worry about other channels right now.

default
{
state_entry()
{
// first line that gets exectuted in script
llSay(0, “Starting up script” );
}
}

This script would say in chat, “Starting up script” once you save the script or reset it. Remember: just taking an object out of your inventory and placing it on the ground, Called Rezzing, does not reset the script! There are ways to do this but that’s for another post. To define another state you type state before the statename. The default state does not need the state keyword in front of it. To change the state of a script you would type: state statename;

You will see some more functions in this example. When an object gets touched there is code that you can put in to a script to catch that touch and do something about it. The touch_start() function will execute when object gets touched. Don’t worry about the integer total_number because we don’t use it. Here’s the example of using a state with a lamp:

default
{
state_entry()
{
// first line that gets exectuted in script
llSay(0, “Starting up script in default state” );
state lamp_off; // Change state to lamp_off
}
}
state lamp_off {
state_entry()
{
llSay(0, “I am Off!” ); // Will say in chat when changes to off state
}
touch_start()
{
state lamp_on; // Change to lamp_on state
}
}
state lamp_on
{
state_entry()
{
llSay(0, “I am On!” ); // Will say in chat when changes to on state
}
touch_start()
{
state lamp_off; // Change to lamp_off state
}
}

If you put this script in any object it will first say “Starting up script in default state” then change the state to lamp_off which will make it say “I am Off.” If someone touches the lamp then it will change the state to lamp_on which will make it say “I am On.” When it gets touched again it will change the state to off and so on until the object gets deleted or put back into your inventory.

One thing I want to let you know before I stop are some technical stuff. Timers will stay on and not reset when state changes. When using link messages to send or receive info from another script or object, the messages can get lost in between state changes. Each state has it’s own message listener so if you are expecting a link message from another script then you shouldn’t change states until you receive the message back. I ran into this when I was changing colors for a box. Each state should have it’s own timer function when using timers.