Ch1addenda

From SYW

Jump to: navigation, search

This addendum contains a description of #llListStatistics(), #Bitfields and #LSL Pros and Cons.

Contents

llListStatistics()

Table 1.8 on Page 16 of Scripting Your World refers to the function llListStatistics(). This function performs statistical operations such as min, max and standard deviation, or a list composed of integers and floats. Here we describe how to use this function.

float llListStatistics( integer operation,  list input )
* operation is a named constant that takes one of the constants shown in the table below.
* list is list of numeric values (integer or float).
Constant Value Description
LIST_STAT_RANGE 0 Calculates the range, i.e.
llListStatistics( LIST_STAT_MAX, list ) - llListStatistics( LIST_STAT_MIN, list )
LIST_STAT_MIN 1 Calculates the smallest number.
LIST_STAT_MAX 2 Calculates the largest number.
LIST_STAT_MEAN 3 Calculates the mean (average). For two numbers a and b, the mean is (a+b)/2. For a list of n numbers, it is
1 / n * llListStatistics( LIST_STAT_SUM, list )
LIST_STAT_MEDIAN 4 Calculates the median number. The median is the number for which half the values are less and half are greater. For example, the median of [1,1,1,2,1000,1000,1000] is 2, while the mean is 429.3.
LIST_STAT_STD_DEV 5 Calculates the sample standard deviation. It is a measure of how spread out the values are, and is defined as the square root of the average of the squares of the numbers.
LIST_STAT_SUM 6 Calculates the sum of all the elements in the list.
LIST_STAT_SUM_SQUARES 7 Calculates the sum of the squares, namely (a*a)+(b*b)+(c*c)...
LIST_STAT_NUM_COUNT 8 Determines the number of float and integer elements.
LIST_STAT_GEOMETRIC_MEAN 9 Calculates the geometric mean. For two numbers a and b, the geometric mean is llSqrt(a*b). For a list of n numbers, it is the n'th root of that list. It indicates the central tendency, or typical value, to expect.

Bitfields

A bit field (also known as a bit pattern) is a way to store a set of Boolean flags compactly, as a series of bits. Each byte can describe up to eight different flags. LSL uses 4-byte integers, so they can store 32 flags. Each bit is numbered from right to left; for example, the image below of an 8-bit binary number shows the third and fourth bits as TRUE, while all other bits are FALSE.

To access these bits, each bit has a value that corresponds to where it would be if the whole byte were interpreted as an integer. These are written as hexadecimal values , prefixed with “0x,” as shown in this table:

Bit Pattern Decimal Value Hexadecimal Value
0000 0000 0000 0001 1 0x01
0000 0000 0000 0010 2 0x02
0000 0000 0000 0100 4 0x04
0000 0000 0000 1000 8 0x08
0000 0000 0001 0000 16 0x10
0000 0000 0010 0000 32 0x20
0000 0000 0100 0000 64 0x40
0000 0000 1000 0000 128 0x80
0000 0001 0000 0000 256 0x100
0000 0010 0000 0000 512 0x200
0000 0100 0000 0000 1024 0x400

Each bit in most LSL-provided bit patterns has a named constant so that it can be accessed without remembering its exact hexadecimal value. For example, PERMISSION_TAKE_CONTROLS is equal to 0x004. Four operators are used uniquely for bit patterns:

  • Complement (~), which flips each bit of a pattern
  • Or (|), which combines two patterns by making a bit TRUE when at least one of the patterns has that bit TRUE
  • Exclusive or (^), which combines two patterns by making a bit TRUE when exactly one of the patterns has that bit TRUE
  • And (&), which combines two patterns by making a bit TRUE if both of the patterns have that bit TRUE.

The following example illustrates how these operators work:

integer x = 0x84;         // x is  1000 0100
integer y = 0x88;         // y is  1000 1000

integer a = ~x;           // gives 0111 1011
integer b = x | y;        // gives 1000 1100
integer c = x ^ y;        // gives 0000 1100
integer d = x & y;        // gives 1000 0000

In LSL, the or (|) operator is frequently used to combine flags, while the and (&) operator is used to compare them. Take for example the PERMISSION_* constants that describe the permissions a script has. PERMISSION_TAKE_CONTROLS has a value of 0x004, and PERMISSION_TRIGGER_ANIMATION has a value of 0x010. A script might request both of these permissions by combining them with the or operator, as in the following snippet:

llRequestPermissions(llGetOwner(),
    PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS);

The script can then compare what permissions were granted by using the bitwise and operator, like this:

integer grantedPerms = llGetPermissions();
if ( grantedPerms & PERMISSION_TAKE_CONTROLS ) {
    llOwnerSay("Script has permission to take controls.");
}
if ( grantedPerms & PERMISSION_TRIGGER_ANIMATION ) {
    llOwnerSay("Script has permission to trigger animation.");
}

LSL Pros and Cons

The only way to program script in Second Life is to use LSL. It is therefore useful to recognize some of the pros and cons of LSL in relationship to other modern computer languages.

Pros

  • States
  • Events
  • Heterogeneous lists
  • Vectors and quaternions as basic types

Cons

  • Incomplete automatic type conversion
  • No library mechanism
  • No code execution outside blocks, such as for initializing global variables
  • No nested lists
  • No associative arrays or maps
  • No structures
  • No in-world debugger (there is a test harness, but it’s weak)
  • Lack of an optimizing compiler
  • Weak memory management
Personal tools
Advert