Likes: 0
Results 1 to 2 of 2
-
12-04-09, 01:08 AM #1
[Guide] Noob guide to LUA (your basic tutorial)
Register to remove this adSome 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:
Code:function nameoffuntion(Unit, Event) end
Code:function NPCNAME_OnCombat(Unit, Event) end
Also for the functions like
Code:function NPCNAME_OnDied or function NPCNAME_OnKilledTarget
First TUT: Making an NPC speak
We can make an NPC speak by adding this under one of our functions
Code:Unit:SendChatMessage(type, language, "what am i going to say?")
Code:function NPCNAME_OnCombat(Unit, Event) Unit:SendChatMessage(11, 0, "Hello there noob!") end
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.
Code:function NPCNAME_OnCombat(Unit, Event) Unit:SendChatMessage(12, 0, "I Shall PWN YOU!") end
Code:RegisterUnitEvent(npcentry, event, "functionname")
Now your script should look like THIS:
Code:function NPCNAME_OnCombat(Unit, Event) Unit:SendChatMessage(12, 0, "I Shall PWN YOU!") end RegisterUnitEvent(npc_entry, 1, "NPCNAME_OnCombat")
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
Code: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")
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:
Code:function NPCNAME_SpellName(Unit, Event) end
Code:Unit:CastSpellOnTarget(spellid, Unit:WhoShouldITarget) CastSpellontarget means its without casting time
Code:Unit:FullCastSpellOnTarget(spellid, Unit:whodoiattack?) this one is with casting time
Code:function NPCNAME_Spellname(Unit, Event) Unit:CastSpellOnTarget(spellid, Unit:whoiattack?) end
we do a casting time spell and have
Code:function NPCNAME_Spellname(Unit, Event) Unit:FullCastSpellOnTarget(spellid, Unit:whoiattack?) end
Code: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()
Code:Unit:RegisterEvent("functionname", timeinMS, HowManyTimes?)
Code: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")
Code:function Name_Event(pUnit, Event) local plr = pUnit:GetRandomPlayer(0) <-- Or something else if (plr ~= nil) then pUnit:FullCastSpellOnTarget(SpellID, player) end end
AoEffect Spells:
Sometimes you want your NPC to cast an AoE liked earthquake
Heres how
Code:function NPCNAME_Earthquake(Unit, Event) Unit:CastSpell(Spellid) end
Code:Unit:RegisterEvent("function_name", timeinms, howManyTimes?)
After adding that our sccript looks like this
Code: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")
Code: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
Code: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")
› See More: [Guide] Noob guide to LUA (your basic tutorial)
-
08-09-09, 05:40 PM #2
- Rep Power
- 0
- Reputation
- 52