Note: Two dashes, i.e. --
, is a “comment.” This means that text after that on the line will not be read my the program as code. Comments aren’t necessary, but make it easier for the human to understand the code.
code here -- now this is a comment, not read by the program.
A variable is pretty much anything. It’s very simple. They can be numbers, strings, booleans, tables, and functions. Numbers, you can guess what those are. A “string” is a fancy programming word for text, you put them in quotes, like “a string.” A boolean is another fancy programming word that’s just a true or false value. A function can do anything you want. It’s like a separate block of code that you can run multiple times rather than writing it all over again. You could have something like this as a function:
function hi() -- This says that from here on is the code for a new function, under the name "hi."
print("Hello World!") -- This prints the text "Hello World!" into the output, or "console."
end -- This tells the computer that the code for our function "hi" is finished, and that whenever we run "hi," (i.e. hi() to run it) it should only execute the code after the keyword "function" and before the keyword "end."
print() is another function, it takes any variables you have, such as the string “Hello World!” and puts it in the output, or console.
Another thing to note is that, with most variables the definition syntax (how the text should look for it to work) is like: variableName = variable
, but also, for functions, just to make it look pretty, you can use function variableName()
instead.
Variable names can be anything that doesn’t include a =, +, -, or a space. You get it, just alphabetical characters and numbers.
In the place of “variable” back there, it can be 0, 1, 2, 3, any number, even negatives. Or it could be a string, like “Hello World!” so the code would be variableName = "Hello World!"
A boolean in that case is simple, just do: variableName = true
or variableName = false
.
A table is a list of sorts. Imagine you have a bunch of variables, say maybe 10, but you don’t want to have a name for every one of them. Then you can do something like:
myTable = {} -- Create a new variable, under the name "myTable." This variable will be a "table" variable.
myTable[1] = "string" -- This will create the first "element" in the table, to the variable "string."
myTable[2] = 0 -- This will create a second "element" in the table, to the variable 0.
-- Now we can access these variables, like this:
print(myTable[1]) -- Prints the first element in our table, "string."
print(myTable[2]) -- Prints the second element in our table, 0.
If you want to test out code like this or try programming, Lua is available for all platforms, but it doesn’t have drawing like Codea does. See lua.org for more details, tutorials, and downloads.
if statements are a little bit more complicated. They need one variable. Their “syntax” is like this: if variableName then
An example of their use is:
theBoolean = true -- Create a boolean variable, "theBoolean."
if theBoolean then -- If our variable "theBoolean" is true, run this next code:
print("It is true!") -- Simple print statement.
else -- theBoolean was false, so instead of the code above, we run this code below:
print("It is false!") -- Simple print statement.
end -- We are done with this if statement, any code after this is not affected by theBoolean.
An “else” in the middle is not required, but if you want to run some code if the variable you gave the if statement is false, then you can have it to run code for if it’s not true.
I’m not going to cover while and for statements for now, those are too complicated for a beginner.