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 Jasa SEO Jasa SEO Murah Sepatu Online Toko Sepatu Online Sepatu Sepatu Murah Sepatu Safety Sepatu Futsal Cheapes Hostgator Coupon Link Booking Televisori offerte Notebook Offerte Berita Terkini Internet Marketer Muda Internet Marketer Indonesia Portatile Apple RDAnet Lorks Karikatur Bisnis Modal Kecil Bisnis UKM Berita Terbaru Iklan Baris Jasa SEO Jasa SEO Murah SEO Indonesia Konsultan SEO SEO Belajar SEO Kursus SEO Kursus SEO Murah Jam Tangan Casio Jam Tangan Casio Jam Tangan Murah Jam Tangan Grosir Baju Terbaru Grosir Baju Baju Terbaru Grosir Baju Murah Bisnis Online Belajar SEO Kerupuk Kerupuk kulit Social Bookmark Dofollow Social Bookmark Kumpulan Puisi Kirim Puisi bola hantu Penumbuh Rambut Penumbuh Rambut timbangan WBC Wonogiri Jasa SEO Murah Jasa SEO Jam Tangan Murah