Jump to content

[TUT] Tables


Recommended Posts

Hello everyone, I'm going to give you the basics of tables. Tables are useful for numerous things and if you are looking to create a game mode it's a must know.

So let's just dive into it:

You can think of tables as a temporary database (db), where you can store integers (numbers), characters (letters), lines of text ("such as this") and variables (which can be any of the previously stated). So before it gets too complicated let's start with a basic empty table

table = {} 

That's it, you have now created an empty table! Now let's populate it so I can give you some good examples;

table = {1,"two", "this last value is three"} 

Now that you have some values in your table you can now reference to them. Tables are indexed by default meaning in this table, table[1] = 1, table[2] = "two", table[3] = "this last value is three" So for example:

table[1] = 1

table (because that is what our table is named) [1] (because its the first value of the table)

so if that's the case then table[2] = "two" Simple isn't it?

Now here is some more advanced stuff that is good to know, You can actually add values after setting up a table, so it is not limited to the initial setup. For example:

local randomNumbers = {4, 5, 11, 10, 9} 
function addNumber(source, cmd, arg) 
     if arg == nil -- theres no specific value to add 
          arg = 1 
          arg = arg 
randomNumbers[6] = arg --- this will add a 6th value into the table 
addCommandHandler("addNumber", addNumber) 

Now you can use table.insert(table, index, value) and table.remove(value, index) but there are shortcuts and it is whatever the scripter prefers.

If you guys are interested in more complex table function let me know I will find time to whip up another tutorial. This is just the tip of the iceberg of what tables can do.

Happy scripting guys!

Link to comment

You miss a then

function addNumber(source, cmd, arg) 
     if arg == nil then -- theres no specific value to add (here you miss a then) 
          arg = 1 
          arg = arg 
randomNumbers[6] = arg --- this will add a 6th value into the table 

Link to comment

A table is a table.

Saying a table is a database can confuse programmers that don't have experience with DBs.

My suggestion is to use the Excel calc pages as examples to refer to tables.

A table is like what you see in Excel page; you can store values in rows.

To create a table, you must add this line to declare it.

tableA = { } 

Creating tables is very useful; you can make values in rows.

For example, a row would be

tableA[1] = 0 

So, we creating a value "0" in the row "1".

There are 2 kind of tables: uni-dimensional and bi-dimensional.

We already saw uni-dimensional with the little example showed above.

For the bi-dimensional tables, we can use a Excel table with columns as example; the columns are defined when you create tables inside tables, like this:

tableA[1] = { } 

The column would be "1".

Now, if we create a row inside a column, it must be like this:

tableA[1][1] = 0 

Or something like that. The more user-friendly you explain things, the better the new users will understand how does stuff like this work.

Link to comment

Yeah I stumbled upon this section and made this up around 1am my time so I apologize for small careless mistakes but for a user who never had any experience with tables I feel it would give them a good idea how they work and why they are so useful

Also I did reference them as temp db only to show that storing data are all the same, a MySQL data base is just a series of tables with indexs.

Btw good addition with the string value for table.troll completely forgot to add that and to the last post if by the time I get home from college tonight I'll write up a quick tutorial if it's not already made

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...