PDA

View Full Version : [Guide] Noob guide to LUA (your basic tutorial)



Simple0steven
12-04-09, 01:08 AM
Some Basic LUA


How a LUA script is built

LUA is a scripting method that allows you to scripts monster's to do things likecast spells speak spawn other mobs and much more. You would mainly want to use it to script an instance


In this TUT i'm going to show you very basic LUA scripting

Everything an npc needs to do starts with FUNCTION and is going to end with END
so we have this:


function nameoffuntion(Unit, Event)
end

An Example would be


function NPCNAME_OnCombat(Unit, Event)
end

This is how the beginning of a script will look like.

Also for the functions like


function NPCNAME_OnDied
or
function NPCNAME_OnKilledTarget

Make sure that you add a RemoveEvents() so that your mob doesnt keep on looking for functions when he is not even in combat


First TUT: Making an NPC speak

We can make an NPC speak by adding this under one of our functions


Unit:SendChatMessage(type, language, "what am i going to say?")

If we wanted him to say Hello there noob! when he enters combat we do this


function NPCNAME_OnCombat(Unit, Event)
Unit:SendChatMessage(11, 0, "Hello there noob!")
end

The two most basic types are 11 and 12
11= monster say
12= monster yell

For a language anyone can understand use 0
0 is universal, so obviously everyone understands it.

Lets start a basic script.


function NPCNAME_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "I Shall PWN YOU!")
end

Now we need add a unit register for the Oncombat function and we do that by adding this to the bottom of our script


RegisterUnitEvent(npcentry, event, "functionname")


Now your script should look like THIS:


function NPCNAME_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "I Shall PWN YOU!")
end

RegisterUnitEvent(npc_entry, 1, "NPCNAME_OnCombat")

What this does is when our NPCNAME enters combat he/she will yell out I Shall PWN YOU!

Now for some more functions


Second Part: More Functions

A LUA script can have very many functions. four basic functions are:
< OnCombat >
< OnDied >
< OnKilledTarget >
< OnLeaveCombat >

Once we add those in it will look like


function NPCNAME_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "I Shall PWN YOU!")
end

function NPCNAME_OnDied(Unit, Event)
RemoveEvents()
end

function NPCNAME_OnKilledTarget(Unit, Event)
end

function NPCNAME_OnLeaveCombat(Unit, Event)
RemoveEvents()
end

RegisterUnitEvent(npc_entry, 1, "NPCNAME_OnCombat")
RegisterUnitEvent(npc_entry, 2, "NPCNAME_OnLeaveCombat")
RegisterUnitEvent(npc_entry, 3, "NPCNAME_OnKilledTarget")
RegisterUnitEvent(npc_entry, 4, "NPCNAME_OnDied")

What these will do is let our mob
enter combat
register he died
register he killed someone
register when he leave combat



THIRD PIECE: Casting Spells

Non-AoE Spell On Everyone.
An npc is pretty boring if he doesnt cast spells on you, so it's time for us to make him

We can do this by a new function in the script:


function NPCNAME_SpellName(Unit, Event)
end

Pnce you make the function, you have to add a:


Unit:CastSpellOnTarget(spellid, Unit:WhoShouldITarget) CastSpellontarget means its without casting time

or we use


Unit:FullCastSpellOnTarget(spellid, Unit:whodoiattack?) this one is with casting time

soooo if we use no casting time we get


function NPCNAME_Spellname(Unit, Event)
Unit:CastSpellOnTarget(spellid, Unit:whoiattack?)
end

OR

we do a casting time spell and have


function NPCNAME_Spellname(Unit, Event)
Unit:FullCastSpellOnTarget(spellid, Unit:whoiattack?)
end


whoiattack can be:
- GetRandomPlayer(0) this is a Random Player
- GetRandomPlayer(1) this is a Random guy in Short-Range
- GetRandomPlayer(2) this is aRandom guy in Mid-Range
- GetRandomPlayer(3) this is a Random guy in Long-Range
- GetRandomPlayer(4) = Random guy with Mana
- GetRandomPlayer(5) = Random guy with Rage
- GetRandomPlayer(6) = Random guy With Energy
- GetRandomPlayer(7) = Random guy WILL NOT BE Main-Tank
- GetMainTank()
- GetClosestPlayer()
- GetAddTank()

Ok, now I want you to add to your script, and we can do this by adding:


Unit:RegisterEvent("functionname", timeinMS, HowManyTimes?)

to your OnCombat function:


function NPCNAME_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "I Shall PWN YOU!")
Unit:RegisterEvent("NPCNAME_Spellname", 10000, 0)
end

function NPCNAME_Spellname(Unit, Event)
Unit:CastSpellOnTarget(spellid, Unit:GetRandomPlayer(0))
end[/color]

function NPCNAME_OnDied(Unit, Event)
RemoveEvents()
end

function NPCNAME_OnKilledTarget(Unit, Event)
end

function NPCNAME_OnLeaveCombat(Unit, Event)
RemoveEvents()
end

RegisterUnitEvent(npcentry, 1, "NPCNAME_OnCombat")
RegisterUnitEvent(npcentry, 2, "NPCNAME_OnLeaveCombat")
RegisterUnitEvent(npcentry, 3, "NPCNAME_OnKilledTarget")
RegisterUnitEvent(npcentry, 4, "NPCNAME_OnDied")

