Session 3 Advanced scripting

trampknowledgeSoftware and s/w Development

Nov 4, 2013 (4 years and 8 months ago)


Advanced scripting
Serious Games and Virtual
World Research Team
Michael Callaghan
Kerri McCusker
Julio Lopez-Losada
Session Overview
Session 3: Practical scripting tutorial
• Introduction to structure of Linden Scripting Language
• Adding scripts to content from practical 2
Linden Script Language
• Simple, yet powerful scripting language, used
to attach behaviours to the objects found in
Second Life.

A script in Second Life is a set of instructions
that can be placed inside any primitive object
in the world, but not inside an avatar.
Linden Script Language
Scripts can make an object:
• Move
• Listen
• Talk
• Operate as a vehicle or weapon

Change colour, size or shape
Linden Script Language
• Multiple scripts may be attached to the same

When several prims are linked, they can each
contain a script which interacts with the other
parts of the object.
Linden Script Language
• Familiar syntax of a C/Java-style language
• Event-driven
• Features states

Manages a variety of built-in functions
LSL basic syntax: comments

Comments: lines prefixed with double-slash (//)

Anything following the double-slash sill be

Example: // This is a comment
LSL basic syntax: variables

Variable: identifier or name of a place to store

It has always a type that constrains what type
of values it can store: string, integer, float,
vector, rotation, key or list.

Variable declaration: integer pos = 3;
LSL basic syntax: lists

Special kind of data type, which can contain
zero or more elements.

Signified by square brackets surrounding their
elements, separated by commas.
• Elements in a list can have different types.
LSL basic syntax: lists
• list days = [“Monday”, “Tuesday”,
“Wednesday”, “Thursday”, “Friday”,
“Saturday”, “Sunday”];
• list even_numbers = [2, 4, 6, 8, 10];

list mix = [1, “two”, 3.0, <4,5,6>];
LSL basic syntax: functions

Function: piece of code that does some work
and returns a result.
• It can be defined by you or built-in.
• A function can, inside of its code, call another
• Calling convention:
type name(type parameter1, type parameter2,...){}
• Example:
integer sum(integer a, integer b) {
integer result = a + b;
return result;
LSL basic syntax: functions
LSL basic syntax: conditionals

Conditional statement: allows the value of a
variable or expression to control the flow of
• Four different conditional statements in LSL:
“if”, “for”, “while”, “do while”.

if (condition) branch_true; else branch_false;

for (initializer; condition; increment) loop;

while (condition) loop;
• do loop while (condition);
LSL basic syntax: conditionals
• One thing that makes LSL unique is its emphasis
on "States" and "Events“.
• A door can be "open" or "closed" and a light
can be "on" or "off". Many real life behaviours
can be modeled with "states“.
• A door can be “touched” to close it. A light can
receive a “signal” to switch it off. Many real life
actions can be modeled as “events”.
LSL structure: states and events
• Section that is running, and waiting for
• Only one state can be active at any one time
per script.

Every script must have a default state with at
least one event in it.
LSL structure: states

Except for the default state, each state is
defined by the word “state” followed by the
name of the state.
• The contents of the state are enclosed in two
curly brackets.
LSL structure: states
LSL structure: events

They are inside of states. When one state is
active, the events inside it wait to be triggered
and run the code inside them.
• Almost every possible interaction can trigger
an event.
LSL structure: events

Events can’t interrupt each other.
• As the functions, some events have parameters.
• Examples:
o touch_start
o moving_start
o listen
o state_entry
o state_exit
o collision_start
default {
touch_start(integer total_number){
state touched;
state touched{
state default;
LSL structure: states and events
integer counter; // Variable declaration
integer sumOne(integer number){ // This is a function
return number + 1; // that gets one integer and add 1
default{ // This is the default state
state_entry(){ // This event is triggered when entering in the state
total = 0; // Now, “total” has the value 0
touch_start(integer total_number){ // This is other event
counter = sumOne(counter); // Here the function is called
llSay(0,”The counter is “ + (string)counter); // This is a built-in function
LSL: putting all together
Now that the building exercise has been completed –
scripts can be added to object such as doors to add
• Add script to garage door
• Add scripts to door
• Add a visitor counter
Session Summary
• Overviewof Linden Scripting Language
• Added scripting functionality to content
Advanced scripting
Serious Games and Virtual
World Research Team
Michael Callaghan
Kerri McCusker
Julio Lopez-Losada