User Tools

Site Tools


en:content_creation:lsl_scripting:lsl_basics

LSL Scripting Basics

resident scripter

Learn the fundamentals of LSL (Linden Scripting Language) to create interactive objects in Caprica.

Introduction

LSL (Linden Scripting Language) is a scripting language used to add interactivity to objects in OpenSimulator-based grids like Caprica. With LSL, you can make objects move, respond to touch, play sounds, display text, and much more.

What You Can Do with LSL: * Make objects move or rotate * Play sounds and music * Display text above objects * Respond to user interactions (touch, sit, chat) * Communicate with other scripts * Create games and puzzles * Build interactive systems

Getting Started

Where to Write Scripts

In-World Script Editor: 1. Right-click an object 2. Select “Edit” → “Content” tab 3. Click “New Script” 4. The script editor will open

External Editors: * Use your preferred text editor * Copy/paste into in-world editor * Many viewers support external editing

Basic Script Structure

Every LSL script has this basic structure:

```lsl default {

  state_entry()
  {
      // Your code here
  }

} ```

Components: * default — The script's main state * state_entry() — Runs when script starts * {} — Code blocks * ; — Statement terminator

Hello World Example

Let's create your first script that says “Hello” when touched:

```lsl default {

  state_entry()
  {
      llSay("Hello World!");
  }
  touch_start(integer total_number)
  {
      llSay("You touched me!");
  }

} ```

What This Does: * Says “Hello World!” when script starts * Says “You touched me!” when someone touches the object

Deploying the Script: 1. Paste into script editor 2. Click Save 3. Close editor 4. The script will compile and run

Variables and Data Types

LSL supports several data types:

Common Types

Type Description Example
——————-——–
integer Whole numbers `integer x = 5;`
float Decimal numbers `float y = 3.14;`
string Text `string name = “John”;`
key UUID (unique ID) `key id = llGetKey();`
vector 3D coordinates `vector pos = <10,20,30>;`
rotation 3D rotation `rotation rot = <0,0,0,1>;`

Declaring Variables

```lsl integer count = 0; float price = 10.5; string message = “Welcome”; vector position = <0,0,0>; ```

Scope

* Global variables — Outside functions, accessible everywhere * Local variables — Inside functions, only in that function

Functions

Functions are blocks of code that perform specific tasks.

Built-in Functions

LSL has many built-in functions (ll* functions):

* `llSay(string msg)` — Says text in local chat * `llShout(string msg)` — Shouts text (heard at greater distance) * `llWhisper(string msg)` - Whispers text (only nearby) * `llGetPos()` — Returns object's position * `llSetPos(vector pos)` — Sets object's position * `llGetKey()` — Returns object's UUID * `llListen(integer channel, string name, string id)` — Listen for chat commands

Custom Functions

You can create your own functions:

```lsl string getGreeting() {

  return "Welcome to Caprica!";

}

default {

  state_entry()
  {
      llSay(getGreeting());
  }

} ```

Events

Events are triggered by specific actions. Functions with `event_` prefix handle these.

Common Events

Event Triggered When
——-—————
`state_entry()` Script starts or state changes
`state_exit()` Script ends or state changes
`touch_start()` Object is touched
`touch_end()` - Touch ends
`collision_start()` - Object collides with something
`collision_end()` — Collision ends
`listen(integer channel)` - Chat on listened channel
`timer()` - Timer fires

Event Parameters

Events often provide parameters:

```lsl touch_start(integer total_number) {

  // total_number = number of avatars touching
  llSay("Touched by " + (string)total_number + " people");

} ```

Conditional Logic

Use if/else statements for decision making.

If Statement

```lsl integer count = 5;

if (count > 10) {

  llSay("More than 10");

} else if (count == 10) {

  llSay("Exactly 10");

} else {

  llSay("Less than 10");

} ```

Comparison Operators

Operator Meaning Example
———-—————–
`==` Equal to `if (x == 5)`
`!=` Not equal to `if (x != 5)`
`>` Greater than `if (x > 5)`
`<` Less than `if (x < 5)`
`>=` Greater or equal `if (x >= 5)`
`⇐` Less or equal `use if (x ⇐ 5)`

Loops

Loops repeat code multiple times.

For Loop

```lsl integer i;

for (i = 0; i < 5; i++) {

  llSay("Count: " + (string)i);

} ```

This will count from 0 to 4.

While Loop

```lsl integer count = 0;

while (count < 5) {

  llSay("Count: " + (string)count);
  count++;

} ```

Vectors

Vectors represent 3D coordinates (X, Y, Z).

Vector Components

```lsl vector pos = <10, 20, 30>; float x = pos.x; 10 float y = pos.y; 20 float z = pos.z; 30 ``` ==== Vector Operations === ```lsl vector a = <1, 2, 3>; vector b = <4, 5, 6>; vector c = a + b; <5, 7, 9> vector d = a - b; ←3, -3, -3> ``` ===== Common LSL Patterns ===== ==== Moving Objects === ```lsl vector target = <10, 20, 30>; float speed = 1.0; llSetPos(target); llSetTimer(“move_back”, 5.0, FALSE); ``` ==== Playing Sounds === ```lsl string sound = “my_sound”; llPlaySound(sound, 1.0); ``` ==== Displaying Text ===== ```lsl llSetText(“Hello Caprica!”, <1,1,1>, 1.0); ``` ==== Listening for Commands === ```lsl integer CHANNEL = 0; default { state_entry() { llListen(CHANNEL, “”, “”, “”); } listen(integer channel, string name, string id, string message) { if (message == “hello”) { llSay(“Hello ” + name + “!”); } } } ``` ===== Debugging ===== ==== llOwnerSay vs llSay === * `llSay` — Everyone can see * `llOwnerSay` — Only owner can see (use for debugging) ==== Adding Debug Messages === ```lsl llOwnerSay(“Script started”); llOwnerSay(“Current state: ” + llGetScriptName()); ``` ===== Best Practices ===== ==== Organization === * Use comments to explain your code * Use meaningful variable names * Break complex tasks into functions - Keep scripts short and focused ==== Performance === * Avoid infinite loops * Use timers instead of busy-wait loops * Minimize listen() usage when not needed - Clean up listeners when done ==== Security === * Never hardcode sensitive data (passwords, keys) * Check permissions before allowing actions * Validate user input - Be careful with money transactions ==== Permissions === Objects have permissions that affect scripts: * Modify — Can edit the script * Copy — Can copy the script - Transfer — Can give/sell the script ===== Resources ===== ==== Built-in Functions Reference === The official LSL wiki has complete documentation: * All ll* functions * Event reference - Data types - Constants ==== Community Resources === * LSL Portal: https://wiki.secondlife.com/wiki/LSL_Portal * LSL Wiki: https://wiki.secondlife.com/wiki/LSL_Wiki * Community forums for help ==== Example Scripts === Many free scripts are available: * LSL Library - Free scripts in-world - Community repositories ===== Next Steps ===== Now that you understand LSL basics: * LSL Events — Deep dive into events * LSL Functions Reference — Function library * LSL Examples - Practical examples * Building Basics — Build objects to script Keep experimenting and learning!

en/content_creation/lsl_scripting/lsl_basics.txt · Last modified: 2026/10/04 19:11 (external edit)

Page Tools