If your npc casts on itself instead of who you want use this


function Name_Event(pUnit, Event)
local plr = pUnit:GetRandomPlayer(0) <-- Or something else
if (plr ~= nil) then
pUnit:FullCastSpellOnTarget(SpellID, player)
end
end

Dont need to use pUnit you can if you want but better off using Unit for this script


AoEffect Spells:
Sometimes you want your NPC to cast an AoE liked earthquake
Heres how


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

Remember we need to make a register also:


Unit:RegisterEvent("function_name", timeinms, howManyTimes?)

to our oncombat function:

After adding that our sccript looks like this


function NPCNAME_OnCombat(Unit, Event)
Unit:SendChatMessage(12, 0, "I Shall PWN YOU!")
Unit:RegisterEvent("NPCNAME_Spellname", 10000, 0)
Unit:RegisterEvent("NPCNAME_Earthquake", 20000, 5)
end

function NPCNAME_Spellname(Unit, Event)
Unit:CastSpellOnTarget(Spellid, Unit:GetRandomPlayer(0))
end

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

function NPCNAME_OnDied(Unit, Event)
RemoveEvents()
end

function NPCNAME_OnKilledTarget(Unit, Event)
end

function NPCNAME_OnLeaveCombat(Unit, Event)
RemoveEvents()
end

RegisterUnitEvent(npcentry, 1, "NPCNAME_OnCombat")
RegisterUnitEvent(npcentry, 2, "NPCNAME_OnLeaveCombat")
RegisterUnitEvent(npcentry, 3, "NPCNAME_OnKilledTarget")
RegisterUnitEvent(npcentry, 4, "NPCNAME_OnDied")

More Events to See:


SendChatMessage
type:
11 – say
12 – yell
13 – whisper

Languages
0 = UNIVERSAL // Anyone can see this
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:
type:
1 = Enter Combat <Needed
2 = Leave Combat <Needed
3 = Killed Target <Needed
4 = Died <Needed
5 = AI Tick
6 = Spawn
7 = Gossip Talk
8 = Reach Waypoint
9 = On Leave Limbo
10 = Player Enters Range

A nice little example script is this


function Dark Illidan_ArcaneExplosion(Unit, Event)
Unit:SendChatMessage(12, 0, "To...much...ENNNNNNNNNEEEEEERRRRGGGGGGGYYYYYY!")
Unit:CastSpell(29973)
ArcaneTimer=math.random(25000,35000)
end

function Dark Illidan_Phase4(Unit, Event)
if Unit:GetHealthPct() < 30 then
RemoveEvents()
Unit:SendChatMessage(12, 0, "Impressive... Time for some fun!")
Unit:CastSpell(20620)
end
end

function Dark Illidan_Phase3(Unit, Event)
if Unit:GetHealthPct() < 50 then
RemoveEvents()
Unit:SendChatMessage(11, 0, "Congratulations on making it this far... but your luck has just run out")
Unit:CastSpell(45031)
end
end

function Dark Illidan_Phase2(Unit, Event)
if Unit:GetHealthPct() < 75 then
RemoveEvents()
Unit:SendChatMessage(11, 0, "I underestimated you mortals at first. It wont happen again. Let's start over and you will see what i mean!")
Unit:CastSpell(25840)
end
end

function Dark Illidan_Earthquake(Unit)
Unit:SendChatMessage(12, 0, "You will not prevail!")
Unit:CastSpell(35569)
end

function Dark Illidan_Garrote(Unit)
Unit:CastSpellOntarget(37066, Unit:GetRandomPlayer(0))
Unit:SendChatMessage(12, 0, "Haha! Gotcha!")
end

function Dark Illidan_OnCombat(Unit, Event)
Unit:SendChatMessage(11, 0, "Who dares disturb me in my lair!")
Unit:CastSpell(43648)
Unit:CastSpellOnTarget(37066, Unit:GetRandomPlayer(0)
ArcaneTimer=math.random(25000,35000)
Unit:RegisterEvent("Dark Illidan_ArcaneExplosion",ArcaneTimer,20)
Unit:RegisterEvent("Dark Illidan_Garrote",20000,4)
Unit:RegisterEvent("Dark Illidan_Earthquake",60000,10)
Unit:RegisterEvent("Dark Illidan_Phase2",1000,1)
Unit:RegisterEvent("Dark Illidan_Phase3",1000,1)
Unit:RegisterEvent("Dark Illidan_Phase4",1000,1)
end

function Dark Illidan_OnLeaveCombat(Unit, Event)
RemoveEvents()
end

function Dark Illidan_OnKilledTarget(Unit, Event)
Unit:SendChatMessage(12, 0, "You are no match for me!")
end

function Dark Illidan_OnDied(Unit, Event)
Unit:SendChatMessage(12, 0, "But...but..How! how could i be defeated by mere mortals...")
RemoveEvents()
end

RegisterUnitEvent(22, 1, "Dark Illidan_OnCombat")
RegisterUnitEvent(22, 2, "Dark Illidan_OnLeaveCombat")
RegisterUnitEvent(22, 3, "Dark Illidan_OnKilledTarget")
RegisterUnitEvent(22, 4, "Dark Illidan_OnDied")

mec
08-09-09, 05:40 PM
very nice