Cholzzon Spammer?

Posts: 30 Join date: 2008-05-08 Age: 15 Location: Sweden
 | Subject: How to Script with Lua ( Credit's to Darhan! ) Fri May 09, 2008 3:32 pm | |
| The Basics of Lua - ImprovedThe 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] [code]function Example_OnCombat(Unit, Event) end[/code]
This is how the beginning of a LUA script looks like.
[color=cyan] 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")[/code]
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[/code]
The Most used for type = 11 or 12 11= monster say 12= monster yell
And the most used for Language = 0 0 = universal, so every race can understand it.
Lets fill it in:
Code:
[code]function Example_OnCombat(Unit, Event) Unit:SendChatMessage(12, 0, "My Tutorial is ownage") end[/code]
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")[/code]
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")[/code]
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 - OnDied - OnKilledTarget - OnLeaveCombat
If we add those functions to our script our script will look like:
Code: [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")[/code]
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:
[code]function Example_Spellname(pUnit, Event) end[/code]
And if you have made the function, you need to add a:
Code: [code]pUnit:CastSpellOnTarget(spellid, pUnit:Getwho?) = Without casting time[/code]
or
Code: [code]function Example_Spellname(pUnit, Event) pUnit:CastSpellOnTarget(spellid, pUnit:Getwho?) end[/code]
Code: [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() *[/code]
Ok, now I want to add it to my script, and we can do that by adding a:
Code: [code]Unit:RegisterEvent("nameofthefunction", timeinmiliseconds, howmuchtimes)[/code]
to the prefered function, in this case to the OnCombat function:
Code: [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")[/code]
If you got some problems with the targeting system, like that the NPC cast it on itself. Then try this code: 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[/code]
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: [code]function Example_Blizzard(Unit, Event) Unit:CastSpell(Spellid) end[/code]
And we also need to register our function with the: Code: [code]Unit:RegisterEvent("nameofthefunction", timeinmilisec, howmuchtimes)[/code]
to the OnCombat Function:
Our script will look like this if we do that:
Code: [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")[/code]
Other events:
Code: [code]SendChatMessage Events: type: 11 – npc say = Nice for little gossips 12 – npc yell = Cool for boss fights 13 – npc whisper
Language: 0 = UNIVERSAL <= I use this 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[/code]
Credit's go to Darhan at Ac-Web.org Tutorial is From here
Last edited by Cholzzon on Fri May 09, 2008 3:37 pm; edited 2 times in total |
|
Cholzzon Spammer?

Posts: 30 Join date: 2008-05-08 Age: 15 Location: Sweden
 | Subject: Re: How to Script with Lua ( Credit's to Darhan! ) Fri May 09, 2008 3:35 pm | |
| Very odd that the Coding did not work... just worked one =/ |
|