What is a Variable?
A variable is a named container that holds a value. You give it a name, declare what kind of data it holds, and then you can read or change that value throughout your script.
The syntax for declaring a variable in LSL is:
type name = value;
For example:
integer score = 0;
string playerName = "Blackthorn";
float speed = 2.5;
LSL has six data types. Each stores a different kind of information. We'll cover each one in this class.
integer
An integer stores a whole number — no decimal point. It can be positive, negative, or zero. The range in LSL is roughly −2 billion to +2 billion.
default
{
state_entry()
{
integer lives = 3;
integer score = 0;
integer penalty = -10;
score = score + 50; // score is now 50
lives = lives - 1; // lives is now 2
llOwnerSay("Score: " + (string)score);
llOwnerSay("Lives: " + (string)lives);
}
}
Notice the (string) before each variable. To include a variable in a chat message, you must convert it to a string first — we'll cover this in Step 8.
- + - * /
- Add, subtract, multiply, divide
- %
- Modulo — the remainder after division.
7 % 3= 1 - ++ --
- Increment / decrement by 1.
score++is the same asscore = score + 1
float
A float stores a decimal number. Use floats for things like speed, distances, time intervals, and colour values.
default
{
state_entry()
{
float speed = 3.5;
float duration = 10.0;
float result = speed * duration; // 35.0
llOwnerSay("Distance: " + (string)result + " meters");
// Timer functions expect a float
llSetTimerEvent(5.0);
}
}
Integer division truncates decimals. 7 / 2 = 3, not 3.5. Use floats when you need precision: 7.0 / 2.0 = 3.5
string
A string holds text. You wrap the text in double quotes. Strings can contain letters, numbers, spaces, and most symbols.
default
{
state_entry()
{
string firstName = "Blackthorn";
string lastName = "Resident";
string greeting = "Hello, " + firstName + " " + lastName + "!";
llSay(0, greeting);
// Says: Hello, Blackthorn Resident!
}
}
The + operator joins (concatenates) strings. You can join as many as you like in one expression.
vector
A vector stores three float values together: X, Y, and Z. Vectors are used for positions, scales, velocities, and — importantly — colours.
default
{
state_entry()
{
// A position in the sim
vector homePos = <128.0, 128.0, 25.0>;
// Colours use vectors: each component is 0.0 to 1.0
vector red = <1.0, 0.0, 0.0>;
vector yellow = <1.0, 1.0, 0.0>;
vector white = <1.0, 1.0, 1.0>;
// Set the object's colour to red
llSetColor(red, ALL_SIDES);
// Read individual components
float myX = homePos.x; // 128.0
float myZ = homePos.z; // 25.0
llOwnerSay("Height: " + (string)myZ);
}
}
Colour vectors in LSL use 0.0–1.0, not 0–255. To convert: divide your RGB value by 255. Red (255,0,0) becomes <1.0, 0.0, 0.0>.
rotation
A rotation stores an object's orientation in 3D space. It has four components: X, Y, Z, and S (the scalar). This is a quaternion — the maths can be complex, but you rarely need to understand the internals.
Most of the time you'll use llEuler2Rot to convert familiar degrees into a rotation value:
default
{
state_entry()
{
// ZERO_ROTATION = no rotation (identity)
rotation noRot = ZERO_ROTATION;
// Rotate 45 degrees around the Z axis using Euler angles
// DEG_TO_RAD converts degrees to radians
rotation spin45 = llEuler2Rot(<0.0, 0.0, 45.0> * DEG_TO_RAD);
llSetLocalRot(spin45);
llOwnerSay("Object rotated 45 degrees.");
}
}
Don't worry about understanding quaternion maths. Just use llEuler2Rot with X/Y/Z degrees and it does the conversion for you.
key
A key stores a UUID — a unique identifier that looks like this: 550e8400-e29b-41d4-a716-446655440000. Every object, avatar, texture, and sound in Second Life has a unique key.
You'll use keys constantly to refer to specific things:
default
{
state_entry()
{
// Get the key (UUID) of the object's owner
key ownerKey = llGetOwner();
llOwnerSay("Owner key: " + (string)ownerKey);
// Get the key of the object itself
key selfKey = llGetKey();
llOwnerSay("My key: " + (string)selfKey);
}
touch_start(integer num_detected)
{
// Get the key of whoever touched the object
key toucher = llDetectedKey(0);
key owner = llGetOwner();
if (toucher == owner)
{
llOwnerSay("Owner touched me.");
}
}
}
- NULL_KEY
- An empty/invalid key:
"00000000-0000-0000-0000-000000000000"
Type Casting
Sometimes you need to convert a value from one type to another. This is called type casting. In LSL you cast by putting the target type in parentheses before the value.
default
{
state_entry()
{
integer score = 42;
float speed = 3.5;
string name = "Blackthorn";
vector pos = <128.0, 128.0, 25.0>;
rotation rot = ZERO_ROTATION;
key ownerKey = llGetOwner();
// Cast to string for use in messages
llOwnerSay("Score: " + (string)score);
llOwnerSay("Speed: " + (string)speed);
llOwnerSay("Pos: " + (string)pos);
llOwnerSay("Owner: " + (string)ownerKey);
// Cast float to integer (truncates — does NOT round)
integer wholeSpeed = (integer)speed; // 3, not 4
llOwnerSay("Whole speed: " + (string)wholeSpeed);
// Cast string to integer
string numStr = "100";
integer numInt = (integer)numStr; // 100
llOwnerSay("Parsed: " + (string)numInt);
}
}
You now know all six LSL data types. Class 4 takes you into events — the mechanism that makes scripts come alive.