Don't click here unless you want to be banned.

LSL Wiki : LocalVariables

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ia360925.us.archive.org

Local Variables


A local variable is a variable that exists within a code block. (Between an opening and closing brace -- { }) They are only accessible from within the same context (scope) or area of the script where they were declared. (Compare with global variables, which can be accessed and referred to in any part of the script.)

All code within the local variable's code block has access to the variable, provided it follows the local variable declaration. (Remember, operations execute in the order in which you write them!) This includes code blocks declared within the local variable's code block.

Example:
myFunction() {
    anInteger = 5; // ILLEGAL!!! anInteger hasn't been declared yet!

    integer anInteger; // Declaration (creation) of a local variable.

    if(someCondition) {
        anInteger = 4; // Valid!
    }

    anInteger = 12; // Valid!
}

default {
    state_entry() {
        anInteger = 122; 
        // ^^^^ ILLEGAL!!! anInteger was declared within the CodeBlock of myFunction(), 
        // So technically, it doesnt exist outside that CodeBlock. 
        // In order to use anInteger again, you have to re-declare it within state_entry(), however,
        // even if you do, it will have no connection to anInteger in myFunction().

        integer anotherInteger; // Declaration (creation of a global variable).
        anotherInteger = 3; // Valid!

        if(someCondition) {
            anotherInteger = 492; // Valid!
            
            if(anotherCondition) {
                anotherInteger = 122; // Valid!
            }
        }
    }

    touch_start(integer n) {
        anotherInteger = 12; // ILLEGAL!!! anotherInteger was declared within state_entry()'s CodeBlock.
        // It must be re-declared to use it in touch_start.

        if(someCondition) {
            integer yetAnotherInteger; // Declaration.
            yetAnotherInteger = 4; // Valid!

            if(someOtherCondition) {
                yetAnotherInteger = 1239; // Valid!
            }
        }
        
        yetAnotherInteger = 1223; 
        //^^^^ ILLEGAL!!! yetAnotherInteger is declared within the CodeBlock of if(someCondition), 
        // it cant be used **outside** of if(someCondition)'s CodeBlock.
    }
}

You can do many odd quirky things if you give two variables the same name. Espicially if they're a local and global variable.
Here's a situation you do NOT wanna be in:
Example (NOT RECOMENDED CODING PRACTICE):
integer myInteger = 3; //This is a global variable

integer myFunction(float myFloat)
{
    integer myInteger = 6; //This is a local variable, with the same name as a global.
    // It is seriously bad coding practice, although legal, to define variables like this. Doing this is called "shadowing" a variable (I think).
    // Any changes made to myInteger within this function affect the variable declared here, NOT the global variable.
    // Anyplace within this function that uses myInteger, will be talking about the integer above, NOT the global variable.

    return myInteger;
}

default
{
    state_entry()
    {
        myInteger = 2; //This sets the global variable to 2
        llWhisper(0,(string)myFunction(1.0)); //This will whisper 6
        llWhisper(0,(string)myInteger); //This will whisper 2.
        
        // Note that myFunction did not change the value of the global, they are seperate variables.
    }
}

Compare with GlobalVariables.


Variable
Comments [Hide comments/form]
It will also be abundantly clear that state-scope-global variables may not be defined, if you try this. (Yet? :) )
-- ChromalBrodsky (2004-03-08 15:32:42)
Do local variables perminantly hold a chunk of memory?
-- ppp-70-226-160-124.dsl.mdsnwi.ameritech.net (2007-12-19 08:45:28)
Attach a comment to this page: