View Full Version : Eluna 101: Lua scripting and you! [WIP!]

04-20-2013, 12:03 AM

Part 1: Introduction to Lua

Hello everyone, and welcome to this extensive guide, covering some very important fundamental Lua knowledge, syntax and operator explanation, and an introduction to Eluna in particular.

Most of everything covered in this guide can however easily be used with Lua in general, or particularily ArcEmu Lua scripting.

Below is a "brief" introduction to what Lua is, and why it's such a great scripting language. If you don't feel like reading it, you can skip to part 2 and get straight into scripting, however I'd recommend reading part 1 as well.

- What is Lua, and why do we use it?

So I guess some of you have never even touched Lua before, and some might not even know what it is. For this reason, I'll let the Lua foundation explain it themselves with the quote below before I jump into the deep end with you!

Lua is an extension programming language designed to support general procedural programming with data description facilities. It also offers good support for object-oriented programming, functional programming, and data-driven programming. Lua is intended to be used as a powerful, light-weight scripting language for any program that needs one. Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++).

The above explanation pretty much covers what Lua is in a very technical sense. For some of you this might not make any sense at all, and we might as well just speak Chinese instead, however, since I do not know Chinese, I'll stick to English.

As the above quote states, Lua is an extension programming language designed to support general programming. What this means, is that Lua is not made to run by itself (even if you can make some pretty neat things with Lua only, do mind!), but it is made to run on top of an already built framework, and retreive and modify information based on that. In emulation, and quite a few game engines, Lua is used for everything from making GUI elements (addons, interfaces, etc.) to scripting bosses, events, and pretty much anything the framework allows for the Lua engine to use.

The reason why Lua is such a popular language to use in the gaming industry, is because of it's performance, speed and easy-to-use syntax. It is also an extremely small and lightweight language, thus making it perfect as a scripting language. Due to it's usual implementation, it's also extremely handy as you can modify, change and add scripts on the fly without having to recompile your entire project. You can simply do the changes you need to do within the script, reload the Lua engine, and the changes will (most of the time) take effect immediately!

I HIGHLY suggest you read up on the official Lua manual as well, as it will give you a much greatet understanding of the language and it's uses, found at the link below:


- Eluna and you!

This is also why Eluna, a 3rd party Lua engine developed for Trinitycore, is based on this language. It gives developers, and you, a very easy to use and powerful tool to work with that Trinitycore lacked before.

Eluna is extremely easy to implement, and very easy to use. If you read this guide, I would expect you to already have a core with Eluna implemented. If not, you can find the source and compile information here;


That thread also includes some more information about Eluna, as well as links to the source and wiki, where all the Lua methods and hooks supported are listed.

The entire forum section below is also dedicated to Lua scripts created for Eluna, so if you're looking for something specific, you can always search through it and see if you can dig something up!


Part 2: The fundamentals!

For this section I would suggest downloading Notepad++ or Lua Express, as either will make your life a lot easier when you script.

After either is downloaded, please proceed to the next part, as we'll jump straight into scripting your first script!

- Lua Express: http://emudevs.com/showthread.php?7-Win-Lua-Express-v2-0-1-2
- Notepad++: http://notepad-plus-plus.org/download/v6.3.2.html

- Functions, if's and end's!

The Lua syntax in general is very lightweight and basic in comparison to many other scripting languages, thus making it extremely easy to learn and use. In this part I'll cover a very basic function, so please open your editor of choice (I will be using Notepad++ throughout this tutorial) and bare with me as we dive into the world that is Lua!


If you are using Notepad++, I would suggest setting the syntax to Lua syntax. This will help you when you code, and it will make structuring your script a lot easier. To set the syntax, click the «Language» button on the top toolbar, go down to «L» and click «Lua». Not only does this set the syntax of the file itself, but it will also set the standard save extension to .lua, which is the standard extension of every Lua script.


Now that you have your editor open, and have your syntax set to Lua, we'll be adding the first lines of code. I will also explain what they are, and what they do.


What we have now added, is what's called a function. Functions are blocks of code to carry out specific tasks we want them to do, and they are used in pretty much any script you'd ever want to code. Let's dissect what we have so far, albeit not too much, however it's very important to learn what everything is from the get go:

(I will be using colors to show what part of the code we're talking about)

function MyFirstFunction()


Every function is defined in the script by the prefix «function». Note that Lua is CASE SENSITIVE, and function HAS to be written with a lowercase «f». Every function also has to be closed by an end. The said end defines when your block of code is complete, and without this your script will cause errors and won't run properly. Just like «function», «end» is case sensitive, and also has to be lowercase.

The functions name can be named whatever you want it to be called, but always do your best for this to be as descriptive of it's task as possible. If you create a script to spawn a creature, but call the script «IlikeCookies» it will not be very easy for you, or anyone else in the long run, to know exactly what the code does without digging into it. For reference sake, we'll be using MyFirstFunction as our function name.

This piece of code alone won't do anything at all! However, this is a very important part of the script, as anything else we want to add will be written inside this code block.

Now, how about we actually get it to do something? In true programming style, we will make the script print «Hello, World!»


Now we're getting somewhere! (Not really, but please stay motivated :P) What we have now added will be explained below:

function MyFirstFunction()
print("Hello, World!")


This is the first method we've now added to our script. "print" is used to print text for debugging or information in a script, and it will post what you write within the quotation marks in your worldserver console. Alternatively, you can use the live Lua demo to test simple script to see if your syntax is correct!

MyFirstFunction() literally triggers the function code block, executing whatever you have coded within that function. In this case, it would cause the function to trigger, causing the "print" method to print "Hello, World!".

Our above script, no matter how basic, is now actually completely functional. Head over to the live demo linked below, and copy paste your script as shown and click run:



As you can see, our first script ran successfully, and it prints "Hello, World!" in the output! Now that we have a very basic script to play around with, I will be introducing you to a couple more important parts of Lua; Statements and indentation!


01-10-2014, 10:50 AM
thanks man this is helpfull

05-15-2015, 03:50 PM
Really think you should update it and what not since I took the time to sticky it. :P

Good tutorial tho!