Misplaced Pages

Linden Scripting Language

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 20:18, 19 December 2011 (More complex example script: Added more complex example script. If there are no objections I will gladly set to work annotating it.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 20:18, 19 December 2011 by Fred Gandt (talk | contribs) (More complex example script: Added more complex example script. If there are no objections I will gladly set to work annotating it.)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
This article relies excessively on references to primary sources. Please improve this article by adding secondary or tertiary sources.
Find sources: "Linden Scripting Language" – news · newspapers · books · scholar · JSTOR (December 2011) (Learn how and when to remove this message)
Linden Scripting Language (LSL)
Typing disciplinestrong
OScross-platform
Websitewiki.secondlife.com/wiki/LSL_Portal
Influenced by
C

Linden Scripting Language, or LSL, is the programming language used by residents of Second Life, a virtual world by Linden Lab.

LSL has a syntax similar to C and allows objects to control the behavior of in-world objects of Second Life from the Internet via email, XML-RPC, and most recently, HTTP requests.

LSL design

Linden Scripting Language is a state-event driven scripting language, in the sense of a finite state machine. A script consists of variables, function definitions, and one or more named states. Each state contains a description of how to react to events that occur while the program is within that state. The system sends events to the script, such as timers, movement, chat (from other agents), email, and collisions (with objects in the virtual world). Scripts can change most aspects of the state of the object and communicate with other objects and agents. As soon as a script is added to an object, and turned on, it begins to execute.

A script is tightly bound to the concept of virtual-world objects (in the 3D modeling sense rather than in the object-oriented sense). An object in Second Life represents something like a chair or a wall, or possibly something invisible. Multiple scripts may be placed inside an object, where they all execute simultaneously.

There are over 300 library functions available. Users can also define additional functions. LSL is a strongly typed language that is compiled to bytecode before runtime execution in a virtual machine (VM) on one of Linden Lab's servers.

LSL's native data structures includes integers, floating point numbers, strings, keys(UUID), vectors(used for 3D coordinates and RGB color expression), and rotations (quaternions). There are also heterogeneous lists. There are no arrays; multiple list functions are used to adapt programs requiring array structures. There is no built-in persistent data storage, such as a file or database. On the other hand, scripts continue to run even when a user is not logged in, and if an object is saved (taken into inventory), and then re-introduced into the World later, it still maintains its previous state. In addition, the mechanisms for communicating via HTTP can be used to store a state externally.

Some functions in LSL have built-in delays, which range from a 0.1-second delay when (for example) requesting string data from a notecard; to a 20-second script pause after sending an e-mail message. The delays help prevent developers from writing LSL scripts that could overtax system resources. Memory available to LSL scripts (when compiled as mono) is capped at about 64 KiB, which places a practical limit on how much a single script can do, however the ability to use multiple scripts and to call scripts from another script allows the user to pragmatically work around this 64 KiB cap.

Permission system

Certain actions in LSL require permissions:

  • take money from agent's account
  • take agent's controls
  • start or stop Animations on agent
  • attach/detach from agent
  • change links
  • track the agent's camera position and rotation
  • control the agent's camera

The permission system, however, is an issue for scripts owned by the land owner: those scripts can modify the parcel settings without requiring permissions. A malicious script can unsit avatars, ban and unban avatars, destroy the terrain that includes returning of objects by letting them fall out of the World, and gather the ip addresses of avatars by manipulating the media settings for that avatar.

Default LSL script

The default script, "Hello, Avatar", a hello world program that speaks (streams message to local chat ("heard" (printed) by all viewers (clients) within 20 meters of the task)) when the script is first saved, initialized or reset (in this case, since the command is in the default "state", the first to be run on initialization) or 'touched' (a simple user interaction usually initiated using a mouse click), looks like:

default
{
	state_entry()
	{
		llSay(0, "Hello, Avatar!");
	}
	touch_start(integer total_number)
	{
		llSay(0, "Touched.");
	}
}

Mono

The revised version of Second Life's scripting virtual machine is based on Mono, the open source implementation of the Microsoft.NET framework. The Mono VM was introduced to several simulators (sims) on the Second Life Beta grid for compatibility testing on 29 January 2008 and later that year on 20 August 2008 Linden Lab started deploying it on the production grid - with the entire production grid updated to use it on 29 August 2008.

While the LSL scripting language remained the same, scripts executed on the Mono underpinnings were up to 220 times faster in execution, but at the cost of a somewhat higher overhead when creating (rezzing) scripted objects and moving them from sim to sim.

More complex example script

Below is an example of a relatively simple LSL script, that when placed in a primitive along with a selection of .wav sound files, users may select the sound files to be played in series. This scripting is required to play full length songs in Second Life, as the sound file length is limited to a maximum of 10 seconds per clip at upload to SL.

// This list is all you need to edit. List the names of the songs (each collection of sound files that makes one song)
// followed by the length of those sound clips (each song should contain clips of equal length)
// The list should be structured like so -
// list songs = ;
// The names of the songs must be identical to some part of the sound files used for that song like so -
// In the prim inventory (along with this script) -
// Box_Of_Rain_wav_1
// Box_Of_Rain_wav_2
// Box_Of_Rain_wav_3
// Servant 1
// Servant 2
// Servant 3
// In the script -
// list songs = ;
// The script will play the clips in alpha/numerical order so name them wisely.
list songs = ; // YUP! EDIT THIS BIT ;-)
integer volume = 10; 
integer lis_count; 
integer playing; 
integer busy; 
integer part; 
integer lis; 
integer sl; 
float delay; 
list cancel = ; 
list playlist; 
list waiting; 
list song; 
string vol_str = "Volume"; 
string song_str = "Songs"; 
string song_name;
list StrideOfList(list src, integer stride, integer start, integer end)
{
	list l = ;
	integer ll = llGetListLength(src);
	if(start < 0)start += ll;
	if(end < 0)end += ll;
	if(end < start) return llList2List(src, start, start);
	while(start <= end)
	{
		l += llList2List(src, start, start);
		start += stride;
	}
	return l;
}
list Volumes(integer vol)
{
	integer v = 0;
	list l = ;
	do
	{
		if(v != vol)
		l += ;
	}
	while((++v) <= 10);
	return l;
}
PageOne(key k, integer c)
{
	llDialog(k, "\nAdjust the volume or select a song to play?",  + cancel, c);
}
PlaySong(string n)
{
	song = ;
	integer c = -1;
	string name = "";
	do
	{
		if(llSubStringIndex((name = llGetInventoryName(INVENTORY_SOUND, (++c))), n) != -1)
		song += ;
	}
	while(name);
	delay = llList2Float(songs, (llListFindList(songs, ) + 1));
	if((sl = llGetListLength(song)))
	{
		llPreloadSound(llList2String(song, (part = 0)));
		if(sl > 1)
		llPreloadSound(llList2String(song, 1));
		playing = FALSE;
		llSetTimerEvent(0.01);
	}
}
integer Chan()
{
	return llRound((llFrand(-5000000.0) + -500000.0));
}
float ScaleVol(integer v)
{
	return (v * 0.1);
}
Listen(integer c, key a)
{
	lis = llListen(c, "", a, "");
}
RemoveListen(integer b)
{
	llListenRemove(lis);
	lis_count = 0;
	if(b)
	busy = FALSE;
	lis = 0;
}
SetListenTimer(integer p)
{
	if(p)
	while(((++lis_count) * llRound(delay)) < 30);
	else
	{
		lis_count = 1;
		llSetTimerEvent(30.0);
	}
}
integer CheckWaitingRoom(integer c)
{
	if(waiting)
	{
		key a = llList2Key(waiting, 0);
		if(!c)
		{
			RemoveListen(0);
			Listen((c = Chan()), a);
			SetListenTimer(playing);
		}
		PageOne(a, c);
		waiting = llDeleteSubList(waiting, 0, 0);
		return 1;
	}
	return 0;
}
default
{
	on_rez(integer param)
	{
		llStopSound();
		llResetScript();
	}
	changed(integer change)
	{
		if(change & CHANGED_INVENTORY)
		llResetScript();
	}
	touch_start(integer nd)
	{
 		while(nd)
		{
			key agent = llDetectedKey(--nd);
			if(!busy)
			{
 				busy = TRUE;
				integer channel = Chan();
				SetListenTimer(playing);
				Listen(channel, agent);
				PageOne(agent, channel);
			}
			else
			{
				list a = ;
				if(llListFindList(waiting, a) == -1)
				waiting += a;
			}
		}
	}
	listen(integer chan, string name, key id, string msg)
	{
		if(msg != llList2String(cancel, 0))
		{
			SetListenTimer(playing);
			if(msg == vol_str)
			{
				llDialog(id, "\nChange the volume?\nThe current volume is set at \"" + ((string)volume) + "\"", cancel + Volumes(volume), chan);
				return;
			}
			if(msg == song_str)
			{
				string current = "";
				if(playlist)
				{
					current = "\n\nThe songs currently queued are\n\"" + llList2String(playlist, 0) + "\" (currently playing)";
					if(llGetListLength(playlist) > 1)
					current += "\n\"" + llDumpList2String(llList2List(playlist, 1, -1), "\"\n\"") + "\"";
				}
				llDialog(id, llGetSubString(("\nSelect a song to play?" + current), 0, 500), cancel + StrideOfList(songs, 2, 0, -1), chan);
				return;
			}
			if(llListFindList(Volumes(volume), ) != -1)
			{
				llAdjustSoundVolume(ScaleVol((volume = ((integer)msg))));
 				PageOne(id, chan);
				return;
			}
			if(llGetListLength((playlist += )) == 1)
			PlaySong((song_name = msg));
		}
		if(CheckWaitingRoom(chan))
		return;
		RemoveListen(1);
	}
	timer()
	{
		if(playlist)
		{
			if(!playing)
			{
				llSetTimerEvent(delay);
				playing = TRUE;
			}
			llPlaySound(llList2String(song, part), ScaleVol(volume));
			if((++part) == sl)
 			{
				if(llGetListLength(playlist) > 1)
				{
					song_name = llList2String((playlist = llDeleteSubList(playlist, 0, 0)), 0);
					llSleep(delay);
					PlaySong(song_name);
				}
				else
				{
					llSetTimerEvent(0.0);
					song_name = "";
					playing = FALSE;
					playlist = ;
				}
			}
			else if(part == (sl - 1))
			llPreloadSound(llList2String(song, 0));
			else
			llPreloadSound(llList2String(song, (part + 1)));
		}
		if(lis && (!(--lis_count)))
		{
			if(!(CheckWaitingRoom(0)))
			RemoveListen(1);
		}
	}
}

References

  1. https://wiki.secondlife.com/Help:Getting_started_with_LSL#What_is_LSL.3F
  2. https://wiki.secondlife.com/Category:LSL_Events
  3. https://wiki.secondlife.com/Category:LSL_Functions
  4. https://wiki.secondlife.com/Category:LSL_Types
  5. https://wiki.secondlife.com/Category:LSL_Integer
  6. https://wiki.secondlife.com/Category:LSL_Float
  7. https://wiki.secondlife.com/Category:LSL_String
  8. https://wiki.secondlife.com/Category:LSL_Key
  9. https://wiki.secondlife.com/Category:LSL_Vector
  10. https://wiki.secondlife.com/Category:LSL_Rotation
  11. https://wiki.secondlife.com/List
  12. https://wiki.secondlife.com/Category:LSL_Notecard An in-world data storage medium mainly used for text.
  13. https://wiki.secondlife.com/Hello_Avatar
  14. http://blog.secondlife.com/2008/08/20/mono-launch/
  15. https://jira.secondlife.com/browse/SVC-1341
  16. https://jira.secondlife.com/browse/SVC-3895

External links

Second Life
Culture
Events and
communities
Software
Related
Vector graphics markup languages and file formats
By type
2D
3D
See also
Categories: