ArcEmu: [Lua] Using tables - ArcEmu

Jump to content

Toggle shoutbox Lastest Announcements

dfighter  : (07 December 2014 - 12:06 PM) Arcemu is in hibernation mode, please read http://arcemu.org/fo...showtopic=26903
dfighter  : (01 January 2013 - 05:56 PM) Arcemu wishes you all a happy new year!
Hasbro  : (12 September 2012 - 10:01 AM) Please excuse our outage from the web! Our web host had a major malfunction!
dfighter  : (01 September 2012 - 04:05 PM) Since the spam bots just don't want to stop, I've enabled admin verification when registering.
dfighter  : (23 January 2012 - 09:56 PM) Please note that from now on you will need to confirm your email on the wiki in order to edit it!
Hasbro  : (31 December 2011 - 12:50 PM) Happy New Years all!
Navid  : (26 December 2011 - 04:09 AM) Merry Christmas !!!!!! Happy holidays all :)
WAmadeus  : (24 December 2011 - 03:54 PM) Merry Christmas to all!
dfighter  : (24 December 2011 - 11:05 AM) The Arcemu team wishes y'all a Merry Christmukkah!
Hasbro  : (05 October 2011 - 12:53 PM) Looking for web designers for upcoming web related project. If you're interested in designing user interfaces contact me
dfighter  : (02 September 2011 - 03:47 PM) So who here wants vehicles in Arcemu? :P http://arcemu.org/fo...showtopic=25440
Hasbro  : (14 August 2011 - 03:25 PM) Join us on irc, grab an irc client and connect to irc.freenode.net join channel #arcemu /server irc.freenode.net:6667 /join #arcemu
jackpoz  : (03 August 2011 - 05:33 AM) to all Lua Engine (old one) users: please check http://arcemu.org/fo...showtopic=25274
Hasbro  : (20 May 2011 - 05:27 PM) Looking for people experienced with CMake configuration and setup! Contact me asap
Hasbro  : (15 May 2011 - 05:03 PM) ArcEmu is recruiting C++ programmers, contact Hasbro if interested.
paroxysm  : (03 May 2011 - 06:26 PM) Updated luabridge gossip example to describe the whole gossip creation process rather than just how to create menu. Gossip tutorial
paroxysm  : (23 April 2011 - 11:35 AM) Lua writers can refer to the Luabridge Tutorials section in the Wiki to learn how to write gossip code correctly.
Hasbro  : (20 April 2011 - 05:22 PM) Thank you for your continuous contribution of bug reports, we are working on them.
Hasbro  : (17 April 2011 - 03:20 AM) Please consider donating to support our bills. Donations can be sent using PayPal to donations@arcemu.org - Thank you for your support.
paroxysm  : (10 April 2011 - 12:43 AM) Refer to the Luabridge Tutorials section in the Wiki to learn the new syntax of luabridge.
Resize Shouts Area

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Lua] Using tables

#1 User is offline   hypersniper 

  • Advanced Member
  • Group: Retired
  • Posts: 227
  • Joined: 21-November 08
  • Gender:Male

Posted 16 November 2009 - 04:00 AM

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 my first one, 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 :D
This diagram will be your guide. Here is a visual representation of want we want our table to be in the end.
                      my_table
 --------------------------------------------------
| KEY   |    1    |       2       |        3       | 
|--------------------------------------------------|
| VALUE | "Hello" | "How are you" | "That is nice" |
 --------------------------------------------------

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
Posted Image


And what's inside those drawers are pieces of paper with words on them...
my_table
Posted Image

Let'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.
my_table = {}

There! We have made a table named my_table. The "{}" mean "New table" basically. So we are saying, "my_table is a new 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:
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")

Now, if we add that to our Lua file we had before, our current script should look like:
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")

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
 --------------------------------------------------
| KEY   |                                          | 
|--------------------------------------------------|
| VALUE |                                          |
 --------------------------------------------------

It's empty! We need to add some data to it, for it to become useful to us later.

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.
my_table[1] = "Hello"

Wow! Looks like to add data to a table, you put a key in between "[ ]" and say what you want the value to be.
Let's see how this looks back in the diagram!
                      my_table
 --------------------------------------------------
| KEY   |    1    |                                | 
|--------------------------------------------------|
| VALUE | "Hello" |                                |
 --------------------------------------------------

Woohoo! We are on our way. Let's put in the other two values too!
my_table[2] = "How are you"
my_table[3] = "That is nice"

And now we have our full table! Scroll up to the top to see it visually.
Once you've done those steps, your Lua file should look like:
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")

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, my_table[1])
    pUnit:SendChatMessage(12, 0, my_table[2])
    pUnit:SendChatMessage(12, 0, my_table[3])
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 part :D.

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
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")

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)
    for i=1, 6 do
        pUnit:SendChatMessage(12,0,i)
    end
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.
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...
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...


6. Let's implement the for loop into our first example!
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")

And that's how for loops are so closely related to tables!

I hope you find this easy to understand,
hypersniper
While I have striven to make this guide as clear, concise and understandable as possible, there may be something that just isn't. If that happens, please post :)
Please don't PM me asking to fix, correct or look-over your scripts. Please post a new thread in the Lua Scripting section so others can learn and help.
0

#2 User is offline   Articus43 

  • Member
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 08-June 08
  • Location:Atlanta, Georgia.
  • Interests:Gaming, solving problems, stuff like that.

Posted 29 November 2009 - 01:16 PM

I love you Hyper.

Something I'd like to add because I've gotten PM's about for loops. Hyp explained this very well. However, if your still not understanding basically a for loop is just for
i=1, 3 do
will make the following code segment repeat 3 times. So
function Hyper_onCombat(pUnit, event)
pUnit:SendChatMessage(12,0,"LuaHypArc is l33t")
for i=1, 6 do
pUnit:SendChatMessage(12,0,"Hypersniper is awesome")
end
end


"pUnit:SendChatMessage(12,0,"Hypersniper is awesome")" will repeat 3 times.

(Sorry Hyp, but I had some PM's from people on WoW-V and such still asking.) :) Still love your guides tho.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users