
I will be covering various Lua script elements as if the reader has zero previous coding experience whatsoever. If you have a basic grasp of what you are doing with Lua, these guides are likely not for you. Variables: So let's start out from the ground up and make a variable: x = 1 What does a variable do? It represents a value. In this case variable "x" represents the value of "1". Pretty simple stuff huh? So now that we have a variable, what can we do with it, and how do we use it? Lets look at our previously created variable "x", and as an example we will print it: print(x) This will have an output of "1", because "x" represents the value of "1" Making sense so far? Variables can have anything whatsoever as a value (a number, string, table, function, etc.), Let's take a look: name = "Ryan" age = 20 info = function() print(name, age) end The variable name itself however must be a plain piece of text, as you can see above. Numbers and such are not valid. Now that we should have a basic idea of variables, the next thing to consider is whether we want our variable to be in the local or global environment. What does this mean? Global variables can be accessed from anywhere within a program (In this case World of Warcraft), while local variables are restricted to the file in which they are created. It's worth noting that Lua runs faster within the local environment, and a lot of the time we simply don't need things to be global. Let's look at another example, we will say we have a document called file1.lua, and another document called file2.lua. This is the contents of file1: local message = "Hello world!" otherMessage = "I'm a piece of text." Now if we wanted to to call upon the variable "message" from within file2.lua, can we do that? We can't because it's declared locally within file1.lua, however "otherMessage" is global, so we CAN use that within a different file. To declare something as a local value simply put the text "local" in front of it. If something is not declared as a local value it is automatically within the global environment. Aswell, Lua is fully lexically scoped: In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them—or semantics. Typically, scope is used to define the extent of information hiding—that is, the visibility or accessibility of variables from different parts of the program. Scopes can: * contain declarations or definitions of identifiers; * contain statements and/or expressions which define an executable algorithm or part thereof; * nest or be nested. A namespace is a scope that uses the enclosing nature of the scope to group logically related identifiers under a single identifier. Thus, scopes can affect the name resolution for their contents. Variables are associated with scopes. Different scoping rules affect how local variables are bound. This has different consequences depending on whether the language has static (lexical) or dynamic scoping. What the hell does this mean? Time to look at another example: local doSomething = function() local x = 1 end doSomething() print(x) Here we have a basic function, which simply contains a variable being created. Does this mean we can use this variable "x"? No, this would return nil, because "x" is local within that function, even if you were to call doSomething() before trying to print "x", it's not going to return any value. Why is this? It's because x is local within that function, and therefore it's not accessible because of the scope. So what if we have a variable, but we want to change it via a function? Well we can do that like so: local x = 5 print(x) We know by now that this has an output of 5, and it's within our local environment, so now we'll change it through a function, without returning a nil value such as last time: local x = 5 local doSomething = function() x = 10 end doSomething() print(x) So here we have our local variable, and then within our function we're changing the value of it from 5 to 10. However this time we can print it because the variable wasn't created locally within the function, and isn't out of our scope. If you would like to try any of the examples shown within this guide out for yourself, or play around with Lua for your own purposes, I would recommend using the Lua Demo tool provided by the official Lua website. Hopefully you've learned something about one of the most basic fundamentals of coding, and if you're hoping to learn more I may be writing more guides for the basic user.
Last updated: 16-10-2011 |