Lua Programming menu

User Tag List

Results 1 to 5 of 5
  1. #1
    mager1794's Avatar Member
    Reputation
    356
    Join Date
    Feb 2008
    Posts
    703
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lua Programming

    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

    Code:
        GoPhase2()
    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.
    Last edited by mager1794; 08-12-2010 at 01:33 PM.
    Lunar Gaming - Reaching For The Stars

    Lua Programming
  2. #2
    Trle94's Avatar Contributor
    Reputation
    167
    Join Date
    May 2009
    Posts
    329
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GOood job I like it


  3. #3
    Dynashock's Avatar Contributor

    Reputation
    176
    Join Date
    Nov 2007
    Posts
    203
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's some good bits in there and I like the fact you're taking a different approach than the other threads in this section, however there are a few flaws I feel inclined to point out.

    1. An if statement in Lua consists of the word 'if', the condition, followed by the word 'then', which you seem to have consistenly forgotten.
    2. 'else if' is not the same as 'elseif'. 'Else if' is another if statement within the else statement and thus requires another end.
    3. For someone who intends to teach about programming you seem to abide by inconsistent poor indentation, which improves readability a lot and helps with finding out the scopes of functions, statements and variables.
    4. To me it sounds like you're only telling one part of the conditional statement. It feels like you don't explain in detail what it does or how it works. If the condition is true it continues with the code block beneath the statement, otherwise it skips that code block and moves on to the else of else if statment. From your paragraph (to me) it seems you only explain how one would use the exists() function, if it was to exist. Which strongly conflicts with the main point of the thread; teaching them to program instead of showing them what to do.
    Furthermore, we also have 'and', 'or' (I see those two often wrongly used), 'while' and 'for'. Although arguably it could be a bit too much for a first post on this subject, thus I would love to see more of these kind of threads in the future.

    Conclusion: you have some good points, and I'm a big fan of your approach rather than taking someone by hand and only telling him how to do it, instead of telling him how it works. I'm a big fan of the cliché saying; give a man a fish and he can eat for a day, or teach him fishing so he can eat for the rest of his life, therefore I salute and will rep you for this first good attempt. However, there are some flaws you have to work out, for you cannot (or preferably should not, it's amazing to see how far someone can get) continue to grow healthily and become acquainted with scripting, especially if Lua is your first programming language, without a solid base.

    PS. Lua Demo is a great site for small things like this, testing if certain logics of your script work and so on.
    Ignorance is bliss.

  4. #4
    mager1794's Avatar Member
    Reputation
    356
    Join Date
    Feb 2008
    Posts
    703
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your comments Dyna. I completely forgot about using 'then', my bad. I Will read thouroughly into all points and improve the tutorial. Some very excellent points, i expected a few problems mostly cause i don't really do Lua - i was gonna do this in C++ but i wanted something that everyone would understand.
    Lunar Gaming - Reaching For The Stars

  5. #5
    bsod-staff14's Avatar Member
    Reputation
    35
    Join Date
    Nov 2008
    Posts
    443
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh neat bra. I'd use this except I already know LUA. But Its a very detailed and nice guide +Rep x2
    Immortal GamerZ Under Development!

Similar Threads

  1. Replies: 8
    Last Post: 02-23-2016, 01:24 PM
  2. [Question] "Beginning" Lua programming
    By Krillere in forum WoW Memory Editing
    Replies: 3
    Last Post: 02-20-2010, 05:47 PM
  3. Lua Programming
    By SectorSeven in forum Suggestions
    Replies: 14
    Last Post: 06-23-2008, 03:18 AM
All times are GMT -5. The time now is 09:39 AM. 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