ArcEmu: [lua] New Way Of Scripting Extras - 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] New Way Of Scripting Extras

#1 User is offline   hypersniper 

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

Posted 26 January 2010 - 03:27 PM

Just a quick piece of info that should help people ;)
Make sure you've read and understand the original thread before reading these extras! Thanks.

1. New way of scripting, easy mode
2. Mystery of the colon revealed

New way of scripting, easy mode
With the new way of scripting came confusion, chaos, and spontaneous combustion. Therefore, I shall present an easier way to use it.
Believe it or not, the old Moon++ scripting project attempted to fix the global var problem almost two years ago. It was in the form of a "global var management" script, which held details of a setvars and getvars command. Not long ago we modified it to work correctly.

Download global_var_management.lua here. (This link will always be the latest version!)

Put it in your scripts folder, and you're good to go.

How to use:
setvars takes two arguments: the unit to store the vars with, and the vars themselves in a table.
getvars takes one argument: the unit to retrieve the vars from, and returns its stored vars in a table.
function myOnLoad(pUnit)
   setvars(pUnit, {m_phase = 0})
   local myVars = getvars(pUnit)
   if (myVars.m_phase == 0) then
      pUnit:SendChatMessage(12, 0, "Quite obviously, my phase is 0.")

Please refer to Moowarrior's guide for more examples.

- Automatically saves vars on a per-instance basis if the unit is in an instance!
- Automatically nils your tables when they're no longer needed!
- You can manually clear out a unit's or instance's vars by using true as the second argument to setvars.
- Much more freekin' simple than that other table crap!

This section is subject to modification as I have plans to add a modvars function.

Mystery of the colon revealed
Do you remember functions in tables? And how we put all our scripts functions in one global table? Here's a simple reminder script.
MY_BOSS = {}

function MY_BOSS.OnSpawn(pUnit)
   pUnit:SendChatMessage(12, 0, "I be here, chaps")

RegisterUnitEvent(12000, 18, "MY_BOSS.OnSpawn")

For the sake of the tutorial, let's say I want to make that speech optional. I'll put a setting at the top of the script, within the MY_BOSS table, again for the sake of the example.
MY_BOSS = {}

--change this to false if you don't want the boss to talk on spawn
MY_BOSS.spawnSpeech = true

function MY_BOSS.OnSpawn(pUnit)
   if (MY_BOSS.spawnSpeech == true) then
      pUnit:SendChatMessage(12, 0, "I be here, chaps")

RegisterUnitEvent(12000, 18, "MY_BOSS.OnSpawn")

Now this is fine. It will work. But, there is a more convenient way to do so.
If you use a colon instead of a dot, you get a new local variable for the function, called 'self', which contains the table on the left of the colon.
Yes, it's completely magical and random. It just comes out of no where. Seriously.

If we integrate the colon into our last example:
MY_BOSS = {}

--change this to false if you don't want the boss to talk on spawn
MY_BOSS.spawnSpeech = true

function MY_BOSS:OnSpawn(pUnit) --COLON HERE
   if (self.spawnSpeech == true) then --means we can use 'self' here
      pUnit:SendChatMessage(12, 0, "I be here, chaps")

RegisterUnitEvent(12000, 18, "MY_BOSS:OnSpawn") --COLON HERE NECESSARY TOO

That's about it. Any questions about the two parts, feel free to 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.

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