PDA

View Full Version : LUA Scripting Guide



StickyIcky
25-07-08, 12:36 PM
LUA SCRIPTING GUIDE


What you need:


=====================a==
1. Notepad
2. Lua Enabled on your server
=======================



Alirte. I will give you a basic example of an Lua code and explain it to you.



function Captain_Dementox_Enrage(pUnit, event)
if pUnit:GetHealthPct() < 40 then
pUnit:RemoveEvents()
pUnit:PlaySoundToSet(11273)
pUnit:CastSpell(33653)
pUnit:CastSpell(36900)
pUnit:SendChatMessage(11,0,"I have not come this far to be stopped! The future I have planned will not be jepordized! Now you will taste true power!")
end
end

function Captain_Dementox_onCombat (pUnit, Event)
pUnit:PlaySoundToSet(11263)
pUnit:SendChatMessage(11, 0, "Alas, sometimes one must take matters into one's own hands.")
pUnit:RegisterEvent("Captain_Dementox_Enrage",1000,0)
end

function Captain_Dementox_onLeaveCombat(pUnit, Event)
pUnit:RemoveAura(33653)
pUnit:RemoveAura(36900)
pUnit:RemoveEvents()
end

function Captain_Dementox_onDeath(pUnit, Event)
pUnit:PlaySoundToSet(11204)
pUnit:SendChatMessage(11,0,"Forgive me my prince....I have..failed...")
pUnit:RemoveAura(33653)
pUnit:RemoveAura(36900)
pUnit:RemoveEvents()
end

RegisterUnitEvent (26, 1, "Captain_Dementox_onCombat")
RegisterUnitEvent (26, 2, "Captain_Dementox_onLeaveCombat")
RegisterUnitEvent (26, 4, "Captain_Dementox_onDeath")

Alrite the word function is always needed. What it does is basically tell the script what you are GOING to do. The names can be whatever you want.

2. The parenthesis (pUnit, Event) declares what you ARE doing.
pUnit means npc/player, event means what it is. They are not so important. You just have to know they have to be there.

3. Now I will list the basic commands in this code:


1. pUnit:PlaySoundToSet(####) <--Plays a sound to the surrounding area

2.pUnit:SendChatMessage(11,0,"Forgive me my prince....I have..failed...") <---Makes the NPC say what is in quotes

3. pUnit:RemoveAura(####) <---Removes the spell ID that is on the unit

4. punit:CastSpell(####) <---Casts the spell ID number on itself NOT on a player

5. pUnit:GetHealthPct() <--- This allows the script to get the current percentage of the mob's health at the time it is executed.

REGISTERING YOUR EVENTS


Okay that is is a breakdown of the code. However in order to run these functions you need to tell the script when to run them and how to run them. This goes at the end of your code. In this example:


RegisterUnitEvent (26, 1, "Captain_Dementox_onCombat")
RegisterUnitEvent (26, 2, "Captain_Dementox_onLeaveCombat")
RegisterUnitEvent (26, 4, "Captain_Dementox_onDeath")

What we are telling the script to do is:

1. run the event Captain_Dementox_onComat
2. run the event Captain_Dementox_onLeaveCombat
3. run the event Captain_Dementox_onDeath

Now how does the script know when these events occur? Well that is what the little numbers are:



RegisterUnitEvent (26, 1, "Captain_Dementox_onCombat")
RegisterUnitEvent (26, 2, "Captain_Dementox_onLeaveCombat")
RegisterUnitEvent (26, 4, "Captain_Dementox_onDeath")
[code]

The number 1 signifies that this will run once the mob enters combat
The number 2 signifies that this will run when you run from a mob
4. signifies that this will run when the mob is killed.

These numbers are hard coded into your Lua engine. You can't just go making numbers up as the engine will not recognize what you are doing.

That about does it for the explanation. I have requested for an Lua section to be created on the site so that all of you may post your scripts or ask for help on them. We will see if it is done. For now I will do the best I can to help you out from here. Lua is not very easy to explain. The best way to learn is by trial and error. I will post some smaller scripts as well that you can look at.

Once you create a code in notepad you must save it as a .lua file. and then put it into the scripts folder in your ascent directory NOT the scripts_bin folder.

By default Lua is disabled in ascent, you must enable it if you compile your own version. So you will find out if your server has it when you put scripts into the script folder of ascent and see if it loads up. Many repacks do have it enabled however.

__________________________________________________ ______________________________
__________________

First we will find the mob we want to use for our script. Lets take the Kobold Vermin. Its creature ID in thottbot is 6. So lets say we want to Kobold to say "You can kiss my ass." when you aggro it. So here we go. Let start off with opening notepad. Here is the script:

We want to first make the event or function so lets give it a simple name like "Kobold Aggro" so here is how we will write the event:
[code]function koboldAggro(pUnit, event)This is the name of the function. All your events/functions will have the word function before it. The (pUnit,event) is the type of function it is. This is going to be the majority of all your scripts. When more advaced stuff comes later on these will change.

Okay now for the next part. We want to make the Kobold yell out his line rite? So here is how we do this:


pUnit:SendChatMessage(12, 0, "You can kiss my ass.")Now for the breakdown.

pUnit: is used becasue that is what you called it at the top in the (pUnit,event). This will be your most common pUnit

Now the SendChatMessage(12, 0, "You can kiss my ass.") is broken down as follows.

SendChatMessage -- this is the command we want to use to make the mob speak.
12 -- means yell. So it will appear in yell
0 -- means Universal language. Basically everyone can understand it
"You can kiss my ass" -- what the mob will say.

Now for the last part of the code.


endThis is simply as it seems. This ends the function. Make sure your ends are placed properly or your script won't work. Generally speaking you only need 1 end at the end of your function. When you get into if statements and nested statements you will need more.

So lets look at the script as a whole.


function koboldAggro(pUnit, event)
pUnit:SendChatMessage(12, 0, "You can kiss my ass.")
end

Spacing is very particular in Lua so is captials so make sure you don't make any mistakes with this. Plus everyone likes a neat code it makes things easier to read. Well we are almost done. So now we need to call this function rite? So here is how we do this.

We make a special function. I made up a name for these type of functions. I call them Primary Functions. (I don't know if that is what they are really called. ) But I call these primary functions becasue you need them to intiate your script. So lets take a look at one primary function rite now.

We call this one EnterCombat. Basically what it does is when you aggro a mob it will run the appropriate script so here is how we write this function.


RegisterUnitEvent (6, 1, "koboldAggro")Now what is this? Alrite well this as you can see doesnt have the word function written before it. That is becasue it is a special function that is loaded when the server starts. There are only 10 of these that can be used. Check the Commands sticky for examples.

Now the numbers in the code are these. the 6 is the entry ID of Kobold Vermin. The 1 means OnAggro or EnterCombat and the "koboldAggro" is the name of the function we made up top. So now ALL Kobold Vermins will do this function when you aggro them. Well I hope you understood this and it helped you out a bit. If you have more questions feel free to make a thread.

Here is your end product:

function koboldAggro(pUnit, event)
pUnit:SendChatMessage(12, 0, "You can kiss my ass.")
end


RegisterUnitEvent (6, 1, "koboldAggro")
Now save your script as Kobold.LUA and stick it in the scripts folder of your ascent server and restart your server. It is important that you save it as an lua or LUA file or it won't work.

ood Luck
Happy Scripting!

Here are things to remember about Lua:

It is EXTREMLY case sensative.
It is Precise
And you must be neat.


All Made By Halestorm

Chmun
03-08-08, 07:17 AM
Good Guide +1Rep

Aelus
06-09-08, 02:22 AM
Got a possible guide on NPC teleporters? I'm trying to add in new locations to a teleporter but when trying it, the server eathier crashes OR it doesnt work.

StickyIcky
06-09-08, 11:58 AM
Actually no sorry...if u ask one of the LUA experts...i'm pretty sure they can make you one or make a guide for one...