Noblebeastx
17-03-10, 10:28 PM
The Ultimate Lua Tutorial
Introduction.
Hello everyone, and welcome to my Ultimate Lua Tutorial. This tutorial is aimed at those of you that wish to learn Lua but cannot seem to find a way to do it easily and reliably. Make no mistakes, however, this tutorial will also cover some advanced features of the programming language.
WARNING. THIS THREAD SPANS 5 POSTS. IF YOU DO NOT LIKE WALL OF TEXTS, PLEASE FIND ANOTHER TUTORIAL. TO BE EXACT, THIS TUTORIAL CONTAINS 40223 CHARACTERS.
Introduction to Lua.
Presumably you have come to learn about Lua, and as such you already have a keen understanding of what Lua is capable of. If you're able to script Lua, you are probably going to able to apply for high-ranked jobs in Private Servers, notably Developer spots, as Lua is a very powerful, lightweight language. With the addition of certain Lua Engines (Notably, LuaHypArc, created by 'hypersniper' of WoW-V.com), Lua can almost do as much as C++, and it's much, much easier to understand and write.
Pros and Cons of learning Lua.
Whilst things have their upside, everything has a downside.
Pros
Lightweight.
Powerful.
Simple.
Supported by all ArcEmu servers by default.
It's power is limitless.
"It's power is limitless" - Well, yes it is. Before you smart-asses decide to say "No it's not, <insert reason about C here>", allow me to point out that Lua's coding is based on C. So, as long as you code it in C beforehand, you can do it in Lua.
Cons
Limits you only to ArcEmu servers (Unless Mangos and Trinitycore decide to support Lua, god forbid.)
Although I said it is limitless, you are limited by what has been coded.
It has a C base, so you're better off learning C in the end.
Lua makes for a great first programming language, as it's simple heirachy (Structure) allows for easy construction of complex scripts. The reason I learnt Lua instead of jumping the gun for C++ is that it is easier to learn Lua and then C++ due to the fact that... well, let's consider Lua as pre-school books. It introduces you to the basics of programming and computer interpretation. It's not great, but at the same time, it's a good supplement for HTML.
The Basics.
This Topic is the lowest level you can get inside this forum topic. This topic consists of:
Low-level functions
Arguments
Gossip NPCs
If statements
Else & Elseif
Commenting
Variables
This tutorial assumes you are looking for ArcEmu placements, and therefore we will be focusing on the ArcEmu way of scripting. In this tutorial, I will teach you how to make a Gossip NPC. Before I start, I believe you should have these things before setting off to learn Lua:
A repack/custom compiled core that supports LuaHypArc.
Notepad++.
Some good music (I'm not talking about Miley Cyrus here. I mean AFI, All That Remains, Three Days Grace; etc).
The ArcEmu command list loaded up as a reference.
If you do not have any of these, do not worry! It is not necessary to learn Lua, but it sure will make it a heck of a lot easier. You can find Notepad++ and the ArcEmu command list by pressing the links below.
ArcEmu Default Lua Command List. (http://www.arcemu.info/wiki/index.php?title=Unit_Commands)
Notepad++ Installer (http://sourceforge.net/projects/notepad-plus/files/notepad%2B%2B%20releases%20binary/npp%205.5.1%20bin/npp.5.5.1.Installer.exe/download)
Don't be alarmed! Both of these items are open source and free (As in "Free Speech" and "Free Beer"), which means you cannot be sued (At least, by the creators) for downloading them.
Setting out into the wilderness.
The first thing I always add to my script is a block comment. This allows me to place, at the top of the script (So it is easily readable) what was on my mind when I was creating the script, what I want it to be able to do, and what needs to be done to fix it.
Comments are lines of the code that are not read by the Lua Engine. It completely and utterly ignores them. There are two types of comments; a single line comment, and a block comment.
Single Line comments
A single line comment does what it says on the tin; it blocks out the rest of the line (from where you used the comment identifier). Anything after the so-called comment identifier is completely ignored by the Lua Engine, and subsequently, ArcEmu-World. A single line comment is indentified by two hyphens placed right next to each other. An example can be seen below.
Stuff() -- This is a comment!
What this does, is that it ensures that anything after the -- is ignored by the Lua Engine. Anything placed after it is considered white space by the handler. This is a great method for ensuring that your thoughts are placed on paper.. er, virtual paper, so that you remember them later. It's all very well creating the best script known to man (Good luck with that ^^), but it's useless if you cannot remember what it does.
But what if this comment spans more than one line? Surely, it'd be messy to do this;
-- This
-- Needs
-- To
-- Be
-- Neater
-- ...!
So what in the world do we do? Well..
Block comments
A block comment is a comment that spans multiple lines until you add the closing symbol. Anything between the opening and closing identifiers are completely ignored. This is great for introductory paragraphs at the top of a script or generally indicating a gap in date/time of thought.
A block comment looks like this:
--[[
This is a block comment
It can span multiple lines!
:D
]]
You may see some scripts use this:
--]]
Instead of
]]
Please note that this is purely aesthetical and has no effect whatsoever on the code.
Anywho, knowing how to comment things is great!... right? Well, yes, but you may as well just make a text file with no symbols in it if you're just going to make a text filled lua file. So, we move onto the actual beef of the script.
Functions.
Functions are the bit (In ArcEmu) that make the script work. It's like petrol to a car. It's the meat of your script. OK, enough analogies. A function is started with the keyword function and ended with the keyword end. After the word 'function', there is a space and there you can put your function name, followed by a pair of parenthesis - (). The parenthesis are known as arguments. They basically decide which bit of the data has been passed onto us from ArcEmu-World. Here is a sample function;
function FunctionName(Unit, Event)
-- This is a function!
end
Note that the keyword 'function' is case-sensitive. Using a capital 'f' (or any other letter, for that matter) will make the compiler not recognise that it is, indeed, a function. Same goes for Function Names, Arguments and.. er, pretty much everything, really.
So, what are these bracketty things..? Well, as mentioned before, they are arguments. In pretty much every function you use as a Lua Scripter, you will have stuff inside these parenthesis. There are different arguements for different events. Here are the most common:
GeneralCreatureEvent(Unit, Event[, pMisc])
GossipOnTalkEvent(Unit, Event, player)
GossipOnSelectEvent(Unit, Event, player, id, intid, code, pMisc)
function HelloWorld()
print("Hello, World!")
print("This is my first Lua Script!")
end
HelloWorld()
Wait, what did I just do there? Well, first of all, I created the function HelloWorld with no arguments. Note that I still added a pair of parenthesis. This is mandatory for every function you create. The first line, function HelloWorld() does nothing more than tell the Lua Engine that we are beginning a new function, which we have designated to be called 'HelloWorld', and that it has no arguments.
The print statement is followed by an opening bracket, and then quotation marks (Unless you want to print a variable). Inside these quotation marks, you type whatever you want to be printed.
If you save this code as HelloWorld.lua and place it inside your /scripts/ folder, and then open up ArcEmu-World, it will print this to the console:
Hello, World!
This is my first Lua Script!
Kinda boring huh? Oh yeah, and it gets better. It'll loop itself over and over and o-.. You get the idea. It's not helpful, so we'll move onto something a bit more.. productive.
Your first Gossip Script.
Whoa, gossip?
WTF is Gossip!?
Well, you know in WoW, when you talk to an NPC it gives you options? Such as, 'Yes, I'm ready', etcetera? Well, that is gossip. And that is what we are going to create now. First of all, you need to create an NPC... go on, I'll wait.
Done? OK, set it's flags to 1. This is the 'Gossip' flag, and enables the NPC to use Gossip. Oh, you don't know how to do that? Just hop on over to WoW-V (http://www.wow-v.com/) and create an account, and then select the 'Create' option, and then the 'Mob/NPC' option. Follow the instructions, hit the 'Advanced' section and enable gossip. Then import it into your server and, at the ArcEmu-World console, type in 'reload creature_names' and 'reload creature_proto'.
Now, create a new file and at the top of it, write this:
--[[
My First Gossip Script!
Tutorial by Neglected
]]
-- Variables
local NPC_ID = YourEntryID
-- On Triggers
-- RegisterUnitEvents
Replace 'YourEntryID' with the entry ID you used when you created your NPC. Note that the only bit the Lua Engine will pay attention to, at the moment, is the local NPC_ID = YourEntryID bit. Everything else is just comments!
But wait.. what is this local NPC_ID bit anyway? Well, that brings us onto our new section. A section within a section.. handy, huh?
Introduction.
Hello everyone, and welcome to my Ultimate Lua Tutorial. This tutorial is aimed at those of you that wish to learn Lua but cannot seem to find a way to do it easily and reliably. Make no mistakes, however, this tutorial will also cover some advanced features of the programming language.
WARNING. THIS THREAD SPANS 5 POSTS. IF YOU DO NOT LIKE WALL OF TEXTS, PLEASE FIND ANOTHER TUTORIAL. TO BE EXACT, THIS TUTORIAL CONTAINS 40223 CHARACTERS.
Introduction to Lua.
Presumably you have come to learn about Lua, and as such you already have a keen understanding of what Lua is capable of. If you're able to script Lua, you are probably going to able to apply for high-ranked jobs in Private Servers, notably Developer spots, as Lua is a very powerful, lightweight language. With the addition of certain Lua Engines (Notably, LuaHypArc, created by 'hypersniper' of WoW-V.com), Lua can almost do as much as C++, and it's much, much easier to understand and write.
Pros and Cons of learning Lua.
Whilst things have their upside, everything has a downside.
Pros
Lightweight.
Powerful.
Simple.
Supported by all ArcEmu servers by default.
It's power is limitless.
"It's power is limitless" - Well, yes it is. Before you smart-asses decide to say "No it's not, <insert reason about C here>", allow me to point out that Lua's coding is based on C. So, as long as you code it in C beforehand, you can do it in Lua.
Cons
Limits you only to ArcEmu servers (Unless Mangos and Trinitycore decide to support Lua, god forbid.)
Although I said it is limitless, you are limited by what has been coded.
It has a C base, so you're better off learning C in the end.
Lua makes for a great first programming language, as it's simple heirachy (Structure) allows for easy construction of complex scripts. The reason I learnt Lua instead of jumping the gun for C++ is that it is easier to learn Lua and then C++ due to the fact that... well, let's consider Lua as pre-school books. It introduces you to the basics of programming and computer interpretation. It's not great, but at the same time, it's a good supplement for HTML.
The Basics.
This Topic is the lowest level you can get inside this forum topic. This topic consists of:
Low-level functions
Arguments
Gossip NPCs
If statements
Else & Elseif
Commenting
Variables
This tutorial assumes you are looking for ArcEmu placements, and therefore we will be focusing on the ArcEmu way of scripting. In this tutorial, I will teach you how to make a Gossip NPC. Before I start, I believe you should have these things before setting off to learn Lua:
A repack/custom compiled core that supports LuaHypArc.
Notepad++.
Some good music (I'm not talking about Miley Cyrus here. I mean AFI, All That Remains, Three Days Grace; etc).
The ArcEmu command list loaded up as a reference.
If you do not have any of these, do not worry! It is not necessary to learn Lua, but it sure will make it a heck of a lot easier. You can find Notepad++ and the ArcEmu command list by pressing the links below.
ArcEmu Default Lua Command List. (http://www.arcemu.info/wiki/index.php?title=Unit_Commands)
Notepad++ Installer (http://sourceforge.net/projects/notepad-plus/files/notepad%2B%2B%20releases%20binary/npp%205.5.1%20bin/npp.5.5.1.Installer.exe/download)
Don't be alarmed! Both of these items are open source and free (As in "Free Speech" and "Free Beer"), which means you cannot be sued (At least, by the creators) for downloading them.
Setting out into the wilderness.
The first thing I always add to my script is a block comment. This allows me to place, at the top of the script (So it is easily readable) what was on my mind when I was creating the script, what I want it to be able to do, and what needs to be done to fix it.
Comments are lines of the code that are not read by the Lua Engine. It completely and utterly ignores them. There are two types of comments; a single line comment, and a block comment.
Single Line comments
A single line comment does what it says on the tin; it blocks out the rest of the line (from where you used the comment identifier). Anything after the so-called comment identifier is completely ignored by the Lua Engine, and subsequently, ArcEmu-World. A single line comment is indentified by two hyphens placed right next to each other. An example can be seen below.
Stuff() -- This is a comment!
What this does, is that it ensures that anything after the -- is ignored by the Lua Engine. Anything placed after it is considered white space by the handler. This is a great method for ensuring that your thoughts are placed on paper.. er, virtual paper, so that you remember them later. It's all very well creating the best script known to man (Good luck with that ^^), but it's useless if you cannot remember what it does.
But what if this comment spans more than one line? Surely, it'd be messy to do this;
-- This
-- Needs
-- To
-- Be
-- Neater
-- ...!
So what in the world do we do? Well..
Block comments
A block comment is a comment that spans multiple lines until you add the closing symbol. Anything between the opening and closing identifiers are completely ignored. This is great for introductory paragraphs at the top of a script or generally indicating a gap in date/time of thought.
A block comment looks like this:
--[[
This is a block comment
It can span multiple lines!
:D
]]
You may see some scripts use this:
--]]
Instead of
]]
Please note that this is purely aesthetical and has no effect whatsoever on the code.
Anywho, knowing how to comment things is great!... right? Well, yes, but you may as well just make a text file with no symbols in it if you're just going to make a text filled lua file. So, we move onto the actual beef of the script.
Functions.
Functions are the bit (In ArcEmu) that make the script work. It's like petrol to a car. It's the meat of your script. OK, enough analogies. A function is started with the keyword function and ended with the keyword end. After the word 'function', there is a space and there you can put your function name, followed by a pair of parenthesis - (). The parenthesis are known as arguments. They basically decide which bit of the data has been passed onto us from ArcEmu-World. Here is a sample function;
function FunctionName(Unit, Event)
-- This is a function!
end
Note that the keyword 'function' is case-sensitive. Using a capital 'f' (or any other letter, for that matter) will make the compiler not recognise that it is, indeed, a function. Same goes for Function Names, Arguments and.. er, pretty much everything, really.
So, what are these bracketty things..? Well, as mentioned before, they are arguments. In pretty much every function you use as a Lua Scripter, you will have stuff inside these parenthesis. There are different arguements for different events. Here are the most common:
GeneralCreatureEvent(Unit, Event[, pMisc])
GossipOnTalkEvent(Unit, Event, player)
GossipOnSelectEvent(Unit, Event, player, id, intid, code, pMisc)
function HelloWorld()
print("Hello, World!")
print("This is my first Lua Script!")
end
HelloWorld()
Wait, what did I just do there? Well, first of all, I created the function HelloWorld with no arguments. Note that I still added a pair of parenthesis. This is mandatory for every function you create. The first line, function HelloWorld() does nothing more than tell the Lua Engine that we are beginning a new function, which we have designated to be called 'HelloWorld', and that it has no arguments.
The print statement is followed by an opening bracket, and then quotation marks (Unless you want to print a variable). Inside these quotation marks, you type whatever you want to be printed.
If you save this code as HelloWorld.lua and place it inside your /scripts/ folder, and then open up ArcEmu-World, it will print this to the console:
Hello, World!
This is my first Lua Script!
Kinda boring huh? Oh yeah, and it gets better. It'll loop itself over and over and o-.. You get the idea. It's not helpful, so we'll move onto something a bit more.. productive.
Your first Gossip Script.
Whoa, gossip?
WTF is Gossip!?
Well, you know in WoW, when you talk to an NPC it gives you options? Such as, 'Yes, I'm ready', etcetera? Well, that is gossip. And that is what we are going to create now. First of all, you need to create an NPC... go on, I'll wait.
Done? OK, set it's flags to 1. This is the 'Gossip' flag, and enables the NPC to use Gossip. Oh, you don't know how to do that? Just hop on over to WoW-V (http://www.wow-v.com/) and create an account, and then select the 'Create' option, and then the 'Mob/NPC' option. Follow the instructions, hit the 'Advanced' section and enable gossip. Then import it into your server and, at the ArcEmu-World console, type in 'reload creature_names' and 'reload creature_proto'.
Now, create a new file and at the top of it, write this:
--[[
My First Gossip Script!
Tutorial by Neglected
]]
-- Variables
local NPC_ID = YourEntryID
-- On Triggers
-- RegisterUnitEvents
Replace 'YourEntryID' with the entry ID you used when you created your NPC. Note that the only bit the Lua Engine will pay attention to, at the moment, is the local NPC_ID = YourEntryID bit. Everything else is just comments!
But wait.. what is this local NPC_ID bit anyway? Well, that brings us onto our new section. A section within a section.. handy, huh?