LSL Wiki Mirror 10-5-2006: variable

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings ::

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:

type name;

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:
integer bar = 3;
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:
integer foo;
Declares a variable of type integer with the name foo.
foo = 2;
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: Okay, so I have a variable. How do I actually do something with the data stored inside it?
A: Simply use the variable name where you'd otherwise place a fixed value. For instance, in the context of "llSay(channel,message);, "channel" and "message" are variable names that would contain the channel to chat on, and the message to say.

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: Does a long variable name make the script run slower? Should I be naming all my variables "a", "b", and so on to save space?
A: No, variable names only matter for how they'll appear in the editor. From the script's perspective, they might as well be named "a" or "b", but scripts with poorly-named variables are an absolute nightmare to work with. Stick with descriptive names like "last_detected_avatar" -- it's easy to figure out what that does. However, be careful to not be too verbose. Names like "key_detected_avatar_in_state_detection_number_3" are even more of a nightmare to work with than really short names.

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();


Global Variables | Local Variables
There is no comment on this page. [Display comments/form]