SCR-301 Intermediate The School of Creation
Lead Instructor

Sorin Todys - Advanced expert with 20+ years of experience in virtual worlds


All classes take place in Alife Virtual World at our dedicated Alife Virtual School region

Introduction to Scripting - Your First Interactive Object (SCR-301)

Introduction to Scripting - Your First Interactive Object - Alife Virtual World School

Learn and Grow at Alife Virtual World School

Part of The School of Creation | Difficulty: Intermediate


Course Details
  • Course Code: SCR-301
  • School: The School of Creation
  • Difficulty Level: Intermediate
  • Course Description: Write your first Linden Scripting Language (LSL) script to make objects respond to touch and create truly interactive experiences in Alife Virtual.
  • Lead Instructor: Sorin Todys, Advanced LSL Expert

1. COURSE OVERVIEW

Welcome to the World of Interaction!

Welcome, creators! If you've ever looked at a static object in Alife Virtual and wished it could *do* something, then you've come to the right place. This course is your gateway into the fascinating world of the Linden Scripting Language (LSL). Scripting is the "brain" that gives objects behavior, turning a simple block of wood into a door that opens, a button that gives gifts, or a light that illuminates a room.

In SCR-301, we will demystify scripting and empower you to bring your own creations to life. We'll start from the very beginning, understanding the core concepts of LSL, and by the end, you'll have scripted your very first interactive object that responds to you and other avatars. This is a hands-on, practical course designed to give you a solid and confident start.

Learning Objectives

Upon successful completion of this course, you will be able to:

  • Understand the fundamental structure of an LSL script, including states, events, and functions.
  • Confidently use the in-world script editor in the Firestorm Viewer to create, edit, and save scripts.
  • Write a script that makes an object communicate in local chat using the llSay() function.
  • Implement the touch_start event to make objects respond to clicks.
  • Control an object's color dynamically using the llSetColor() function and vector data types.
  • Personalize interactions by detecting the name of the avatar touching your object.
  • Create a simple "state machine" to give an object different behaviors, such as an on/off switch.

What You Will Master

By the end of this course, you will have moved beyond being a static builder and will have become an interactive creator. You will master the ability to take any basic prim you create and imbue it with life. You'll be able to confidently build objects that greet visitors, change appearance on command, and function as simple devices. This foundational knowledge is the most crucial step in your journey to becoming an advanced LSL scripter.

Prerequisites

Required: Basic building skills in Alife Virtual. You must be comfortable with creating a prim (a cube, sphere, etc.), moving it, and accessing its edit menu. We recommend our BLD-101: Building Fundamentals course if you are new to creating objects.

Lead Instructor

This course is taught by Sorin Todys, our resident expert in the Linden Scripting Language. With over 20 years of experience scripting in virtual worlds, Sorin has a unique talent for making complex programming concepts easy to understand for beginners and intermediate learners alike. He is passionate about empowering students to unlock their full creative potential through code.

2. LESSON 1: The Magic of "Hello, World!" - Your First Script

Theory: What is a Script?

Think of a script as a recipe or a set of instructions that you place inside an object. The object will follow these instructions precisely. LSL is an event-driven language. This means a script spends most of its time waiting for something to happen (an "event"). Events can be anything: an avatar touching the object, a timer going off, or a message being received from another script.

When an event occurs, the script looks for a matching set of instructions (an "event handler") and executes the code inside it. The fundamental structure you'll always see is:

  • States: A script is always in a "state." The starting state is always called default. States help organize your script's behavior, like having a "door open" state and a "door closed" state.
  • Events: These are the triggers. The most common one we'll start with is touch_start, which fires when someone clicks on the object.
  • Functions: These are the actions. They are commands that tell the object to *do* something, like speak (llSay()), change color (llSetColor()), or move (llSetPos()). Every function call must end with a semicolon ;.

Step-by-Step: Creating a "Greeter" Object

Let's make an object say "Hello, World!" when you touch it. This is the traditional first step in learning any new programming language.

  1. Rez a Prim: Right-click the ground and choose Create. A build menu will appear. Click on the ground again to "rez" (create) a simple wooden cube. This cube is our object, or "prim."
  2. Enter Edit Mode: Right-click your new cube and select Edit from the pie menu. You'll see arrows and tools appear around your object.
  3. Navigate to the Content Tab: In the Edit window that pops up, find and click on the Content tab. This is the inventory of the object.
  4. Create a New Script: Click the New Script button. A new item named "New Script" will appear in the Content tab, and the script editor window will open, pre-filled with a default script.
  5. Examine the Default Script: The default script looks something like this. Let's break it down.
    default { state_entry() { llSay(0, "Hello, Avatar!"); } touch_start(integer total_number) { llSay(0, "Touched."); } }
    • default { ... }: This is our main state. All the code lives inside these curly braces.
    • state_entry() { ... }: This event runs once when the script first starts or is reset. Here, it makes the object say "Hello, Avatar!".
    • touch_start(...) { ... }: This event runs every time someone touches the object. Here, it makes the object say "Touched.".
  6. Modify the Script: Let's make it our own. Delete the entire contents of the script window and replace it with this simpler version. This version will only react to a touch.
    default { touch_start(integer total_number) { llSay(0, "Hello, World! You touched me."); } }
  7. Save Your Script: Click the Save button at the bottom of the script editor. The script will compile. If there are no errors, the button will become grayed out. If there are errors, they will appear in the chat window below the code. The most common error is a missing semicolon ;!
  8. Test It!: Close the script editor and the Edit window (by pressing Escape or closing the window). Now, left-click on your cube. Look at the local chat in the bottom left of your screen. You should see your object saying: [Object] YourObjectName: Hello, World! You touched me.

