How to understand Lua - The Basics menu

Shout-Out

User Tag List

Results 1 to 5 of 5
  1. #1
    Nymphx's Avatar Active Member
    Reputation
    83
    Join Date
    Sep 2008
    Posts
    212
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to understand Lua - The Basics

    ::How To Understand Lua - The Basics::

    In this guide I'm going to explain in as much detail everything in one of my most popular Lua scripts for emulation (Leviathan.lua).

    This guide is mostly to help beginners understand the functions of an Lua script, but it caters for all. Read on and you may pick up a few tips that you may not have known previously.


    Please note: The pastebin links at the bottom of this post guarantees easier viewing of the script.

    The script:

    Code:
                    
    function Leviathan_OnEnterCombat(pUnit,Event) -- This referrs to RegisterUnitEvent(22884, 1, "Leviathan_OnEnterCombat")
    											-- At the bottom of the script. These RegisterUnitEvent functions are what
    											-- Are activated at the beginning of the script. Just copy what is at the bottom
    											-- Of this and edit the name/id (explained in detail lower down).
        pUnit:SendChatMessage(12, 0, "I may look like a usual beast that you hunt, but having two heads can have it's uses!")
    	-- Above is a chat message. 12 is the ID for a /say. 14 for a /yell. These are the only 2 you will really use
    	-- but more are available if you search the internets and MMowned. Its a very basic command and to customise it you will
    	-- only really need to edit the type ID and the actual message inside the speech marks "".
        pUnit:RegisterEvent("Leviathan_Morph1", 1000, 0)
    	-- This registers an event, and refreshes the register every 1000 milliseconds.
        pUnit:CastSpell(15473)
    	-- Instantly casts the spell of ID '15473' on its target
    	-- Its also used if you want to cast an AOE such as Consencration
        pUnit:RegisterEvent("Leviathan_Talk", 15000, 0)
        pUnit:RegisterEvent("Leviathan_Empowering", 20000, 0)
        pUnit:RegisterEvent("Leviathan_SpellCrushing", 14000, 0)
        pUnit:RegisterEvent("Leviathan_SpellBolt", 15000, 0)
        pUnit:RegisterEvent("Leviathan_SpellPain", 17000, 0)
    	-- All of the above are registering seperate events (1 talk, 4 spells)
    	-- The timers are very crucial as they help balance the fight. Set them accordingly
    end
    --Phase/Morph Specific Spells--
    --ShadowPhase, 100 to 75 percent--
    function Leviathan_Empowering(pUnit,Event)
    --   pUnit:RegisterEvent("Leviathan_Empowering", 20000, 0)
    -- This function is called and executed every 20000MS whilst the OnEnterCombat function
    -- Is still active, or if pUnit(RemoveEvents); hasn't been executed.
        pUnit:CastSpell(33783)
    end
    
    function Leviathan_SpellCrushing(pUnit,Event)
        pUnit:CastSpellOnTarget(40243,pUnit:GetRandomPlayer(0))
    	-- Cast spell on target is a more precise way of spell management
    	-- There are quite a few different selections to choose from but the
    	-- main two are GetRandomPlayer and GetMainTank.
    end
    
    function Leviathan_SpellBolt(pUnit,Event)
        pUnit:CastSpellOnTarget(27209,pUnit:GetRandomPlayer(0))
    end
    
    function Leviathan_SpellPain(pUnit,Event)
            pUnit:CastSpellOnTarget(25368,pUnit:GetRandomPlayer(0))
            pUnit:CastSpellOnTarget(25368,pUnit:GetMainTank())
    		-- Example of the "GetMainTank" selection.
    end
    --FirePhase, 75 to 50 percent--
    function Leviathan_SpellFirecone(pUnit,Event)
            pUnit:CastSpellOnTarget(19630,pUnit:GetClosestPlayer())
    		-- Another example of a target selection for a spell.
    		-- this one can be useful for AOE's/knockbacks.
    end
    
    function Leviathan_SpellFireball(pUnit,Event)
            pUnit:CastSpellOnTarget(25306,pUnit:GetMainTank())
    end
    
    function Leviathan_SpellFireRain(pUnit,Event)
            pUnit:CastSpell(27212)
    end
    
    function Leviathan_SpellSteal(pUnit,Event)
            pUnit:CastSpellOnTarget(30449,pUnit:GetRandomPlayer(0))
    end
    --IceFrostPhase, 50 to 25 percent--
    function Leviathan_SpellFrostbolt(pUnit,Event)
            pUnit:CastSpellOnTarget(38697,pUnit:GetRandomPlayer(0))
    end
    
    function Leviathan_SpellBreath(pUnit,Event)
            pUnit:CastSpellOnTarget(44799,pUnit:GetClosestPlayer())
    end
    
    function Leviathan_SpellClaw(pUnit,Event)
            pUnit:CastSpellOnTarget(3130,pUnit:GetMainTank())
    end
    
    function Leviathan_SpellCone(pUnit,Event)
            pUnit:CastSpellOnTarget(10159,pUnit:GetClosestPlayer())
    end
    
    function Leviathan_SpellPlague(pUnit,Event)
            pUnit:CastSpellOnTarget(40351,pUnit:GetRandomPlayer(0))
    end
    --WarriorPhase, 25 to 1 percent--
    function Leviathan_SpellCleave(pUnit,Event)
            pUnit:CastSpellOnTarget(25231,pUnit:GetMainTank())
    end
    
    function Leviathan_SpellBlackCleave(pUnit,Event)
            pUnit:CastSpellOnTarget(33480,pUnit:GetClosestPlayer())
    end
    
    function Leviathan_SpellClap(pUnit,Event)
            pUnit:CastSpellOnTarget(6343,pUnit:GetMainTank())
    end
    
    function Leviathan_SpellRend(pUnit,Event)
            pUnit:CastSpellOnTarget(25208,pUnit:GetRandomPlayer(0))
    end
    
    function Leviathan_SpellAgony(pUnit,Event)
            pUnit:CastSpellOnTarget(36836,pUnit:GetRandomPlayer(0))
    end
    
    function Leviathan_SpellAgony(pUnit,Event)
            pUnit:CastSpellOnTarget(36836,pUnit:GetRandomPlayer(0))
    end
    --Morphs and Phases--
    --Morph one, Al'Ah display ID, FirePhase enabled, 75 to 50 percent--
    function Leviathan_Morph1(pUnit,Event)
     if pUnit:GetHealthPct() <= 75 then
     -- This function wil be called at 75% health (Less than or Equal to signs used)
     -- This function is called every 1000 MS whilst the first phase is active but
     -- will not be executed until 75% health.
        print "Group through stage 1"
    	-- Prints a message to the administrator console
        pUnit:RemoveEvents()
    	-- Removes ALL events
        pUnit:SetModel(18945)
    	-- Changes the model of the NPC
        pUnit:SendChatMessage(12, 0, "You think you are a match for me? Pfft, what about if I do.... THIS?")
        pUnit:SetScale(0.3)
    	-- Changes the scale of the NPC, exactly like .mod scale #
        pUnit:CastSpell(25431)
        pUnit:RegisterEvent("Leviathan_Talk", 15000, 0)
        pUnit:RegisterEvent("Leviathan_SpellFirecone", 18000, 0)
        pUnit:RegisterEvent("Leviathan_SpellFireball", 15000, 0)
        pUnit:RegisterEvent("Leviathan_SpellFireRain", 8000, 0)
        pUnit:RegisterEvent("Leviathan_SpellSteal", 20000, 0)
        pUnit:RegisterEvent("Leviathan_Morph2", 1000, 0)  
    	-- Registering more events, plus the 50% phase.
    end
    end
    --Morph two, Ahune The Frost Lord display ID, IceFrostPhase enabled, 50 to 25 percent--
    function Leviathan_Morph2(pUnit,Event)
     if pUnit:GetHealthPct() <= 50 then
        print "Group through stage 2"
        pUnit:RemoveEvents()
        pUnit:SetModel(23344)
        pUnit:SendChatMessage(12, 0, "Hah, you think that you may triumph over me, but you are only halfway through this igneous shell of steel!")
        pUnit:SetScale(0.3)
        pUnit:CastSpell(7301)
        pUnit:RegisterEvent("Leviathan_Talk", 15000, 0)
        pUnit:RegisterEvent("Leviathan_SpellFrostbolt", 11000, 0)
        pUnit:RegisterEvent("Leviathan_SpellBreath", 20000, 0)
        pUnit:RegisterEvent("Leviathan_SpellClaw", 8000, 0)
        pUnit:RegisterEvent("Leviathan_SpellCone", 15000, 0)
        pUnit:RegisterEvent("Leviathan_SpellPlague", 17000, 0)
        pUnit:RegisterEvent("Leviathan_Morph3", 1000, 0) 
    	-- This is pretty much the same structure as 75% phase, but a different
    	-- model ID and set of spells/chat message.
    end
    end
    --Morph three, Doom Lord Kazzak display ID, WarriorPhase enabled, 25 to 1 percent--
    function Leviathan_Morph3(pUnit,Event)
     if pUnit:GetHealthPct() <= 25 then
        print "Group through stage 3, final stage."
        pUnit:RemoveEvents()
        pUnit:SetModel(17887)
        pUnit:SendChatMessage(12, 0, "I might be growing weaker with every fist thrown against my tattered skin, but you have now unleased the might of my vengence!")
        pUnit:SetScale(0.15)
        pUnit:CastSpell(43716)
        pUnit:RegisterEvent("Leviathan_Talk", 15000, 0)
        pUnit:RegisterEvent("Leviathan_SpellCleave", 11000, 0)
        pUnit:RegisterEvent("Leviathan_SpellBlackCleave", 13000, 0)
        pUnit:RegisterEvent("Leviathan_SpellClap", 12000, 0)
        pUnit:RegisterEvent("Leviathan_SpellRend", 19000, 0)
        pUnit:RegisterEvent("Leviathan_SpellAgony", 17000, 0)
        pUnit:RegisterEvent("Leviathan_Morph4", 1000, 0)  
    end
    end
    --Last 1 percent--
    function Leviathan_Morph4(pUnit,Event)
     if pUnit:GetHealthPct() <= 1 then
        print "Group through stage 4, Boss at 1%."
        pUnit:RemoveEvents()
        pUnit:SendChatMessage(12, 0, "The pain, the agony.. Please spare of me! I beg you!")
        pUnit:SetScale(0.3)
        pUnit:CastSpell(1020)
    	-- The boss casts a bubble on himself (Divine Shield).
    	-- Notice that CastSpell is used here to auto target himself.
        pUnit:SendChatMessage(12, 0, "Oh! Spare me such pain?") 
    end
    end
    
    function Leviathan_Death(pUnit)
    -- Function called On the death of the NPC.
    -- This is registered at the bottom of the script and explained
    -- more there.
        print "Group killed Leviathan"
        pUnit:SendChatMessage(12, 0, "Master, please forgive me...")
        pUnit:RemoveEvents()
    end
    
    function Leviathan_Talk(pUnit, Event)
    Choice=math.random(1, 3)
    -- math.random is your friend. Use it to make your fight unique
    -- Can be used to cast spells, walk places, patrol, talk, etc.
    if Choice==1 then
    -- Executed if the random choice = 1
    pUnit:SendChatMessage(14, 0, "You are quite strong my friend! Feel the power of the elements!")
    pUnit:CastSpell(31895)
    end 
    if Choice==2 then
    -- Executed if the random choice = 2
    pUnit:SendChatMessage(14, 0, "If you run, I might just spare your lives!")
    pUnit:CastSpell(31895)
    end
    if Choice==3 then
    -- Executed if the random choice = 3
    pUnit:SendChatMessage(14, 0, "The elements that my Master, Supremus, has given me.. They will blow you away!")
    pUnit:CastSpell(31895)
    end 
    end
    -- Two ends because of the Choice
    
    function Leviathan_OnLeaveCombat(pUnit, event)
    -- Function called upon leaving combat, registered at the beginning
    pUnit:RemoveEvents()
    end
    
    function Leviathan_OnKilledTarget(pUnit)
    -- Function called when the NPC kills a player.
    pUnit:SendChatMessage(12, 0, "Feel my Wrath!")
    pUnit:PlaySoundToSet(9250)
    end
    
    -- The start registers as I call them. These are 'special'
    -- The ID's (1,2,3,4) are to determine what the event is
    -- (1 = oncombat, 2=onleavecombat, 3=onkilledtarget, 4=ondeath)
    -- More ID's can be found with a simple search of a lua functions list
    -- or something similar.
    -- These can also be used to register GO's, GossipEvents, etc.)
    RegisterUnitEvent(22884, 1, "Leviathan_OnEnterCombat")
    RegisterUnitEvent(22884, 2, "Leviathan_OnLeaveCombat")
    RegisterUnitEvent(22884, 3, "Leviathan_OnKilledTarget")
    RegisterUnitEvent(22884, 4, "Leviathan_Death")
    Pastebin link to the script as Raw: Lua pastebin - collaborative debugging tool
    Pastebin link to the script with comments: http://pastebin.com/f73a44f1c

    If there are any suggestions for this guide, please tell me as this is my first attempt at a guide

    I hope this helps someone and enjoy Lua!

    How to understand Lua - The Basics
  2. #2
    Jchunx's Avatar Legendary


    Reputation
    755
    Join Date
    Aug 2006
    Posts
    1,091
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where's all the feedback, guys?
    Come on, let's at least help someone who's trying to contribute out.

    Although I don't do anything related to WoW Emulation, I can tell you spent a lot
    of time working on this.
    +7 reputation added, from me.

  3. #3
    Nymphx's Avatar Active Member
    Reputation
    83
    Join Date
    Sep 2008
    Posts
    212
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /wub Chunx. Yeah Stoneharry, you're right but its all the same Bleh!

  4. #4
    Claiver's Avatar Member
    Reputation
    138
    Join Date
    Mar 2009
    Posts
    217
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good contribution, will you also cover the basics of the Lua for Items etc.? If yes, you might be interested in releasing a bigger all-in-one guide sometimes with me, or not xD.

    Claiver +Rep'd

  5. #5
    Bugazi's Avatar Member
    Reputation
    2
    Join Date
    Dec 2007
    Posts
    41
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. +rep.

Similar Threads

  1. Understanding The basics of Lua Locals!
    By Dibes in forum WoW EMU Guides & Tutorials
    Replies: 1
    Last Post: 07-12-2009, 12:08 AM
  2. [MAYA TuT] Guide 1: The Basics - How to Model
    By Strupantwn in forum WoW ME Tools & Guides
    Replies: 14
    Last Post: 08-15-2008, 02:04 AM
  3. [MAYA TuT] Guide 2: The Basics - How to Shape
    By Strupantwn in forum WoW ME Tools & Guides
    Replies: 9
    Last Post: 08-11-2008, 08:59 AM
  4. SQL 101: Understanding the Basics
    By SectorSeven in forum Programming
    Replies: 2
    Last Post: 05-21-2008, 12:36 PM
  5. How to make a movie (the basics)
    By Nemorosa in forum Community Chat
    Replies: 8
    Last Post: 01-25-2008, 05:48 AM
All times are GMT -5. The time now is 08:07 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search