[Lua] A small introduction to tables menu

User Tag List

Results 1 to 11 of 11
  1. #1
    Dynashock's Avatar Contributor

    Reputation
    176
    Join Date
    Nov 2007
    Posts
    203
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Lua] A small introduction to tables

    [Disclaimer]: This guide's written with in mind that you have at least a basic knowledge of Lua.
    Update! Spawning example has been added.

    Intro
    I am writing this (small) introduction to tables, because at a certain point I felt I had 'mastered' the Lua used of WoW scripting, however I couldn't be any more wrong. Lua at MMOwned isn't very complex, and it only covers certain (basic) aspects of Lua scripting, in terms of guides and releases (besides some exceptions). Lua is far stronger than that and is capable at more than you'd think, especially in use with the GuaEngine (or any 'upgraded' engine). This guide will cover the basic aspects of tables and how to use them, I hope you'll enjoy reading it and may it be of any use to you.


    Tables - basics
    What are tables? See it as a sort of closet with various drawers. In each drawer you can store something. But together they are part of a much greater whole. In our case of tables; a table is the closet in which you store your information in. It's not like a 'variable' though, which can only contain a single piece of information, because a table has 'drawers' which allows a table to store much more information, each apart of each other, while they all can still be called by the table's name. The information gets stored in an organised way in the drawers assigned to keys or indexes.

    Example:
    Code:
    The variable  'pie' could contain 'cherrypie'. 
    		The table 'pie' could contain 'cherrypie', 'applepie', 'blueberrypie'.
    Syntax and tables
    I'll be covering the following stuff:

    Creating a table:
    - table
    - 'dictionary'

    Basic syntaxes of a table:
    - table.insert
    - table.remove
    - for k,v in pairs(table) do

    Creating a table
    Table
    Creating a table is very easy and are created by the table constructers { and }. It's the same how you would create a variable instead you put the information between { } and each bit of information is seperated by a comma. A piece of information is also referred to as a value. If you want to store a string you will have to use " and ", else it might try to store a table/variable instead. Numbers do not require " ". The Lua engine will automatically assign an index to each 'drawer'. Note: you can also of course store numbers, variables, tables and even functions in a table! You can show what's in a table with tablename[index/key].

    Example:
    Code:
    pie = {"cherrypie", "applepie", "blueberrypie"}
    		In this case the indexes of cherrypie would be 1, applepie would have 2, and blueberrypie 3.
    		Thus pie[2] = applepie.
    Dictionary
    A dictionary is more or less the same as a table however it allows you to give a name to each 'drawer' instead of an index number. You create one the same way you'd create a table (thus with {""}) but this time you define each name like NameOfDrawer = "1". An example to clarify:

    Example:
    Code:
    deliciousness = {pie = "blueberrypie", cake = "chocolatecake"}
    		This will store the value blueberrypie in the drawer with the key value 'pie' and the value chocolatecake in the drawer with the key value 'chocolatecake'. 
    Thus deliciousness[cake] = chocolatecake.
    Basic syntaxes of a table
    Table.insert(table, position, value)
    Position is not neccessary, if you won't provide it Lua will put the value as the last one in the row.

    Example:
    Code:
    pie = {"cherrypie", "applepie", "blueberrypie"} 	-- Creating the table.
    		table.insert(pie, 2, "blackberrypie")		-- Inserting blackberrypie at index number 2 in the table pie.
    		The result of this would be that blackberrypie will now have the 2 as index which means that applepie will now have 3 as index.
    Table.remove(table, position)
    Pretty straightforward, it removes a defined 'drawer'. Position is the index or key value of the 'drawer'.

    Example:
    Code:
    	pie = {"cherrypie", "applepie", "blueberrypie"} 	-- Creating the table.
    		table.remove(pie, 1)				-- Removes the drawer with 1 as index, which is cherrypie.
    		Cherrypie won't exist in the table pie anymore.
    For k,v in pairs(table) do
    This will go through all the elements of the table till the table's end and perform, if defined, an action on it. For instance I could print out every element as in key-value. This means that they'll show up as KEY VALUE. This is guaranteed to work with either a dictionary or a table. i,k (index-key) will also work if you're using a normal table.

    Example:
    Code:
    pie = {"cherrypie", "applepie", "blueberrypie"} 	-- Creating the table.
    		for k,v in pairs(pie) do				-- Tells the engine to go through all of the table and put the keys and values of the table in pairs and then do something with them.
    		print(pie)					-- prints out the table as in KEY	VALUES.
    		end						-- Ends the expression 'for'.
    	Output:
    		1	cherrypie
    		2	applepie
    		3	blueberrypie
    Why would you want to use tables?
    Simple. It can save time (both writing the script and running the script) and can avoid unneccesary loading. It also allows loading from external files and it can help if you want to add some easy user customisation.

    Tables and a simple (or not so simple?) announcer
    Now that we've had a basic introduction to tables, let's see what this can do for us in WoW.
    Code:
    announce1 = "Let's rock this thing!"		-- asigns the string "Let's rock this thing!"	 to the variable announce1.
    announce2 = "I haven't used tables before."		-- asigns the string "I haven't used tables before." to the variable announce2.
    announce3 = "What the heck?"				-- asigns the string "What the heck?" to the variable announce3.
    announce = {announce1, announce2, announce3}		-- puts the variables announce1, announce2 and announce3 in the table. (If you will use " " here you'll store announce1 exactly, not the variable containing the information!
    
    function Announcer_onSpawn(pUnit, Event)
    pUnit:RegisterEvent("RandomAnnounce", 60000, 0)	-- Registers the event RandomAnnounce for pUnit to do every 60000 miliseconds (= 60 seconds).
    end
    
    function RandomAnnounce(pUnit, Event)
    pUnit:SendChatMessage(14, 0, announce[math.random(1,3)])	-- This is where the magic happens. I'll explain this under here.
    end
    
    RegisterUnitEvent(ENTRYID, 18, "Announcer_onSpawn")
    A closer look at this line.

    Code:
    pUnit:SendChatMessage(14, 0, announce[math.random(1,3)])
    It sends out a chat message for pUnit but now we've replaced the string we are used to do with our announce table. It cannot yell out our table because we need to select which drawer it should use. As explained before table[index/key] will show the value for that index/key. Instead of a static number we have replaced it with a randomisation, which will draw a random number between 1 and 3 (including 1 and 3 of course).
    So what if math.random(1,3) rolls 3? This means that the element/value with the index 3 will be called, which in this case is "What the heck?". Thus pUnit will now yell out "What the heck?".

    Another example of tables in WoW
    Another possibility of tables: spawning. This is not the whole script just the spawning part.

    Code:
    CreatureID = {57000, 57001, 57003, 57004, 57005}	-- These are entry ids of the mobs we want to spawn.
    coordslocation = {500, -500, 10, 3} 				-- !Made up coordinates!. These are X, Y, Z, O.
    
    function CreatureSpawning(pUnit, Event)
    for k,v in pairs(CreatureID) do
    pUnit:SpawnCreature(v, coordslocation[1]+math.random(-5,5), coordslocation[2]+math.random(-5,5), coordslocation[3], coordslocation[4], FACTION, DURATION) -- if you're using GuaEngine you'll also have equip1, equip2 and equip3
    end	-- end for closing the 'for' statement
    end	-- end for closing the function
    A closer look at this:
    Code:
    for k,v in pairs(CreatureID) do
    pUnit:SpawnCreature(v, coordslocation[1]+math.random(-5,5), coordslocation[2]+math.random(-5,5), coordslocation[3], coordslocation[4], FACTION, DURATION)
    First of all we're putting the table in pairs again. Then we proceed to spawn a creature with entryid v which is value. The values are the elements in the CreatureID table. Because we're using in pairs it'll go through all of the table and use all of the values. So actually this tells the Lua engine to run that line with each v(alue) from the table until it reaches the end of the table, which in our case is three times (= 3 mobs!).
    It's not neccessary to put the coordinates in a table. But I'm showing it this time, because it allows easier customisation and loading from an external (text) file. Anyway, I'll explain it nonetheless.
    Coordslocation[1] means it'll open up the drawer with index 1, which is 500. It fills in 500 as X, but what's that? It's our friend math.random(-5,5) which will randomise our location a bit. It will draw a number between -5 and 5 and ADD it to the X coordinate. Same goes for the Y coordinate (= coordslocation[2]). Our Z coordinate is coordslocation[3] (= 10). And our O coordinate is coordslocation[4] (= 3)
    Now because it actually runs the line 3 times seperately it means it will also do math.random(-5,5) 3 times. This means that the location of the spawning will change for each of the mob.

    Note: You don't have to use different entryids for each mob. You can enter 57000 as many times as you'd like to and then the script will run the spawning line with entryid 57000 until the end of the table.


    Additional notes
    Thank you for reading. I hope it's been of any help to you.
    Pay Lua.org a visit. It's a great source of information and there's even more to tables than 'just' this.
    Keep on practising and messing around and you will get better.
    Last edited by Dynashock; 06-28-2009 at 07:47 AM. Reason: Forgot the O at the spawning spart. >.>

    [Lua] A small introduction to tables
  2. #2
    stoneharry's Avatar Moderator Harry

    Authenticator enabled
    Reputation
    1613
    Join Date
    Sep 2007
    Posts
    4,554
    Thanks G/R
    151/146
    Trade Feedback
    0 (0%)
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Easy on the eyes to read and explained well +Rep
    You should give some examples of what tables can be used for in terms of WoW to interest people into wanting to try and do that for themselves.

  3. #3
    Dynashock's Avatar Contributor

    Reputation
    176
    Join Date
    Nov 2007
    Posts
    203
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I might come up with another example besides the announcer later this night. Thanks for the rep and comment.

  4. #4
    Troys's Avatar Contributor
    Reputation
    122
    Join Date
    Oct 2006
    Posts
    601
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    +rep gave me a good idea of what a table is and how i can use this
    Pals 4 Life

  5. #5
    AngelSandy's Avatar Member
    Reputation
    19
    Join Date
    Jan 2009
    Posts
    330
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an Example:

    Code:
    local SpellTable =
    {123,345,567,890}
    
    function SpellChoice(pUnit,event)
          pUnit:FullCastSpellOnTarget(math.random([SpellTable]),pUnit:GetRandomPlayer(0))
    end
    This should be a valid function.
    What it does, is that it chooses a random spell in the Table I defined and casts it on a random Player in range.

    (If it's not entirely correct please tell me ^^)

  6. #6
    Dynashock's Avatar Contributor

    Reputation
    176
    Join Date
    Nov 2007
    Posts
    203
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by AngelSandy View Post
    Here is an Example:

    Code:
    local SpellTable =
    {123,345,567,890}
    
    function SpellChoice(pUnit,event)
          pUnit:FullCastSpellOnTarget(math.random([SpellTable]),pUnit:GetRandomPlayer(0))
    end
    This should be a valid function.
    What it does, is that it chooses a random spell in the Table I defined and casts it on a random Player in range.

    (If it's not entirely correct please tell me ^^)
    Not entirely correct.

    It should be (the rest of your code is correct):
    Code:
    pUnit:FullCastSpellOnTarget(SpellTable[math.random(1,4)], pUnit:GetRandomPlayer(0))
    What this tells Lua is: open drawer math.random(1,4) (so that's either 1, 2, 3 or 4) from the SpellTable table. So if it's 3, it'll open drawer 3 with as value 567.

    You cannot use tables in math.random unless you define which values in the tables you want to use, like SpellTable[1]. Also you put SpellTable between [] and that tells Lua to open a table with SpellTable as key. But you haven't defined which table you want to open so it just doesn't work.
    Last edited by Dynashock; 06-27-2009 at 11:22 AM. Reason: Explaining what my code does exactly.

  7. #7
    AngelSandy's Avatar Member
    Reputation
    19
    Join Date
    Jan 2009
    Posts
    330
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes.

    I couldn't remember if it was that way or, the other way around ^^

    FIXED EXAMPLE
    Code:
    local SpellTable =
    {123,345,567,890}
    
    function SpellChoice(pUnit,event)
          pUnit:FullCastSpellOnTarget(SpellTable[math.random(1,4)], pUnit:GetRandomPlayer(0))
    end

  8. #8
    Sounddead's Avatar Contributor
    Reputation
    160
    Join Date
    Sep 2007
    Posts
    1,126
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is actually really good. +repx2

    I live in a shoe

  9. #9
    Heliumz's Avatar Member
    Reputation
    26
    Join Date
    Jun 2009
    Posts
    113
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oua ! love it! very useful! Thxss +Rep

  10. #10
    svedin's Avatar Contributor
    Reputation
    124
    Join Date
    Jun 2008
    Posts
    557
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, didn't know this. so Thanks

    +Rep x2

  11. #11
    Mildan's Avatar Member
    Reputation
    10
    Join Date
    Feb 2009
    Posts
    80
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome guide! Will surely come in handy! +rep

Similar Threads

  1. [Release] LUA-Commands as SQL DB-Table
    By sheepking in forum WoW EMU General Releases
    Replies: 4
    Last Post: 10-16-2009, 07:46 AM
  2. A small introduction to HTML and CSS
    By v1pera in forum Programming
    Replies: 4
    Last Post: 02-26-2009, 09:57 AM
  3. [Request] A small lua help.
    By Ellenor in forum WoW EMU Questions & Requests
    Replies: 1
    Last Post: 08-30-2008, 01:33 AM
  4. [Just Another Small Release] Second LUA Boss I made :)
    By The_Zealot in forum World of Warcraft Emulator Servers
    Replies: 1
    Last Post: 03-01-2008, 09:59 PM
  5. [Small-Release] Understanding Itempetfood table
    By The_Zealot in forum World of Warcraft Emulator Servers
    Replies: 18
    Last Post: 02-14-2008, 04:13 PM
All times are GMT -5. The time now is 04:31 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search