The Basics of Lua - Improved


The Build of a LUA script:

Lua is a nice scripting method that allows you to scripts npc's.
You can let them cast spells and let them say things, so this is great if you want to script
an instance or a event boss/mob.

I will teach you the basics of LUA today, and we will start with the base of a script.
Everything that a npc needs to do starts with; FUNCTION and ends with END
so we get this:

Code:
function functionname(pUnit or Unit, Event)
end

So if we fill it in we get this:

Code:
function Example_OnCombat(Unit, Event)
end
This is how the beginning of a LUA script looks like.


Tutorial 1: The SendChatMessage Part
Ok, Now its time to let the npc say something.
We can do that by adding this:

Code:
Unit:SendChatMessage(type, language, "Some text here")

To the Example_OnCombat function and if we do that it looks like:

Code:
function Example_OnCombat(Unit, Event)
Unit:SendChatMessage(type, language, "Some text here")
end

The Most used for type = 12 or 14
12= monster say
14= monster yell

And the most used for Language = 0
0 = universal, so every race can understand it.

Lets fill it in:

Code:
function Example_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "My Tutorial is ownage")
end

Now we need to register the OnCombat function and we can do that if we add a: to the bottem of the script
Code:
RegisterUnitEvent(entryidofthenpc, event, "functionname")

If we do that our script will look like:



Code:
function Example_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "My Tutorial is ownage")
end
RegisterUnitEvent(entryidofthenpc, 1, "Example_OnCombat")


This means if you enter his range and he starts to attack, he will yell: My tutorial is ownage.

Lets move on to the next part.



Tutorial 2: Adding More Functions

A LUA script can have more functions. The four basic functions are:
- OnCombat: Already added in the script tongue.gif
- OnDied
- OnKilledTarget
- OnLeaveCombat

If we add those functions to our script our script will look like:

Code:
function Example_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "My Tutorial is ownage")
end

function Example_OnDied(Unit, Event)
Unit:RemoveEvents()
end

function Example_OnKilledTarget(Unit, Event)
end

function Example_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end

RegisterUnitEvent(entryidofthenpc, 1, "Example_OnCombat")
RegisterUnitEvent(entryidofthenpc, 2, "Example_OnLeaveCombat")
RegisterUnitEvent(entryidofthenpc, 3, "Example_OnKilledTarget")
RegisterUnitEvent(entryidofthenpc, 4, "Example_OnDied")
This will allow the mob to do things when he:
- Starts to fight
- Dies
- Killed someone
- Leave combat



Tutorial 3: Adding spells to the NPC

Non Area on Effect Spell;On Targets.
A scripted npc is kinda boring without spells, so lets add them!
We can do that by adding another function to the script:

Code:
function Example_Spellname(pUnit, Event)
endp

And if you have made the function, you need to add a:
Code:
pUnit:CastSpellOnTarget(spellid, pUnit:Getwho?)         = Without casting time
or
Code:
pUnit:FullCastSpellOnTarget(spellid, pUnit:Getwho?)  = With casting time
So if we do that we get this:

Code:
function Example_Spellname(pUnit, Event)
pUnit:CastSpellOnTarget(spellid, pUnit:Getwho?)
end


Code:
*Getwho can be:
- GetRandomPlayer(0) = Random Player
- GetRandomPlayer(1) = Random in Short-Range
- GetRandomPlayer(2) = Random in Mid-Range
- GetRandomPlayer(3) = Random in Long-Range
- GetRandomPlayer(4) = Random with Mana
- GetRandomPlayer(5) = Random with Rage
- GetRandomPlayer(6) = Random With Energy
- GetRandomPlayer(7) = Random NOT Main-Tank
- GetMainTank()
- GetClosestPlayer()
*

Ok, now I want to add it to my script, and we can do that by adding a:

Code:
Unit:RegisterEvent("nameofthefunction", timeinmiliseconds, howmuchtimes)
to the prefered function, in this case to the OnCombat function:

Code:
function Example_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "My Tutorial is ownage")
Unit:RegisterEvent("Example_Spellname", 10000, 0)
end

function Example_Spellname(pUnit, Event)
pUnit:CastSpellOnTarget(spellid, pUnit:GetRandomPlayer(0))
end

function Example_OnDied(Unit, Event)
Unit:RemoveEvents()
end

function Example_OnKilledTarget(Unit, Event)
end

function Example_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end

RegisterUnitEvent(entryidofthenpc, 1, "Example_OnCombat")
RegisterUnitEvent(entryidofthenpc, 2, "Example_OnLeaveCombat")
RegisterUnitEvent(entryidofthenpc, 3, "Example_OnKilledTarget")
RegisterUnitEvent(entryidofthenpc, 4, "Example_OnDied")

If you got some problems with the targeting system, like that the NPC cast it on itself.
Then try this code:
Code:
function Name_Event(pUnit, Event)
local plr = pUnit:GetRandomPlayer(0) <-- Or something else
if (plr ~= nil) then
pUnit:FullCastSpellOnTarget(SpellID, plr)
end
end


Area on Effect Spell:
Its also nice to let the npc cast an AoE spell like Blizzard.
We can do that by adding this function to the script:

Code:
function Example_Blizzard(Unit, Event)
Unit:CastSpell(Spellid)
end

And we also need to register our function with the:
Code:
Unit:RegisterEvent("nameofthefunction", timeinmilisec, howmuchtimes)
to the OnCombat Function:

Our script will look like this if we do that:

Code:
function Example_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "My Tutorial is ownage")
Unit:RegisterEvent("Example_Spellname", 10000, 0)
Unit:RegisterEvent("Example_Blizzard", 20000, 5)
end

function Example_Spellname(pUnit, Event)
pUnit:CastSpellOnTarget(spellid, pUnit:GetRandomPlayer(0))
end

function Example_Blizzard(Unit, Event)
Unit:CastSpell(Spellid)
end

function Example_OnDied(Unit, Event)
Unit:RemoveEvents()
end

function Example_OnKilledTarget(Unit, Event)
end

function Example_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end

RegisterUnitEvent(entryidofthenpc, 1, "Example_OnCombat")
RegisterUnitEvent(entryidofthenpc, 2, "Example_OnLeaveCombat")
RegisterUnitEvent(entryidofthenpc, 3, "Example_OnKilledTarget")
RegisterUnitEvent(entryidofthenpc, 4, "Example_OnDied")



Other events:

Code:
SendChatMessage Events:
type:
12 – npc say   = Nice for little gossips
14 – npc yell  = Cool for boss fights

Language:
0 = UNIVERSAL <= I use this one the most
1 = ORCISH
2 = DARNASSIAN
3 = TAUREN
6 = DWARVISH
7 = COMMON
8 = DEMONIC
9 = TITAN
10 = THELASSIAN
11 = DRAGONIC
12 = KALIMAG
13 = GNOMISH
14 = TROLL
33 = GUTTERSPEAK
35 = DRAENEI

RegisterUnitEvents events:
eventtype:
1 = Enter Combat  <= Important
2 = Leave Combat  <= Important
3 = Killed Target <= Important
4 = Died          <= Important
5 = AI Tick
6 = Spawn         <= Also a nice one
7 = Gossip Talk
8 = Reach Waypoint
9 = On Leave Limbo
10 = Player Enters Range
Credits to darhan


› See More: LUA Basics