Welcome to the MMOtop - Arcemu, Mangos, Trinity.
+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Jerry's Avatar
    Status : Jerry is offline
    Join Date : Mar 2009
    Location : Serbia
    Posts : 2,321
    Thanks: 223
    Thanked 501 Times in 286 Posts
    Rep Power : 10
    Reputation:2077Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute Jerry has a reputation beyond repute

    Default [GUIDE] Tables, made simple

    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.

    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

    And what's inside those drawers are pieces of paper with words on them...
    my_table
    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.

    Code:
    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.

    Code:
    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!

    Code:
    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 .

    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

  2. The Following User Says Thank You to Jerry For This Useful Post:

    smokyramone (10-05-2010)


 

Similar Threads

  1. [Release] Simple WoW Server Site(To get you started)
    By Jerry in forum Emulator Website / Script's Releases
    Replies: 13
    Last Post: 03-26-2011, 03:51 AM
  2. Goldguide (Made 40k in one week)
    By Jerry in forum WoW Guides
    Replies: 1
    Last Post: 05-31-2010, 01:35 AM
  3. [Release] Simple Cool Website
    By Jerry in forum Emulator Website / Script's Releases
    Replies: 5
    Last Post: 05-03-2010, 12:44 AM
  4. Simple Trinitycore2 Repack
    By Jerry in forum Emulator Repacks
    Replies: 1
    Last Post: 03-01-2010, 05:13 AM

Visitors found this page by searching for:

Nobody landed on this page from a search engine, yet!
SEO Blog

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts