Variables Example

From Second Life Wiki
Jump to navigation Jump to search

Variables Example

This is an example to show the basic use of variables. --Eyana Yohkoh 19:24, 27 August 2007 (PDT)

// this script will add 1 to x every other time it is touched.

integer x = 8;      // this makes an integer set to the value of eight when the script starts
integer y = FALSE;  // you can set integers to TRUE or FALSE too
                    // in this example, y is going to be used to see if the last time the object
                    // was clicked 1 was added to x.
                    // basically by setting y to TRUE and FALSE the script will be able to add one
                    // to x every other time



default
{
    state_entry()       // we'll just have the script say the value of x when it loads
    {
        llSay( 0, "x has a value of: " + (string)x );
    }
        
    touch_start(integer total_number)
    {
        if ( y == FALSE )           // first time clicked y would be false so that's when 1 
                                    // should be added to x                                    
        {
            x = x + 1;              // adding one to x here

            llSay( 0, "1 added to x. x is now: " + (string)x ); 
                                    // this will make the object say the value of x.
            y = TRUE;               // since this started with y being FALSE, now it's time
                                    // to make it TRUE so the next time it's clicked it does
                                    // something different.
        }
        
        else if ( y == TRUE )       // do this stuff is y is TRUE
        {   // object says this
            llSay( 0, "nothing added to x this time. x is still: " + (string)x );

              y = FALSE;          // since nothing was added this time then set y back to FALSE
                                // so something gets added next time.
        }
    }
}