This is not written bby me !!!
Credits: hypersniper
Due to feedback about how difficult my last tables tutorial was, I've made this one that I'm sure will be easier for you! If you had troubles with [Only registered and activated users can see links. ], please read this one, then try and follow my first one again.
Remember, YOU CAN LEARN THIS! (My motivation to you:)) PLEASE READ EVERY WORD OF THIS GUIDE SLOWLY AND CAREFULLY, WORK ALONG WITH ME
This diagram will be your guide. Here is a visual representation of want we want our table to be in the end.
0. But what would this look like in real life? In real life, a table is much like a chest of drawers. The entire chest of drawers is named "my_table", and each drawer has a number.my_table
--------------------------------------------------
| KEY | 1 | 2 | 3 |
|--------------------------------------------------|
| VALUE | "Hello" | "How are you" | "That is nice" |
--------------------------------------------------
my_table
And what's inside those drawers are pieces of paper with words on them...
my_tableLet's see how this would look like in Lua.
1. We want out table to be named "my_table". So let's set that up now. Put this at the very top of a brand new Lua file.
There! We have made a table named my_table. The "{}" mean "New table" basically. So we are saying, "my_table is a new table".Code:my_table = {}
2. Now, let's add code for an npc that says three messages when he is spawned. I want these messages to be "Hello", "How are you" and "That is nice". The code would look like:
Now, if we add that to our Lua file we had before, our current script should look like:function npc_OnSpawn(pUnit, event)
pUnit:SendChatMessage(12, 0, "Hello")
pUnit:SendChatMessage(12, 0, "How are you")
pUnit:SendChatMessage(12, 0, "That is nice")
end
RegisterUnitEvent(1337,18,"npc_OnSpawn")
3. Now let's make use of the table. In our first step we said "my_table is a new table". Let's show what it currently looks like visually.my_table = {}
function npc_OnSpawn(pUnit, event)
pUnit:SendChatMessage(12, 0, "Hello")
pUnit:SendChatMessage(12, 0, "How are you")
pUnit:SendChatMessage(12, 0, "That is nice")
end
RegisterUnitEvent(1337,18,"npc_OnSpawn")
It's empty! We need to add some data to it, for it to become useful to us later.my_table
--------------------------------------------------
| KEY | |
|--------------------------------------------------|
| VALUE | |
--------------------------------------------------
So, looking back at our first diagram, we need a key of "1" to have a value of "Hello". How on earth are we going to do that!? Let me tell you, it's simple. Just put this under your "my_table={}" line in your script.
Wow! Looks like to add data to a table, you put a key in between "[ ]" and say what you want the value to be.Code:my_table[1] = "Hello"
Let's see how this looks back in the diagram!
Woohoo! We are on our way. Let's put in the other two values too!my_table
--------------------------------------------------
| KEY | 1 | |
|--------------------------------------------------|
| VALUE | "Hello" | |
--------------------------------------------------
And now we have our full table! Scroll up to the top to see it visually.Code:my_table[2] = "How are you" my_table[3] = "That is nice"
Once you've done those steps, your Lua file should look like:
4. Now let's make all that effort worth while. Since we have put data into our table, we can use said data! Let's replace the messages in the SendChatMessage functions with the data from our table.my_table = {}
my_table[1] = "Hello"
my_table[2] = "How are you"
my_table[3] = "That is nice"
function npc_OnSpawn(pUnit, event)
pUnit:SendChatMessage(12, 0, "Hello")
pUnit:SendChatMessage(12, 0, "How are you")
pUnit:SendChatMessage(12, 0, "That is nice")
end
RegisterUnitEvent(1337,18,"npc_OnSpawn")
That's how you access data from a table! Just like giving data to a table, without the equals partmy_table = {}
my_table[1] = "Hello"
my_table[2] = "How are you"
my_table[3] = "That is nice"
function npc_OnSpawn(pUnit, event)
pUnit:SendChatMessage(12, 0, my_table[1])
pUnit:SendChatMessage(12, 0, my_table[2])
pUnit:SendChatMessage(12, 0, my_table[3])
end
RegisterUnitEvent(1337,18,"npc_OnSpawn").
5. Now you basically understand the concept of tables. I'm going to go into the for loop now. It is very closely related to tables.
New Example
Wow. That code sure is long for just saying a sequence of numbers. You guessed it, we can make it shorter using a "For" loop. I'm not gonna waste any time, here's what the code looks like with a For Loop. This code and the previous script do exactly the same thing.function numberSayer_OnSpawn(pUnit, event)
pUnit:SendChatMessage(12,0,"1")
pUnit:SendChatMessage(12,0,"2")
pUnit:SendChatMessage(12,0,"3")
pUnit:SendChatMessage(12,0,"4")
pUnit:SendChatMessage(12,0,"5")
pUnit:SendChatMessage(12,0,"6")
end
RegisterUnitEvent(1337,18,"numberSayer_OnSpawn")
That code is crazy. I don't understand where this "for" came from, or this silly "i"! If you're thinking that, good, cause I'm about to explain it.function numberSayer_OnSpawn(pUnit, event)
for i=1, 6 do
pUnit:SendChatMessage(12,0,i)
end
end
RegisterUnitEvent(1337,18,"numberSayer_OnSpawn")
for i=1, 6 do This means, "For every number between 1 and 6, store that number in i, and do something."
So, it automatically increases the variable i, starting at 1, and ending at 6. Every time it increases the variable i, it performs the stuff after the 'do'.
In detail...
6. Let's implement the for loop into our first example!when i is 1, the stuff after the 'do' becomes:
pUnit:SendChatMessage(12,0,"1")
when i is 2, the stuff after the 'do' becomes:
pUnit:SendChatMessage(12,0,"2")
And so on...
And that's how for loops are so closely related to tables!my_table = {}
my_table[1] = "Hello"
my_table[2] = "How are you"
my_table[3] = "That is nice"
function npc_OnSpawn(pUnit, event)
for i=1,3 do
pUnit:SendChatMessage(12, 0, my_table[i])
end
end
RegisterUnitEvent(1337,18,"npc_OnSpawn")
I hope you find this easy to understand,
hypersniper



LinkBack URL
About LinkBacks






Reply With Quote


Bookmarks