LSL Variables

From Second Life Wiki
Jump to navigation Jump to search

A variable is a place to store information, like a number or strings.

LSL is a strongly and dynamically typed language. This means that variables must be declared by type and that variables may only hold values of a corresponding type.

Variable Conventions

Common Variable Uses

  • i is usually used as an index variable in a for loop. j is usually used for a nested for loop (when i is already being used), and then k.
  • x, y, and z usually refer to the first three dimensions of some vector (when applicable).

Magic Numbers

The phrase "magic number" refers to a number written literally as a constant in code, but the meaning is not intuitive from the context of the code. It may also be such a number used repeatedly throughout the code.

For example:

if ( vctSpd.z < 10 ) { }

The 10 is a "magic number". In a scenario where this test is repeated 20 times in the script, any change to the number would require 20 updates by hand, which increases the chance for errors.

To avoid magic numbers, declare a variable of the appropriate type, set the value of the variable in the declaration and use the variable in your code.

integer intSlowSpeed = 10;

if ( vctSpd.z < intSlowSpeed) { }

Updating the value of intSlowSpeed here requires only one change and reduces the opportunity for error. The name of the variable also expresses its meaning.