Key Concepts Highlighted

  • Prim: The basic building block in Alife Virtual (e.g., a cube, sphere).
  • Script Editor: The in-world tool for writing and saving LSL code.
  • default state: The primary container for your script's logic.
  • touch_start() event: The trigger that executes code when an object is clicked.
  • llSay() function: The command to make an object speak in local chat.
  • Semicolon ;: The most important piece of punctuation in LSL. Every complete command statement must end with one. Forgetting it is the #1 cause of syntax errors.

Congratulations! You've just written your first working script and created an interactive object. You've taken your first step into a larger world of creation!

3. LESSON 2: Beyond Words - Changing Colors and Detecting Who Touched You

Theory: Variables and More Functions

In Lesson 1, our object did the same thing every time. To make it more dynamic, we need to introduce variables and new functions. A variable is like a labeled box where you can store information for the script to use later. This information can be a number, a piece of text, or even a color.

  • Data Types: Every variable has a type. The most common are:
    • integer: A whole number (e.g., 5, -10, 1000).
    • float: A number with a decimal point (e.g., 3.14, -0.5).
    • string: A piece of text, enclosed in double quotes (e.g., "Hello, Sorin!").
    • vector: A set of three floats, used for positions, forces, and most importantly for us, colors (e.g., <1.0, 0.0, 0.0> for red).
    • key: A unique identifier for any person, object, or item in the world.
  • New Functions:
    • llSetColor(vector color, integer face): This function changes the color of the object. It requires a vector for the color and an integer specifying which face of the prim to color. We can use the special constant ALL_SIDES to color the entire object.
    • llDetectedKey(integer index): When used inside a touch event, this function returns the unique key of the avatar who touched the object.
    • llGetUsernameFromKey(key id): This function takes a user's key and returns their username as a string.

Practical Example 1: The Color-Changing Block

Let's modify our script to make the block turn bright red when touched.

  1. Open the script from Lesson 1 by editing the cube, going to the Content tab, and double-clicking your script.
  2. Modify the code inside the touch_start event. We'll add the llSetColor function.
    default { touch_start(integer total_number) { // Make the object say something in chat llSay(0, "Ouch! You touched me, so I'm turning red."); // Define the color red as a vector vector red_color = <1.0, 0.0, 0.0>; // Apply the color to all sides of the prim llSetColor(red_color, ALL_SIDES); } }

    Note on Colors: Color vectors use RGB (Red, Green, Blue) values from 0.0 (none of that color) to 1.0 (full intensity). So <1.0, 0.0, 0.0> is pure red, <0.0, 1.0, 0.0> is pure green, and <1.0, 1.0, 1.0> is white.

    Note on Comments: The line starting with // is a comment. The script ignores it. It's for you to leave notes for yourself or others reading your code!

  3. Click Save and close the editor. Touch your cube. It should now turn bright red and speak to you!

Practical Example 2: The Personal Greeter

Now, let's make the greeting personal. Instead of "Hello, World!", it will say hello to the specific person who touched it.

  1. Edit your script again. This time, we'll use variables to store the toucher's information.
  2. Replace your code with the following:
    default { touch_start(integer total_number) { // 1. Get the unique key of the person who touched the object. key toucher_id = llDetectedKey(0); // 2. Convert that key into a username string. string toucher_name = llGetUsernameFromKey(toucher_id); // 3. Use the name in our llSay function. // The '+' symbol is used to join strings together. llSay(0, "Aha! It was " + toucher_name + " who touched me!"); } }
  3. Save the script. Now when you touch it, it will say your actual username! Try having a friend touch it and see what happens. This is the beginning of creating truly social and responsive objects.

4. LESSON 3: Smarter Objects - Using States to Create a Light Switch

Advanced Application: State Machines

Right now, our object does one thing when touched. What if we want it to do one thing on the first touch, and something different on the second touch? This is where states become incredibly powerful. We can create a script that acts like a light switch: the first touch turns it on, the second turns it off.

To do this, we will create two states: an "off" state and an "on" state. We will use the default state as our "off" state, and we will create a new, custom state called on.

Real-World Scenario: Scripting a Clickable Light

This is a classic and highly useful script. It demonstrates states, changing visual properties, and even adding light to the scene.

Best Practices for This Script:

  • Clear State Names: Naming your states logically (e.g., on, off, open, closed) makes your code much easier to read.
  • Use state_entry: The state_entry event is perfect for setting up the initial appearance and properties of an object as soon as it enters a new state.
  • Comment Your Logic: Explain *why* you are changing states. In a few months, you'll thank yourself for it.

Step-by-Step: The Light Switch Script

  1. Create a new cube prim (or use a