megaman54 Posted June 3, 2011 Posted June 3, 2011 I have a small question: what is the difference between these things: (these are just examples) local testVar = "helo" testVar = "helo" What i mean, is the "local" before the variable name, where is this needed/used ? Thanks!
Aibo Posted June 3, 2011 Posted June 3, 2011 local variables have a scope limited to the block where they are declared (such as script files, functions, if statements, loops, etc). so outside of that block they are either nil or have some other value if defined. and testVar = "helo" will be a "global" variable for all script files in a resource (separated only by server/client side). some examples: local a = "one" -- variable is local to the script file b = "banana" -- variable is global for the resource outputChatBox(a) -- a is "one" if true then local a = "two" -- adding local a variable inside "if" block local c = "bleh" -- and some othe local variable outputChatBox(a) -- now this a is "two" outputChatBox(c) -- "bleh" end outputChatBox(a) -- outside that "if" block a is still "one" outputChatBox(c) -- and c is nil -- in some other same-side script file in the same resource: outputChatBox(a) -- nil outputChatBox(b) -- "banana"
MTA Team qaisjp Posted June 4, 2011 MTA Team Posted June 4, 2011 I already made a post noting this, please use the search next time.
megaman54 Posted June 4, 2011 Author Posted June 4, 2011 local variables have a scope limited to the block where they are declared (such as script files, functions, if statements, loops, etc).so outside of that block they are either nil or have some other value if defined. and testVar = "helo" will be a "global" variable for all script files in a resource (separated only by server/client side). some examples: local a = "one" -- variable is local to the script file b = "banana" -- variable is global for the resource outputChatBox(a) -- a is "one" if true then local a = "two" -- adding local a variable inside "if" block local c = "bleh" -- and some othe local variable outputChatBox(a) -- now this a is "two" outputChatBox(c) -- "bleh" end outputChatBox(a) -- outside that "if" block a is still "one" outputChatBox(c) -- and c is nil -- in some other same-side script file in the same resource: outputChatBox(a) -- nil outputChatBox(b) -- "banana" WOW! Thanks man. Now i know exactly what those things do! Thanks !
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now