Network: WoW Gold | WoW Accounts | MPS Games | FPSowned
MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides
Homepage »      Register »      Hall of Fame »      Ranks And Awards »      Advertise »      Marketplace »
 
Sign up



Do you like this excellent information? Then Donate HERE to remove ads and support the MMOwned community.


Go Back   MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides > WoW Emulator Server > Emulator Server Guides

Emulator Server Guides Guides for working with World of Warcraft Emulator servers. Learn how to create a WoW Server here.
[NO QUESTIONS HERE]

Reply
 
LinkBack Thread Tools
  #1  
Old 06-26-2009
Dynashock is offline.
Master Sergeant
  
 
Join Date: Nov 2007
Posts: 110
Reputation: 34
Points: 510, Level: 1
Points: 510, Level: 1 Points: 510, Level: 1 Points: 510, Level: 1
Level up: 22%, 390 Points needed
Level up: 22% Level up: 22% Level up: 22%
Activity: 1.8%
Activity: 1.8% Activity: 1.8% Activity: 1.8%
[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 09:47 AM. Reason: Forgot the O at the spawning spart. >.>
Reply With Quote


Donate to remove ads, get your "DONATOR title, and get access to the MMOwned community's elite Shoutbawx.

  #2  
Old 06-26-2009
stoneharry's Avatar
stoneharry is online.
Is Bored
  
 
Join Date: Sep 2007
Location: England
Posts: 1,213
Nominated 35 Times in 1 Post
Nominated TOTM/W Award(s): 1
Reputation: 531
Points: 7,790, Level: 10
Points: 7,790, Level: 10 Points: 7,790, Level: 10 Points: 7,790, Level: 10
Level up: 18%, 910 Points needed
Level up: 18% Level up: 18% Level up: 18%
Activity: 26.4%
Activity: 26.4% Activity: 26.4% Activity: 26.4%

Easy on the eyes to read and explained well
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.
__________________
My only form of communication is [Only registered and activated users can see links. ] or private message me on here.
Reply With Quote
  #3  
Old 06-26-2009
Dynashock is offline.
Master Sergeant
  
 
Join Date: Nov 2007
Posts: 110
Reputation: 34
Points: 510, Level: 1
Points: 510, Level: 1 Points: 510, Level: 1 Points: 510, Level: 1
Level up: 22%, 390 Points needed
Level up: 22% Level up: 22% Level up: 22%
Activity: 1.8%
Activity: 1.8% Activity: 1.8% Activity: 1.8%
Yeah, I might come up with another example besides the announcer later this night. Thanks for the rep and comment.
Reply With Quote
  #4  
Old 06-27-2009
Troys is offline.
Contributor
  
 
Join Date: Oct 2006
Location: Cali-Bud
Posts: 541
Reputation: 112
Points: 1,977, Level: 3
Points: 1,977, Level: 3 Points: 1,977, Level: 3 Points: 1,977, Level: 3
Level up: 83%, 123 Points needed
Level up: 83% Level up: 83% Level up: 83%
Activity: 5.6%
Activity: 5.6% Activity: 5.6% Activity: 5.6%

+rep gave me a good idea of what a table is and how i can use this
__________________
webhosting-dealz.blogspot.com
Reply With Quote
  #5  
Old 06-27-2009
AngelSandy's Avatar
AngelSandy is offline.
Knight-Lieutenant
  
 
Join Date: Jan 2009
Location: Denmark
Posts: 316
Reputation: 19
Points: 572, Level: 1
Points: 572, Level: 1 Points: 572, Level: 1 Points: 572, Level: 1
Level up: 35%, 328 Points needed
Level up: 35% Level up: 35% Level up: 35%
Activity: 9.1%
Activity: 9.1% Activity: 9.1% Activity: 9.1%

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 ^^)
__________________
Reply With Quote
  #6  
Old 06-27-2009
Dynashock is offline.
Master Sergeant
  
 
Join Date: Nov 2007
Posts: 110
Reputation: 34
Points: 510, Level: 1
Points: 510, Level: 1 Points: 510, Level: 1 Points: 510, Level: 1
Level up: 22%, 390 Points needed
Level up: 22% Level up: 22% Level up: 22%
Activity: 1.8%
Activity: 1.8% Activity: 1.8% Activity: 1.8%
Quote:
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 01:22 PM. Reason: Explaining what my code does exactly.
Reply With Quote
  #7  
Old 06-27-2009
AngelSandy's Avatar
AngelSandy is offline.
Knight-Lieutenant
  
 
Join Date: Jan 2009
Location: Denmark
Posts: 316
Reputation: 19
Points: 572, Level: 1
Points: 572, Level: 1 Points: 572, Level: 1 Points: 572, Level: 1
Level up: 35%, 328 Points needed
Level up: 35% Level up: 35% Level up: 35%
Activity: 9.1%
Activity: 9.1% Activity: 9.1% Activity: 9.1%

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 
__________________
Reply With Quote
  #8  
Old 06-27-2009
Sounddead's Avatar
Sounddead is offline.
Contributor
  
 
Join Date: Sep 2007
Location: Canada
Posts: 1,060
Reputation: 147
Points: 2,462, Level: 4
Points: 2,462, Level: 4 Points: 2,462, Level: 4 Points: 2,462, Level: 4
Level up: 52%, 338 Points needed
Level up: 52% Level up: 52% Level up: 52%
Activity: 5.3%
Activity: 5.3% Activity: 5.3% Activity: 5.3%

This is actually really good. +repx2
__________________


Reply With Quote
  #9  
Old 07-01-2009
Heliumz's Avatar
Heliumz is offline.
Master Sergeant
  
 
Join Date: Jun 2009
Posts: 84
Reputation: 13
Points: 219, Level: 1
Points: 219, Level: 1 Points: 219, Level: 1 Points: 219, Level: 1
Level up: 55%, 181 Points needed
Level up: 55% Level up: 55% Level up: 55%
Activity: 2.6%
Activity: 2.6% Activity: 2.6% Activity: 2.6%

Oua ! love it! very useful! Thxss
Reply With Quote
  #10  
Old 07-01-2009
svedin's Avatar
svedin is offline.
Contributor
  
 
Join Date: Jun 2008
Location: Sweden
Posts: 373
Reputation: 80
Points: 2,157, Level: 4
Points: 2,157, Level: 4 Points: 2,157, Level: 4 Points: 2,157, Level: 4
Level up: 9%, 643 Points needed
Level up: 9% Level up: 9% Level up: 9%
Activity: 12.0%
Activity: 12.0% Activity: 12.0% Activity: 12.0%

Thanks, didn't know this. so Thanks

x2
__________________
Reply With Quote
  #11  
Old 07-12-2009
Mildan is offline.
Sergeant
  
 
Join Date: Feb 2009
Posts: 68
Reputation: 10
Points: 223, Level: 1
Points: 223, Level: 1 Points: 223, Level: 1 Points: 223, Level: 1
Level up: 56%, 177 Points needed
Level up: 56% Level up: 56% Level up: 56%
Activity: 0.3%
Activity: 0.3% Activity: 0.3% Activity: 0.3%

Awesome guide! Will surely come in handy! +rep
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On



All times are GMT -4. The time now is 07:59 PM.




Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490