[Lua][HowTo] Nil Checks, Local, and "And" menu

User Tag List

Results 1 to 12 of 12
  1. #1
    BrantX's Avatar Contributor
    Reputation
    205
    Join Date
    Jul 2007
    Posts
    899
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Lua][HowTo] Nil Checks, Local, and "And"

    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<3.

    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
    Code:
    function Boss_CastSpell(Unit,Event)
        Unit:FullCastSpellOnTarget(5,Unit:GetMainTank())
    end
    A 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"

    Code:
     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.

    Code:
    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
    end
    What 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 = ...
    Local = ... are used in alot of my scripts that i have worked on, Pretty much what Local = ... does is that it pretty much renames or rephrases the word you want it to be. Pretty simple and it makes your script look VERY neat.
    I'll show you an example of a "Local" being used correctly in a script.

    Vindicated(AKA:SmartAss<3)'s Version :
    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.

    Code:
    local plr = Unit:GetClosestPlayer()
    local tank = Unit:GetMainTank()
    local plrMana = Unit:GetRandomPlayer(4)
    function Boss_DrainPlayer(Unit,Event)
        Unit:FullCastSpellOnTarget(24435,tank) -- Sexy!
     if (tank ~= nil) then -- Look how sexy!
        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

    Code:
    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.

    Code:
    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.
    Last edited by BrantX; 03-25-2009 at 06:30 PM.



    [Lua][HowTo] Nil Checks, Local, and &quot;And&quot;
  2. #2
    BrantX's Avatar Contributor
    Reputation
    205
    Join Date
    Jul 2007
    Posts
    899
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /head-desk
    BUMP



  3. #3
    RyeRye's Avatar Contributor
    Reputation
    240
    Join Date
    Aug 2008
    Posts
    996
    Thanks G/R
    0/1
    Trade Feedback
    3 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brant, never surprises me.

    +Rep (if I can)



  4. #4
    stoneharry's Avatar Moderator Harry

    Authenticator enabled
    Reputation
    1613
    Join Date
    Sep 2007
    Posts
    4,554
    Thanks G/R
    151/146
    Trade Feedback
    0 (0%)
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Maybe people will understand why most boss fights were ban in emulater server releases from this +Rep

  5. #5
    BrantX's Avatar Contributor
    Reputation
    205
    Join Date
    Jul 2007
    Posts
    899
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guy for the feedback.
    And Stoneharry yes i know, haha.



  6. #6
    Vindicated's Avatar Contributor
    Reputation
    226
    Join Date
    Aug 2008
    Posts
    1,067
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pretty good guide overall, but I think your description of "local" is a little shaky. Here is a little better one:
    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.
    +rape
    Last edited by Vindicated; 02-25-2009 at 12:21 AM.


  7. #7
    BrantX's Avatar Contributor
    Reputation
    205
    Join Date
    Jul 2007
    Posts
    899
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Vindicated View Post
    Pretty good guide overall, but I think your description of "local" is a little shaky. Here is a little better one:
    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.
    +rape
    Thanks, I guess you explained it better than me :P



  8. #8
    alj03's Avatar Contributor
    Reputation
    91
    Join Date
    Feb 2008
    Posts
    1,103
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome Mate, +Rep.
    Death to all but Metal.

  9. #9
    TheZaronz's Avatar Active Member
    Reputation
    97
    Join Date
    Dec 2007
    Posts
    567
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easy and useful guide! +Rep m8

    [ Edit: Gotta spread ]

  10. #10
    Mircast's Avatar Member
    Reputation
    14
    Join Date
    Sep 2008
    Posts
    139
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "There you are, Doesn't that look so "advanced" and nice? I think it does assface >.>"

    Ha, good guide. Will rep in 24 hours.

  11. #11
    Ellenor's Avatar Active Member
    Reputation
    18
    Join Date
    Jan 2008
    Posts
    281
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Vindicated View Post
    Pretty good guide overall, but I think your description of "local" is a little shaky. Here is a little better one:
    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.
    +rape

    Very nice explanation of the "local command" vindicated and with these 3 commands it will open new source to boss fight etc.

    +2 rep for u vindicated for the explanation.

    5/5 stars on this thread!

    Also +2 rep to u Caedec for the sharing/release of these lua commands and for their explanation!! i Could say this thread must be stinky.
    Last edited by Ellenor; 03-10-2009 at 10:22 PM.

  12. #12
    BrantX's Avatar Contributor
    Reputation
    205
    Join Date
    Jul 2007
    Posts
    899
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Updated "nil" so that it shows a working format.



Similar Threads

  1. paying for a good private lua macro for holy paladin and ret paladin pvp
    By Speedracer22 in forum WoW UI, Macros and Talent Specs
    Replies: 0
    Last Post: 03-25-2012, 02:00 PM
  2. Lua npc that creats guilds and arena teams
    By Summer in forum WoW EMU Questions & Requests
    Replies: 0
    Last Post: 03-04-2010, 03:08 PM
  3. [Lua Question] Npc not moving and target question
    By Kaidos in forum WoW EMU Questions & Requests
    Replies: 9
    Last Post: 06-03-2009, 03:07 PM
  4. LUA Scripted Boss with mobs and objects
    By denniskramer in forum WoW EMU General Releases
    Replies: 6
    Last Post: 12-16-2008, 07:20 AM
  5. [Lua] Make mobs with sound. And a trigger question.
    By sammiboy in forum WoW EMU Questions & Requests
    Replies: 5
    Last Post: 09-26-2008, 09:04 AM
All times are GMT -5. The time now is 11:48 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search