ArcEmu: [lua] Functions In Tables - ArcEmu

Jump to content

Toggle shoutbox Lastest Announcements

dfighter  : (07 December 2014 - 12:06 PM) Arcemu is in hibernation mode, please read
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
Hasbro  : (14 August 2011 - 03:25 PM) Join us on irc, grab an irc client and connect to join channel #arcemu /server /join #arcemu
jackpoz  : (03 August 2011 - 05:33 AM) to all Lua Engine (old one) users: please check
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 - 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] Functions In Tables

#1 User is offline   hypersniper 

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

Posted 17 December 2009 - 03:31 AM

Refresher on Tables
I'm sure you remember what tables are, right? If you don't, give my simple tut, and my advanced tut a read to refresh your mind. Basically tables are used to organise data, and improve efficiency. They are like a queue of people, each is numbered 1 to 10 (keys) and at each position in the queue is a person (value). But, in Lua, we can have very fancy queues and values.

Custom functions
This is a very closely related concept that not many people understand. In your scripting life, you've just made functions after RegisterEventing them, with (pUnit, event) and whatnot. I'm going to flip it all upside down so you actually understand what a function is.
Firstly, functions can be anything. They don't need pUnit or event. For example, in a Lua script you can save yourself a lot of time if you're doing something repetitive.
function Say(person, message)
   person:SendChatMessage(12, 0, message)

function unit_onspawn(pUnit, event)
   Say(pUnit, "I have now spawned")

function unit_oncombat(pUnit, event)
   Say(pUnit, "I attack!")

RegisterUnitEvent(1337, 18, "unit_onspawn")
RegisterUnitEvent(1337, 1, "unit_combat")

This may confuse you. Why is pUnit in the brackets? What is this 'person'? I'll explain.
- When creating a custom function, you can ask for whatever you want in the brackets. In my case, I need a person to send the message, and the message to send.
- These "things in the brackets" are called arguments or parameters. Custom functions allow for custom arguments. Very handy.
- Later, you can simply use that function whenever you want. Using a function is "calling" it. So in my OnSpawn event, I call the Say function with arguments pUnit and "I have now spawned". This will make pUnit say "I am now spawned", and by using the Say function, I have saved about 30 characters of typing. :(

But this leads me to another question. How come unit_onspawn and unit_combat come with free complimentary arguments, when no one said what arguments they should take? This magic happens inside the Lua engine thanks to the special RegisterUnitEvent, so you really don't have to worry about it.

Back to tables
In other news, time to learn more about tables. We learnt in the advanced tables tutorial that you can use strings as keys.
table1["hello"] = 5

There is another way you can do this. This is just an easier way to set table1's "hello" key:
table1.hello = 5

Combining our two new pieces of knowledge to form
Functions in tables! Take a look at this example:
table1 = {} --set up an empty table (MAKE SURE YOU DO NOT LOCAL THESE TABLES)
function table1.sayhello(pUnit, event)
   pUnit:SendChatMessage(12, 0, "Hello")

function table1.sayhelloimhere(pUnit)
   pUnit:SendChatMessage(12, 0, "Hello I'm here!")

function unit_onspawn(pUnit, event)
   pUnit:RegisterEvent("table1.sayhello", 1000, 0)

RegisterUnitEvent(1337, 18, "unit_onspawn")

So what this does is make unit #1337 say "Hello" every second, after saying "Hello I'm here" when he spawns.

That ends this tutorial, laying the foundations for the next tutorial that will explain how these become useful in emulation Lua.

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

#2 User is offline   P1raten 

  • Newbie
  • Group: Members
  • Posts: 5
  • Joined: 11-June 09
  • Gender:Male

Posted 21 February 2010 - 05:21 AM

Very handy. Thx. :)

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