Lua Logo
Tables

Welcome to a great speciality of LUA!

Tables are the most comfortable in LUA what I have ever seen. Not that they would be complicated very much, but they are so extensive and so good to use that we will be busy with it for a long time. Basically we are not going to stop being busy with it, because the complete LUA exists in a table.

Namely in

_G

Everything, simply everything what is known in LUA is located in the table _G.
If something is not included in _G it does not exist or it is somewhere local. (not existing or only existing if the according chunk is executed)

Furthermore, several prepared functions are available in some LUA-tables. This helps us, because we do not have to develop a lot of functions as they are already existing. And these functions are much faster as well.
Why faster? They have been written in the programming language "C" mostly. This language is very close to the operating system.
This language is not easy to program. "C" is not as easy as LUA but it is about 3 times faster than LUA.
(But LUA is very quick as well)

It seems that this introduction has been longer than the preface of the tutorial. ;) That's right

What do we know about tables?

  • It is a variable
  • As every variable it has a name
  • A table starts with {
  • and will be finished with }
  • A table can include other variables (all variables and also other tables)
  • There are 2 different kind of tables existing
  • It can be of numerical type myTable[4]
  • or it can have a name myTable.myVariable

The most important is to tell the interpreter that we are creating a table.
We do this as we initiate the table like this way:

myTable = {}

Now the interpreter knows that everything that follows and has this name belongs to this table
(it is also possible to directly assign values but this will be learned later)

nach obenTables with Names

Now we rework our script in a way, that all values are a part of a table. It will become uniform and regular. You will see.

Starting point:

player1Name = "Chepulis"
player1Gold = 1000
player1IsAlive = true
player2Name = "Nagle"
player2Gold = 1500
player2IsAlive = true

The according tables:

Initialization:

player1 = {}
player2 = {}


Assigning values Version 1

-- Player 1
player1.Name = "Chepulis"
player1.Gold = 1000
player1.IsAlive = true

-- Player 2
player2.Name = "Nagle"
player2.Gold = 1500
player2.IsAlive = true
player2.Stone = 800


Assigning values Version 2

-- Player 1
player1 = {
Name = "Chepulis", -- don't forget the comma, as this separates the different indices of a table
Gold = 1000, -- don't forget the comma
IsAlive = true, -- don't forget the comma (as this is the last index, you can also leave it)
}
-- Player 2

player2 = {
Name = "Nagle",-- don't forget the comma
Gold = 1500, -- don't forget the comma
IsAlive = true, -- don't forget the comma
Stone = 800, -- don't forget the comma (as this is the last index, you can also leave it)
}

Assigning values Version 3 (this version can be a little bit confusing; take care)

player1 = {Name = "Chepulis", Gold = 1000, IsAlive = true}


All 3 versions are absolutely equivalent. It depends on the overview and what is appropriate.

An example of a table that includes another table:

player1.position = { X = 120, Y = 360}

This has to be created like:

player1 = {}
-- we assign player 1 to be a table
player1.position = { X = 120, Y = 360}
--[[ we also assign that player1.position is a table and we assign according values]]

All versions are absolutely equivalent. It depends on the overview and what is appropriate. We are going to use all of them.

Perhaps I assume that most of these things are already known but I appreciate tables very much. So let's go on.

Version 1. What are we doing?

We assign a value to a variable. This is something we already know.
But there is a point inside. So what?
The point tells us, that on the left side there is the mother and on the right side there is her child. So far so good.

A mother can have a lot of children. And alike in real life every child is different.
And what about children of the children? This is also possible as we are not living in China. You only have to assign that there are existing children of the children.

mother = {} -- we define that there are children
mother.daughter = {} -- we define that there are children
mother.daughter.child = 5

And the child? It also can have children.

mother = {} -- we define that there are children
mother.daughter = {} -- we define that there are children
mother.daughter.child = {} -- we define that there are children
mother.daughter.child.grandchild = "dennis"

And this could go on and go on. However, as the width of our screen is not wide enough we will stop.
And if one of our children do not have a child? No problem at all. We are programming in LUA.

mother = {} -- we define that there are children
mother.daughter = {} -- we define that there are children
mother.daughter.child2 = 5
mother.daughter.child1 = {} -- we define that there are children
mother.daughter.child1.grandchild1 = "dennis"
mother.daughter.child1.grandchild2 = "Liz"
mother.daughter.child1.sizeofshoe = 3

All right? These kind of tables are very seldom but it is good know how to create and use them. However, you see that these table are hard to read.


But there was another kind of a table. The numerical one.

nach obenTables with Numbers

This kind of table is easy as well. We can assign numbers for the children and therefor a combination with loops can be done easier as well.

mother = {}
mother[1] = true
mother[2] = "lovely"

They can also have children. As well as the others. However, the variety of the possibilities makes it a little more complex now.
It is easier to walk this way:

mother = {}
mother[1] = {}
mother[1][1] = "Helena"

It seems to be crazy but the values can be 0, negative or decimal numbers. It doesn't matter at all. LUA is changing all these values in a way that we do not take notice of it. As shown above at chapter "Tables with Names".
But we take even numbers as this is easier for us.

Though, we can do an exercising what is possible.

At LUA we can do a lot of things that are impossible in other programming languages. However, this is too much for me now and I am tired, so I want to go to sleep. I will go on working with this tutorial tomorrow.

I guess, we have to learn this by an application.

-- Player 1

player1[1] = "Chepulis"
player1[2] = 1000
player1[3] = true

-- Player 2

player2[1] = "Nagle"
player2[2] = 1500
player2[3] = true
player2[4] = 800

This way is OK, but not reasonable for our purpose. We would have to keep in mind that [1] is Name, [2] is Gold, [3] is IsAlive and [4] is Stone.

If we would be able to mix it, it would be better for us. Fortunately, this is possible.

nach obenTables with Names and Numbers

player = {} -- for all following tables

-- Player 1

player[1] = {}
player[1].Name = "Chepulis"
player[1].Gold = 1000
player[1].IsAlive = true

-- Player 2

player[2] = {}
player[2].Name = "Nagle"
player[2].Gold = 1500
player[2].IsAlive = true
player[2].Stone = 800

Great, this is quite good. For example we are now able to search via a loop which player is alive or which name is Nagle or what ever we want to know.

But, we don't know the size of the table!? We are going to learn this in another chapter. Don't panic.

We can mix these methods in a lot of different ways. We only have to keep in mind to think about, what is reasonable for our purpose.

family = {}
family[1]={}
family[1].mother="Angie"
family[1].father="Michael"
family[1].childer={}
family[1].children[1]="Jessica"
family[1].children[2]="Thomas"


Another picture:
tables
Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006