Tables

From GMod Wiki

(Difference between revisions)
Jump to: navigation, search
 
Line 91: Line 91:
</lua>
</lua>
-
If you want use string as indexes you also can do this like this more advanced way:
+
If you want use string as indexes you also can do this like this more advanced way.
-
Just use a point between the table name and the index and remove the square brackets and the quotes.
+
Just put a point between the table name and the index and remove the square brackets and the quotes.
But you can't replace with a variable though.
But you can't replace with a variable though.

Latest revision as of 17:51, 5 March 2011

Tables are used to store several variables.

Typically a table looks like this:

 
myTable={} 
 

As you can see, the only difference between making a variable and making a table is that tables are set to {}. Essentially, {} is an empty table.

The { and } are called curly brackets, and they're only used to create tables in Lua.

Now don't get me wrong here, tables are very useful. I use them all the time.

Here's an example of their use:

 
myTable={} 
myTable[1]="First Entry" 
myTable[2]="Second Entry" 
myTable[3]="Third Entry" 
 

So this might be a little confusing. Let me explain.

Tables are kind of like a row of lockers. Each locker has a number on it, called an index. The index is always put inbetween a [ and a ] (these are called square brackets)

Whenever we do myTable[1]="First Entry", we're putting a string in the first locker, so to speak. That locker is full now (by full I mean you placed something in it). So you could say that the code says "myTable's first locker should have "First Entry" in it".

With me so far?

So whenever we do myTable[2]="Second Entry", we're putting "Second Entry" in myTable's second locker.

So far I've been calling the table's spaces lockers. But the common term for these 'lockers' is a slot.

So, what can tables do? Well for one thing, they keep your data organized.

 
playerScore={} 
playerScore[1]=0 
playerScore[2]=0 
playerScore[3]=0 
 

The table I made here stores scores for players, by their number. So, playerScore[1] is player 1's score, playerScore[2] is player2's score, and so on.

Now I'll show you how to grab a variable from the table.

 
playerScore={} 
playerScore[1]=0 
 
Msg(playerScore[1].."\n") 
 

The above example prints a 0 in the console, because playerScore[1] is 0.

Yeah, pretty easy right? You just need to type the name of the table, and a pair of square brackets with the index in it.

So lets see what else we can do with tables.

See, in Lua, table indexes should start at 1 and keep going up, like you can see below:

 
myTable={} 
myTable[1]=0 
myTable[2]=0 
myTable[3]=0 
myTable[4]=0 
 

1, 2, 3, 4. Like I said, starting at 1 and going up. But they don't have to:

 
myTable={} 
myTable[240]=25 
myTable[231]=90 
myTable[92]=48 
myTable[119]=52 
 

You can store variables in any index in a table. Anytime you use an index that hasn't been used before, it makes a new slot. Like you can see above, I used 240 for an index, but that doesn't mean there are 240 slots. As you can see above there are only four slots.

You can also use text to index a table (although I don't really recommend this, setting and getting info this way is slightly slower than numbers).

 
myTable={} 
myTable["george"]=1 
myTable["alex"]=3 
myTable["orange"]=4 
 

If you want use string as indexes you also can do this like this more advanced way. Just put a point between the table name and the index and remove the square brackets and the quotes. But you can't replace with a variable though.

 
myTable={} 
myTable.george=1 
myTable.alex=3 
myTable.orange=4 
 

You can get the values also like this or like the normal way you have to use when you use string indexes with those square brackets:

 
Msg("The number is "..myTable.george.."\n") 
Msg("The number is "..myTable.alex.."\n") 
Msg("The number is "..myTable.orange.."\n") 
 

That would print 'The number is "1"', 'The number is "3"' and 'The number is "4"' to the console. This way of returning also works for normal string indexes.

Like you can see above, we're using a string for a table index instead of a number.

You can even use another variable to index a table.

 
names={} 
names[1]="Jeff" 
names[2]="Josh" 
names[3]="Ryan" 
 
function sayName(index) 
    Msg("I said "..names[index].."\n") 
end 
 
sayName(1) 
sayName(2) 
sayName(3) 
 

So in the script above, I've got a table with names from 1 to 3, a function, and then I call it three times.

See the function I made? index is it's argument if you remember from the lesson before. Whenever sayName is called, it uses index as, duh, an index for names. So whenever I do:

 
sayName(1) 
 

the function does:

 
Msg("I said "..names[1].."\n") 
 

Want to try this for yourself?

Open up Notepad as per usual... We'll make a simple script to keep track of how much of each thing is available, and then have messages say it. Write this, change it around a bit if you want.

 
shelves={} 
shelves[0]=20 
shelves[1]=15 
shelves[2]=17 
shelves[3]=35 
 
function howMany(onWhat) 
    Msg("There are "..shelves[onWhat].." things on that shelf.\n") 
end 
 
howMany(0) 
howMany(1) 
howMany(2) 
howMany(3) 
 

Save it as tables.lua, and load it in Garry's Mod. If everything worked correctly you should get this in the console:

There are 20 things on that shelf.

There are 15 things on that shelf.

There are 17 things on that shelf.

There are 35 things on that shelf.

That's it for this lesson.


Yarin Kaul Icon ArrowSquare32 left.png Functions

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

Comments Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox