Welcome to my Lua Scripting Tutorial.
This isn't guide. Its a tutorial, I will be teaching you how to script a boss in Lua and the programming concepts behind it. Thats what seperates this from every other tutorial. Most are not tutorials - merely a guide to show you what to do.
After finishing this tutorial you will have a good understanding of atleast basic programming, giving you the ability to read most programming languages.
Oddly enough, I'm gonna start with something that most people find completely unimportant. Commenting. Regardless of alot of peoples opinions, commenting is very important. Its alot easier to read a message you wrote to youself as opposed to actual code. Lets say you wanted to implement a phasing system in your script. It would be irritating reading for numbers and just deciphering all of the If's so it would be easier just to comment it like this
Code:
if(pUnit:GetHealthPct() == 90) then--start phase 2
-- Phase 2 Code
elseif(pUnit:GetHealthPct() == 50) then--start phase 3
-- Phase 3 Code
end
Now you dont have to decipher that code - you know what it does and I didn't even explain it to you. On alot of more advanced programming sites - a Tutorial is just heavily commented code. And its very helpful.
Now I'm sure you noticed in the code above the word 'if' next to an opening parenthesis, and then the comment begining after a closing one. That is what we call a conditional statement. In my opinion its a huge oxymoron. It a feature that advances programming capabilites, by limiting things. For example if you request a file in a program. And you check if the file exists and it doesn't. Clearly you cant continue the code you had planned on using because the file that you required doesn't even exists. So you have to bail from it - and to do this, you check for if the file exists if so - execute your code if not then do nothing, or return a message alerting the user that their file didn't exist.
Like this
*Note that this is not real functions to my knowledge just used as an example
Code:
if(exists(File(name)) then
print("The File Exists")
else
print("The FIle Does Not Exist")
end
When your using a conditional statement you have your initial 'if' statement. Remember you must use an 'if' statement before you can do 'elseif' or 'else'. What will happen inside of a conditional statement is when the code is ran - it will start by seeing if the conditions of the first 'if' statement are true. If so it will execute the code designated to be executed
Another important feature in Lua and programming in general is the ability to make a function. For this were gonna reference our phase code from above, what a function allows you to do is gather a bunch of commands into a group. So when we move into phase2 we could put all of the code into a function and then just call the function. It sounds like more work - and in this case it is. But if you were to use the same commands over and over again - it would be a lot easier to just create a function and call it every time instead of retyping the function. Lets create our first function.
Code:
function GoPhase2()--Our phase function
--Some fake commands to show concepts
pUnit:SetPhase(2) -- This is a real function but used improperly
pUnit:GiveSpell(482)
pUnit:GIveSpell(4723)
pUnit:CanFly(true)
end
We have those 4 functions typed inside of our new functions. Now inorder to use this function we can simply type someone down the line
Relatively simple, just like most Lua scripting. Its not made to be hard, and they did a very good job on that. You ever notice how in life, things can change, like i dont know, a variable. Well there are variables in Lua as well. So say you wanted to be able to change something and then use it later - a variable is the way to go. I once again bring you to our Phase Change script. In the script we will define a variable that will keep track of what phase we are in. Lets Begin
Code:
phase = 1
function GoPhase2()--Our phase function
phase = 2
end
function GoPhase3()--Our phase function
phase = 3
end
if(pUnit:GetHealthPct() <= 90 and phase == 1) then--start phase 2
GoPhase2()
elseif(pUnit:GetHealthPct() <= 50 and phase == 2) then--start phase 3
GoPhase3()
end
We updated the variable phase everytime we called our phase change command. Notice how we also checked to make sure that we were in the right phase before we changed into a new phase. If we hadn't we would have been stuck in Phase 2 the entire time. With this small script. I combined everything that I tought you in this tutorial so far. It truly shows how important knowing these programming and scripting fundamentals are. Now lets move on to scripting our first boss.
Its always good to map out what your gonna do - and by that I mean lets create a few functions that we know we are gonna use for our script instead of going blindly into the script.
Were going to use 4 functions to start with:
OnCombat
OnLeaveCombat
OnDeath
OnKilledTarget
Lets make it look like this
Code:
function OnCombat(Unit, event, player)
end
function OnLeaveCombat(Unit, event, player)
end
function OnDeath(Unit, event, player)
end
function OnKilledTarget(Unit, event)
end
Next we will have to Register the events otherwise they become 100% worthless.
There is a Global Function called "RegisterUnitEvent" that we will use. It requires what is called a parameter or an argument. Each Parameter has its own kind of value in this function it needs an 2 integers and a string. A string is a string of characters or as most commonly known - text.
Code:
function OnCombat(Unit, event, player)
end
function OnLeaveCombat(Unit, event, player)
end
function OnDeath(Unit, event, player)
end
function OnKilledTarget(Unit, event)
end
RegisterUnitEvent(ID, 1, "OnCombat")
RegisterUnitEvent(ID, 2, "OnLeaveCombat")
RegisterUnitEvent(ID, 3, "OnKilledTarget")
RegisterUnitEvent(ID, 4, "OnDeath")
Next we are gonna register some spell events - these have to be created as a function first and then inside of our oncombat function we can tell them when to activate. It will be very simple, since this isn't a hardcore boss, but a basic tutorial to get you prepared to start scripting, and actually understand what your doing.
Go pickout 2 spells from WoWHead or you can just use mine
Code:
function CannonFire(Unit,event)
Unit:CastSpell(40787)
end
function FellFire(Unit,event)
Unit:FullCastSpell(44844)
end
Now To register it inside of the OnCombat Function
Code:
function OnCombat(Unit, event, player)
Unit:RegisterEvent("CannonFire",7000,0)--Register function
Unit:RegisterEvent("FellFire",3000,0)
end
Add This Line in OnDeath and OnLeaveCOmbat
Code:
Unit:RemoveEvents()
Now the AI will quit running when it needs to. Now lets add some chat messages, to make our boss seem more real. Lets add this in to the OnCombat script.
Code:
Unit:SendChatMessage(14, 0, "Finally, A New Toy To Play With!")
And Make out OnKilledTarget Function look like this
Code:
function OnKilledTarget(Unit, event)
Unit:SendChatMessage(14, 0, "Awwwww, I Broke It....")
end
That is our basic boss script. I have given you the knowledge to script. Now you have to do the research to learn about the functions in Lua. I gave you the base to know what your doing. One of the biggest things in programming - there is alot of research involved if you wanna be good. Enjoy your stepping stone into the programming world.