LSL Scripting — Variables

In the first article of this series, we looked at the environment in which scripts are run in Second Life™, basically the context of scripts within the sim (simulator) software..In this article we move forward to consider variables.

In 1976, Swiss computer scientist Niklaus Wirth wrote the book: “Algorithms + Data Structures = Programs”. The title is a good starting point for our discussion. Scripts are usually small programs written in interpreted languages; depending on the underlying interpreter to execute them rather than being complete in themselves and able to run directly under a computer’s operating system. For today’s implementation of LSL, Mono is the interpreter and LSL (Linden Scripting Language)  is the scripting language. Scripts are simply a specific type of program and thus fall on the right-hand-side (after the equals sign) of Wirth’s title.To understand scripts, we need to look at the two terms on the left-hand-side (before the equals sign).

An algorithm is simply a step-by-step process to achieve a goal. We’ll get into algorithms later in this series of articles when we start working with example scripts. Each script is the implementation of an algorithm in LSL. The development of scripts goes from concepts of desired goals, into choosing or developing algorithms to get there, into translating the algorithms into LSL coding.

But what about the data? Just about any algorithm uses some form of data. How the needed data is contained and accessed within a script brings us to data structures. Data structures stem from a combination of what options a scripting language provides combined with choices made by the scripter or scriptwriter. In LSL, the types of data are limited, strictly defined, and not extendible (new types can’t be defined from the built-in types).

In many programming languages there are three types of containers for data: literals, constants, and variables. The value of a literal is exactly what it is, for example, 42, or “He’s dead, Jim”, an integer literal and a character-string literal. Constants are named containers but, once defined, the values they represent can’t be changed. Variables are also named containers and, as the name suggests, the values they contain can be changed.

LSL has both literals and variables, but no declaration (i.e. statement that defines) or enforcement of a constant type. Instead, particularly with built-in constants, the lexical convention of naming constants with capital letters and underscores is used, for example, SMART_COOKIE.

Desk with different pigion holes

Desk with different pigeon holes

Variables are, in a sense, like the different sizes and shapes of the pigeon holes in a roll-top desk, with different sized and shaped holes for different contents. In short, variables in LSL have types and must be declared as being one type or another before they can be used.

There is one further difference in variables beyond type that we’ll encounter that depends on where they are declared in a script. Some variables are known to the  entire script (and only the one script) and are called global variables and some are known only within a single event-handler and are known as local variables..The extent of a script within which a variable is known is called its scope. An important difference between global and local variables is that the values in global variables persist over multiple events while the values in local variables are lost at the end of processing a single event. This will become clearer when we get into example scripts. Now we can consider the types of variables connected with the types of data they can hold, whether global or local.

Types of Data in LSL

Type of Data Description Example Literal
Integer Positive, negative, and zero whole numbers, i.e. numbers without a decimal point or fraction 42
Float Positive, negative, and zero numbers with a decimal point and fractional parts 3.14159
String A series of text characters considered as a single item “You’re very tall”
Key A special type of string to hold a Universal Unique IDentifier (UUID) “01234567-89ab-cdef-0123-456789abcdef”
Vector Three floats taken as the x, y, and z components of a single item <112.5, 224.0, 27.0>
Rotation Four floats taken as a single item <0.0, 0.0, 0.707, 0.707>
List Contains a series of values of the above types, but cannot contain another list [“Answer”, 42, “Time (Myr)”, 5.35]

 

Now, let’s see how the example literals could be used in declaring and initializing variables.

integer answer = 42;
float pi = 3.14159;
string remark = "You're very tall";
key item = "01234567-89ab-cdef-0123-456789abcdef";
vector mypos = <112.5, 224.0, 27.0>;
rotation myrot = <0.0, 0.0, 0.707, 0.707>;
list stuff = ["Answer", 42, "Time (Myr)", 5.35];

We’ll talk about them later, but notice each statement is ended with a semicolon. That’s part of the LSL syntax. The use of a semicolon to end a coding statement allows a statement to extend over multiple lines; i.e. the end of line doesn’t end the statement, the semicolon does.

The above covers the basics of variables in LSL. The third article in this series will discuss the overall structure of LSL scripts.Thank you for your attention.

Caledon Oxbridge also has an inworld Basic Scripting class, Mondays at noon SLT in our main lecture hall. Please see our schedule page.

Leave a Reply

Your email address will not be published. Required fields are marked *