top of page

Lua Tables in Mudlet

Tables in Lua are basically 'advanced variables'. Normally when you create a variable, it is set globally and you can not use that same variable in another script for another function. To remedy this, we can create a sort of 'top' variable and put your global variables inside of it. By doing this you become more organized and you gain access to more advanced table features that can then be used to do some pretty amazing things. The real power in Lua comes from tables.

 

Here is a breakdown of what a table looks like.

Top-Variable

Variable 1

Variable 2

Variable 3

Variable 4

As you can see by the map, the layout is pretty simple. You can go as deep as you'd like in variables, there is no limit on how many tables-within tables you can have. I mostly use tables as namespaces so that I don't accidentally overwrite something I've written before.

Now for the How-to

Take your regular variable: target = "senoske"

and when you call on "target" it'll return with "senoske". 

 

Now, we can put target inside of another variable with it's current value. The variable name becomes a key, and "senoske" becomes the value of that key. The variable we put target into then becomes a table.

 

thisTable = { --table name and open the table
   ["target"] = "senoske"  --key/value pair
   } --close the table

 

So, now it's a table. "target" is the key and "senoske is the value".

 

Normally we'd use send("kill "..target) to call "senoske" from the variable. This doesn't change much- we'd use thisTable.target.

 

So, send("kill "..thisTable.target) would be used. The spot before the period says "Yeah, the street name we're looking for is "thisTable". The spot after says "The house number we're looking for is "target", who owns that house?"

 

So, it'll pull "senoske" out of thisTable.target and voila you have a table.

There are multiple kinds of tables. The two simplest and easiest to understand are numbered tables and libraries. 

Numbered tables (Arrays) have numbers for the keys, so like:

 

thisTable = {  --name and open
   [1] = "lala",     --key/value
   [2] = "hoho",     --key/value
   [3] = "meme",     --key/value
   }--make sure to always close your table.

 

 

And libraries look like this:

 

thisTable = { --name and open
   ["target"]  = "senoske"  --key/value
   ["ally"]    = "karai"    --key/value
   ["sensefor"]= "jalo"     --key/value

 }--close

 

You call all of these the same way, only the syntax is a little different. For a library, you'd do thisTable.target or thisTable.ally

 

For a numbered table you'd do thisTable[1] or thisTable[2].

 

If you want to loop through a table and echo its contents, you'd use FOR. There are two kinds of FOR. There is one for libraries, and one for numbered tables. 

 

For libraries you'd use:

for k,v <--these two letters don't matter. They can be words or whatever. The easiest is to just use k for key and v for value.

for k,v in pairs(thisTable) do  <--pairs is for libraries. ipairs is for numbered tables. Think of it like "integer pairs)

 

 

for k,v in pairs(thisTable) do
 echo(v)
end--for

That would echo the library like this: "senoskekaraijalo"

 

So you'd want to change the echo to:

echo(v.." ")

and it would turn out like this: "senoske karai jalo "

bottom of page