Forums » MacOS X

Lua Scripting

Jun 01, 2007 Zed1985 link
Any of you guys know a good free tutorial site where one can learn lua?

Something like 3schools.com, but they don't have lua :(
Jun 05, 2007 Person link
I wrote a quick tutorial a while back. It'll get you started at least:

#!/usr/local/bin/lua5.1
--The header line at the top is not necessary, it just makes life easier on a mac.
--that double minus is a comment. It means that Lua does not interpret anything else on the line. This line is wrapped around, so even though this looks like it's on three lines, it will not interpret any of this.

print("It will however, interpret this.") --there's your first command. print prints! surprise! It doesn't print to your printer though, just to whatever command line interface you called the Lua script from. So on a mac, I access Lua through Terminal. It would print whatever's in the () to that terminal. Let's make it print something else.

print(2) --you probably noticed how there weren't quotes around the two. That's because quotes denote a string in Lua. A string is just a list of letters like "Haydan wants to learn Lua" or even just "a" Before actually displaying stuff, print automatically converts everything to a string. 2 is just a number, so it doesn't need to be denoted as a string, because print will try to make it a string before it actually prints. Fortunately for us, it can.
--This has to do with one of the greatest features of Lua: dynamic typing. In English, that means that you do not have to define the TYPE of a variable and stick to it religiously, like in C, Java, and many others. For example, you could write the following code, and you wouldn't come up with an error:

a = "this" --the value of the variable 'a' is now the STRING "this"
a = 1 --the value of the variable 'a' is now the NUMBER 1
a = true --the value of the variable 'a' is now the BOOLEAN true.
a = {"a", "b"} --the value of the variable 'a' is now the LIST {"a", "b"}
a = nil --the value of the variable 'a' is now unassigned. 'a' is recycled

--These are also the five basic types, although there are more advanced ones you will learn later. Another note about 'nil'. 'nil' is special in Lua, it is not a value, but the absence of a value. So let's say we having defined 'b' yet. If you wrote something like:

print((b == nil)) --'true' would be printed to your command line.

--Now let's look at if then statements. We're going to write a simple program to greet you.

print("What is your name?") --ask user's name
name = io.read("*line") --read and store user's name in 'name' variable
if name == "Calder" --'if' statement to determine if "Calder" is using it
then --Lua will only run the following line if (name == "Calder") is true
print("Good day to you, my holy creator!") --prints special hello
else --Lua will only run the following line if (name == "Calder") is false
print("Good day, " .. name) --The '..' glues things together. LATER
end --end of if statement

--Okay, back to the '..', there's a couple more things worth noting. Whenever you call a function, whether you've written it or it's part of Lua like print, you should always enclose the arguments in (). Arguments are the input values Lua feeds to the function. For print, you put the arguments in parenthesis just like anything else. Remember how print automatically tries to convert non-strings to strings? Well, that only works when you all non-string or all string arguments. If we have an multiple things we want to tie together into one string, so that print can actually print it, we have to use the '..', or concatenation function. So if you do the following, you have to use a '..'.

print("How old are you?") --ask user's age
age = io.read("*line") --read and store user's age as variable 'age'
print("Next year, you will be " .. (age + 1) .. " years old.") --LATER

--There's a simple print function. It tapes (age + 1) to the end of "Next year, you will be " and then tapes " years old." to the end of that string. After all this doctoring, the final string is fed to print. There are two things to note from this example:
--If you try to concatenate a 'nil' value, an undefined variable for instance, you will receive an error. Lua can add numbers, booleans, and of course strings to strings, but if you have to write a special function to interpret lists, tables, 'nil' or others into string form.
--You might have noticed the () around 'age + 1'. In Lua, you can also use parentheses just like in math, to tell the program to evaluate them before evaluating the rest of a command. In this case, we want 'age + 1' to be evaluated before it's concatenated so we don't run into any possible errors.

--The last thing you need to know about are tables. Tables are the only advanced data structure in Lua. Tables are not given a definite length, individual entries are simply defined when they are needed. You create a table by making an empty list, and then defining values from there. Before I can show you anything, I need to explain about indexes and values. Tables in Lua are stored as one variable, but each value inside a table is stored under a 'index' or key. The index, is basically a number or string that a specific value is bound to. For example:

c = {} --makes 'c' a table
c[1] = "this " --defines cell in 'c' with INDEX 1 and VALUE "this"
c[2] = "is " --defines cell in 'c' with INDEX 2 and VALUE "is"
c[3] = "a table" --defines cell in 'c' with INDEX 3 and VALUE "a table"
--of course, you can also use non-numerical indexes, like:
c["a"] = "a" --defines cell in 'c' with INDEX "a" and VALUE "a"
c["Calder"] = "Nerd" --defines cell in 'c' with INDEX "Calder" and VALUE "Nerd"
--Now, if you wanted to access these values in the table later, all you have to do is call them by their index. Like:
print(c[1] .. c[2] .. c[3] .. " in string form") --prints table as string
--or
print("Calder is a " .. c["Calder"])
--There you have it ladies and gentlemen: TABLES!!!

--You know what's great about the way we wrote the tutorial? By commenting all non-code out, we've made it so this is actually a valid Lua program. Now let's try running it and see what happens.
--There you go, it runs correctly!

--Well, that's about it for your first Lua tutorial. Once you understand this stuff, you'll be able to keep extending your Lua vocabulary through writing programs, trying stuff out, and looking at other people's programs. I'll include a couple quick examples with this tutorial.
Jun 05, 2007 who? me? link
ok now how do you program AI with this?