This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Jasonfran's profile picture

This may sound nooby but...

Started by Jasonfran, 07 December 2012 - 06:26 AM
Jasonfran #1
Posted 07 December 2012 - 07:26 AM
I see that sometimes people use

local variable = "Something"

Or 

variable = "Something"

When do you use local and when not?
Cranium #2
Posted 07 December 2012 - 07:33 AM
Local variables only apply to that instance, and the values will disappear once the program is terminated. Not using local will make a variable 'global', which will make it available for use across all programs, until the computer is restarted. It is generally good practice to make all of your variables and functions local, unless you have a specific reason not to.
ChunLing #3
Posted 07 December 2012 - 07:56 AM
When you initially call something and set it's scope (the level at which the memory allocated to it will remain), you use a declaration, local variable. Usually the declaration is combined with an assignment, to avoid having "got nil" errors, but the assignment is a separate operation from the declaration. You only need to declare a variable once, after that you will change the value using assignments.

If you assign a variable that has not previously been declared, then lua will make the new variable global in scope. It is good practice to avoid this when you don't have a good reason for making it global other than because you forgot to declare it.
Jasonfran #4
Posted 07 December 2012 - 07:57 AM
Thanks guys, thats gave me a good understanding