PDA

View Full Version : [HowTo] Nil Checks, Local, and "And"



BrantX
29-07-10, 02:20 AM
Hello all and welcome to my Lua Scripting Guide for the following commands:
Nil Check
Local
And
I hope you all enjoy the guide i have created, And please say thanks.

Legend:
Pink = Important Line
Green = Nil Check
Orange = Local = ...
Red = And

Let us begin.

I. Nil Checks
Nil/nil is another name for the word "NULL" NULL is an English word meaning 'nothing' or 'without value'. In Lua they have renamed NULL to Nil. Using Nil's or using Nil Checks can probably be the best thing you have ever done for your script.

Say you've made a boss script and it went something like this

function Boss_CastSpell(Unit,Event)
Unit:FullCastSpellOnTarget(5,Unit:GetMainTank())
endA group on your server is fighting it, all of a sudden one of the members of the group DC's and he was the tank. Your server would be getting spammed and spammed with "Nil Error", thus creating a "Crash".
You would use "nil" to check for the error. Nil is another name for "NULL"



function Boss_CastSpell(Unit,Event)
if (Unit:GetMainTank() ~= nil) then
return
else
Unit:FullCastSpellOnTarget(5,Unit:GetMainTank())
end
end


You would check for a nil(Meaning that if the Main Tank was Null it would "Return" Thus forcing no NULL and would just not cast the spell. BUT you could make the boss cast a spell on the player if tank isn't there, by doing this.


function Boss_CastSpell(Unit,Event)
Unit:FullCastSpellOnTarget(5,Unit:GetMainTank())
if (Unit:GetMainTank() == nil)then
Unit:FullCastSpellOnTarget(5,Unit:GetClosestPlayer ())
if (Unit:GetClosestPlayer() == nil) then
return
end
end
endWhat the above script would do is..
Cast spell "5" on Main Tank
if Main Tank ~= nil(NULL) then
Cast spell "5" on Closest Player
if ClosestPlayer ~= nil(NULL) then
return
thus casting NO spell if ClosestPlayer and Tank are NOT there or are either dead.

Nil Checks are used in every Lua Script i have ever created. Please start using them. *cracks the whip*


II. Local = ...
Unlike global variables, local variables have their scope limited to the block where they are declared. A block is the body of a control structure, the body of a function, or a chunk (the file or string with the code where the variable is declared). So in English, this means that when you declare something local, it is kept private to that function of or block of code as said above.



local plr = Unit:GetClosestPlayer()
local tank = Unit:GetMainTank()
local plrMana = Unit:GetRandomPlayer(4)
function Boss_DrainPlayer(Unit,Event)
Unit:FullCastSpellOnTarget(24435,tank) -- ***y!
if (tank ~= nil) then -- Look how ***y!
Unit:FullCastSpellOnTarget(24435,plr)
Unit:FullCastSpellOnTarget(46453,plrMana)
if (plr ~= nil and plrMana ~= nil) then
return
end

There you are, Doesn't that look so "advanced" and nice? I think it does assface >.>
Local just makes the work rephrased...
local NameOfTank = Unit:GetMainTank()
local Frostbolt = 11
local Spell = SpellID
local EYEBEAM = SpellIDofBeam

Another good usage for "Local" is incase you want to change the spell's up a bit, or a saying.

local saying = "I FREAKING LOVE TO EAT CHEESE!"
function CheeseMan_OnSpawn(Unit,Event)
Unit:SendChatMessage(14,0,saying)
end

You can do alot of stuff with local.

III. And
Okay, Say you wanted to... make a NPC spawn when 2 NPC's died...or EVEN 3 OR 4 OR 5!
anyway, you would use "and" insted of calling 40,000 functions(sarcastic ;D) Here is an example


local X,Y,Z = Unit:GetX(),Unit:GetY(),Unit:GetZ()
local npc1 = Unit:GetCreatureNearestCoords(X,Y,Z,666)
local npc2 = Unit:GetCreatureNearestCoords(X,Y,Z,777)
local npc3 = Unit:GetCreatureNearestCoords(X,Y,Z,888)
local npc4 = Unit:GetCreatureNearestCoords(X,Y,Z,999)
local plr = Unit:GetClosestPlayer()
function Boss_Check(Unit,Event)
if (plr ~= nil) then
return
else
if (npc1:IsDead() == 1 and npc2:IsDead() == 1 and npc3:IsDead() == 1 and npc4:IsDead() == 1) then
Unit:SpawnCreature(ID,X,Y,Z,35,0)
Unit:SpawnCreature(ID,X,Y,Z,35,0)
Unit:SpawnCreature(ID,X,Y,Z,35,0)
Unit:SpawnCreature(ID,X,Y,Z,35,0)
end
end
end

"and" is simple, easy, and very good to use insted of calling more functions than you already need.
It will also help you script boss fights.

With all of these 3 commands in Lua that i've just taught you about will help you provide excellent scripts.
Here is a script that i whipped up in about 5 min. and i used all of the 3 commands.



local tank = Unit:GetMainTank()
local plr = Unit:GetClosestPlayer()
local plrMana = Unit:GetRandomPlayer(4)
function Boss_OnCombat(Unit,Event)
if (plr ~= nil) then
Unit:SendChatMessage(12, 0, "Where'd ya go!")
else
Unit:FullCastSpellOnTarget(11,tank)
if (tank ~= nil) then
Unit:SendChatMessage(14, 0, "I kill PEOPLE WITH MANA!!")
Unit:FullCastSpellOnTarget(11,plr)
Unit:FullCastSpellOnTarget(5,plrMana)
if (plr ~= nil and plrMana ~= nil) then
return
end
end
end
end

Hope you enjoyed, have fun scripting!
Now that you know the "and","local" and "nil" functions/commands in Lua why not use them more often? Everybody'll be glad you did.

Also, Please give me feedback, I'd enjoy/love all of it.