Chapter 0
If you know lua you can skip this (although if you know lua there is not much to learn here
)
In lua there are basic types of objects: strings (like "hello world" or 'hello worldz'), numbers (3 or 5,2), booleans or bools for short ( can only have true or false value), functions and tables. There are some more advance things but i hope that they will be easy to understand when we come to them.
Last two elements need some explaining. Functions are the most basic thing in almost all programming languages that do stuff. They can have arguments and results. It's like a normal (math) function e.g.
sin(x)=y. Here
x is argument
y is result. In lua (like other programing languages) things need to be declared before used. In math that means that first you need to say
f(x)=x*x only then you can use it to prove something or do other operations with
f. So in lua:
declarations looks like this:
function dostuff(x)
end
using it sowhere else:
y=dostuff(4) -- do stuff with 4 and assign the value thats returned to y.
You will notice that there is '--' here. It means that lua should ignore everything after this. It's called a comment. For another example lets do something more usefull:
function square(x)
return x*x -- multiply x by itself (thus squaring) and return it to be used later
end
functions can return one (or more separated with ',') value or not return any value at all. In that case there comes a special value called
'nil'. Setting something to
nil means that that item does not exist or is not set.
favourite_number=5 -- :) five my favourite number
favourite_number=nil -- on the other hand i don't need a favourite number.
Other of the more advance things (and basic building blocks) is a table. Table is basically what think it is. Every thing in one side is attached to other thing. E.g.:
box={} -- this is to create an empty table called 'box'
box['thingy']='candy' -- set item in the box called 'thingy' to string 'candy'
y=box['thingy'] -- set y to what the 'thingy' is in the box
y=box.thingy -- same thing as a line before, this is more comfortable but limited in some ways
Also you can create manipulate tables in other ways:
box={} -- lets recreate the box (forgetting the old one)
table.insert(box,"candy") -- it adds "candy" to the box table like this- first it finds last numbererd item in the box and appends candy after it.
y=box[1] -- gets "candy"
here table.insert is one of many helpful functions supplied by lua to make our life easier. Also it can be replaced (only in this example) by this:
box[1]='candy'
I hope this explains some of the most basic stuff. And if you don't understand something please ask