Variable
A
variable is an
identifier or name of a place to store information in a
script. The process of creating a variable is called "variable declaration", "declaring a variable" or "defining a variable".
A variable always has a
type which is either
string,
integer,
float,
vector,
rotation,
key, or
list. The type of a variable constrains what type of values can be stored in it.
Variables can be declared wherever
code can be run, as well as before the
default state (see
global variable). This includes, but is not limited to:
events and
functions. Variables have a
scope which is either
local or
global.
To declare a variable, use this general format:
The type is one of
LSL's
types, and the
name is any combination of letters (caps or lowercase), numbers, or underscores (_), that does
not start with a number.
Variable names that contain the characters: left single quote (`), hash/number sign (#), dollar sign ($), backslash (\), right single quote ('), and question mark (?) at the beginning or end of the variable name will not report an
error but these characters will be ignored. Hence the variable called "foo" will be the same as the variable called "foo$", "$foo", "foo?", etc.
Variables can also be given an initial value on the same line as their declaration:
Declares a variable of type
integer, with the name
bar, and assigns it a value of 3.
To
assign a value to a variable, follow this format:
name = value;
Where name is the
name of the variable and
value is the value assigned to it.
Examples:
Declares a variable of type
integer with the name
foo.
Assigns
foo the value 2.
string MyLongNamed2284StringThat_USES_underscore;
Declares a variable of type
string with the name
MyLongNamed2284StringThat_USES_underscore.
MyLongNamed2284StringThat_USES_underscore = "hello!";
Assigns
MyLongNamed2284StringThat_USES_underscore the value "hello!"
Q: Is there a maximum length to a variable's name?
A: Yes, 255 characters. Interestingly, if you define a variable with a longer name, your script will compile succesfully, but attempting to actually do anything with it will cause the compiler to fail with the message, "ERROR: Name not defined within scope". It shouldn't matter anyway; 255 characters is more than sufficient for even the most obsessively descriptive variable name.
A: While you can only really use a variable with up to 255 characters, one may be defined with up to 16,384 characters. If you exceed that limit and attempt to compile the script, your client will freeze. The Lindens do not plan on fixing this any time soon, because it is really esoteric.
Q: Can I use a string as the name of a variable or function?
A: No, there's no way to convert a string to a variable or function name directly. You'd need to use something like this: if (msg == "test") testFunction();
Local Variables |
Global Variables |
Memory Usages