User:Jesse Barnett/Script Anatomy

From Second Life Wiki
Jump to navigation Jump to search
/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////ANATOMY OF A SCRIPT///////////////////////////////////

//////GLOBAL VARIABLES
//Variables declared here can be used throughout the script
//Global Variables make the script easier to read and edit
//Global Variables use less memory if used multiple times in a script
//Global Variables use more memory if only used once or twice, balance this with readability
//Name a variable so that you can remember what it is and references
//This becomes more important as the script grows in length

string strExample = "I am a string";
list lstExample = ["I am a string in a list"]; //lists start at 0
vector vecExample;// Global Variables need to be declared but do not need to be defined yet

//////USER DEFINED FUNCTIONS
//Please see the wiki section on User Defined Functions for more info

my_function(){
	llOwnerSay("I am called from a User Defined Function");
}

/////STATES
//Every script has to have at least one state
//The first state is named default and does not need to be declared as a state
//except to call it from another state
//Any other states need to be declared
//Example: "state next" w/o qoutation marks
//Wherever it is used it will always be both words: "state next" w/o quotation marks
//Declaring state next and calling just "next" will not work
//Multiple states can make a script easier to read and maintain
//It is better to stay within one state if at all possible

default{ //notice that "state" is not necessary
	/////EVENTS
	//Events are declared inside a state
	//You can not declare an event inside another event or User Defined Function

	state_entry() {
		llOwnerSay(strExample + " called from within state_entry");
		//Notice that i have just combined the two strings this will result in
		//this message:
		//"I am a string called from within state_entry"

		my_function();
		//Now I have just called the user defined function

		vecExample = llGetPos();
		//Now I have defined the vector I declared globally
		//This can now be used throught the script and
		//will stay the same unless it is redefined
	}
	touch_start(integer n) {
		llOwnerSay("I have just been touched");

		/////Local Variables
		//Local Variables use less memory then Glabal Variables
		//But can only be used within the current event
		string lstExampleString = llList2String(lstExample, 0);
		llOwnerSay(lstExampleString);

		llOwnerSay("I am a vector cast as a string: " + (string)vecExample);
		//llOwnerSay "says" strings and only strings to
		//"say" anything that is not a string it must be "cast"
		//Please see the wiki section on "typecasting" for more information
		//vecExample is a vector but here I have "cast" it as a string

		state next;
		//Several things occur when you change states
		//Some porcesses are put on hold and you may
		//need to put some on hold yourself
		//to do this use state_exit()
	}
	state_exit() {
		llOwnerSay("I am called from state_exit");
	}
}//Brackets are used everywhere in a script
//A closing bracket has to be used for every opening bracket

state next{
	state_entry() {
		llOwnerSay("WOOHOO! I am called from a different state " +
			"Touch me again to switch back to state default");
		//Notice how I broke the string into multiple parts to keep the line
		//from running off the end"
	}
	touch_start(integer n) {
		llOwnerSay("Switching back to the default state now");
		state default;
		//Notice that here I have had to call default as a state
	}
	//If you do not need it, you do not have to call state_exit
}