Understanding Lua menu

User Tag List

Results 1 to 10 of 10
  1. #1
    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)

    Understanding Lua

    Understanding Lua


    I am making this thread because a lot of people ask me every day to fix their Lua scripts, what’s wrong, and such like that. Once you understand Lua and how it works, then you are done and dusted, and you can fix most errors that occur when you script, and do more things with variables. This guide consists of entirely how I learn Lua and script Lua. There may be more professional terms but this is what I use!
    This guide will cover the basics of Lua, to some variable work. Nothing to advance but it helps you understand how to do advance things.
    This guide actually explains it every step of the way instead of just telling you this does that.

    This guide consists of four sections:
    1 – Input, process and output.
    2 - Understanding Errors
    3 – Variables
    4 – Tips and Tricks

    Warning: You may not understand this guide if you are a first-timer.

    Oh and I'll let you know now, my graphics involving screenshots and anything really, is terrible.

    Section 1: Input, process and output

    Lua works in three sections, much like anything else in our modern day world. Have you ever heard of the term “Analyse, evidence, evaluate”, “Input, process, outpout” or many other abbreviations which all mean the same thing. It’s also Lua.
    Code:
    Input: function (inputted from a registered event)
    Process: Do something (tell the script what we want to do)
     Output: end (Now we end it all)
    When registering an event, there are many different syntaxes and types you can do, the most common being:
    “RegisterUnitEvent(id of npc, 1, “Name of function”)”
    Registering the unit event is for a creature. There are other types such as gameobject, player and quest events. The id of the npc is obvious in what you fill it in with, as with quest and gameobjects. The 1 is the value for registering a event on combat. Lets clear up a myth; You do not need to put as the function name at some point “_OnEnterCombat” or anything similar to that. The function name can actually be anything you want it to be, but every time it must be unique to any other function in that script or outside it. You could even have “LolcatsIEatBabies_rofl” as your function name. I will add a huge list of all the different register types at the Tips and Tricks section. When you want to register something to happen after a timer, you have the obvious on enter combat and such that starts the event, but then how do you do something after a timer? That’s simple. In the function of the registered event you add this: RegisterEvent(“Name”, 1000, 1).
    RegisterEvent is always the same, the name has to be unique again and the 1000 is the timer. 1 second = 1000, so if you put that it would happen every second. The 1 after that is the amount of times to do it. Do not put 999 or anything like that; simple put 0 if you want it to keep doing it forever. When you want it to stop no matter if you put it as 0 or 5 etc, you simple do :RemoveEvents(). If there are no events to remove, there’s no point putting this on leave and death is there?
    An obvious error I see people doing a lot is the problem with your functions.
    Function Football_Attacked(pUnit, Event)
    This means that you can only use pUnit and Event till you define a new target.
    Some people use Unit, unit etc instead. Note that Lua is case sensitive.
    If you define it as pUnit then you can’t say Unit:SendChatMessage etc!
    If you want to cast it on the player now, you need to define the player, you can’t just add “player” to the function and then say it. You need to do something similar to this:
    pUnit:FullCastSpellOnTarget(X, pUnit:GetRandomPlayer(0))
    X being spell id.
    Or
    player = pUnit:GetRandomPlayer(0)
    player:AddItem(X, Amount)

    X being item id.
    This is going into variable work now so this is the end of section 1.

    Section 2: Understanding Errors

    Errors, don’t we all panic when we say big red writing saying “ERROR”? Well there’s no need to, it’s often a silly mistake such as missing an end or having a capital letter in the wrong place and such. I will go through basic errors, what they’re telling us and how to understand them.

    Error A:

    This error here is telling us a lot. Firstly it could no load the script zzzzNuhtc.lua at line 21. It's expecting the error to be because of a end, basically this error means there are to many ends. You always have 1 end for the function, and then an end for each if statement and such. You do not need ends for process parts.

    Error B:


    Do not be mistaken, this error does not mean at line 167. It's telling you theres to many ends, by saying to many ends at a if statment I had. Knock one off and it should be fine!

    Error C:


    "Unit Expected - Got nil"


    I don't have a screenshot for this error.
    This generally happens when your npc is trying to cast a spell on somone, but it isn't getting anything. It will also tell you the line. This is how you fix it:


    Code:
    function spell(pUnit, Event)
    local player = pUnit:GetRandomPlayer(0) -- This could be something else
    if player == nil then
    else
    pUnit:FullCastSpellOnTarget(11, player)
    end
    end
    Looks complicated? It really isn't.
    This is some brief variable work again, I wont go into depth. You tell the script that the player (could be dog, plr, or something) is to get a random player (can change this). If the player is nil (which means nothing) then don't do anything, otherwise cast the spell on him; meaning if the player is there we will cast spell 11 on him.


    Any other errors you get and don't understand, reply and say what they are, and I will add it to this section!

    Section 3: Variables

    Variables is much easier than people make it out to be.
    Variables can be extreamly simple, or extreamly advance.

    A variable is basically when you tell the script or more that you something is something else, simple as that.
    local variables effect only that script.
    global variables effect the whole Lua engine.
    Without saying if a variable is local or global, it will only last in that function if that.

    Lets start by telling the script a variable.

    local Dog = 5082

    This means that whenever the script sees the word "Dog" it will think "5082". You could now put Dog instead of the npcid when registering a event for example now.


    local plr = pUnit:GetRandomPlayer(0)


    Here we have told the script that the variable is a player, and it will get that target.
    Warning: When getting something it has to be in a function, while if your simple telling the script it's something then you can put it outside the script, the same place as registering a unit on combat.


    local Ca
    t

    This means Cat is equal to nothing, it is nothing, we have set it up so we can define it later.
    Here is where we can go advanced. We can say half way through a boss fight:


    Cat = pUnit

    This means that, this one npc who is using the script at that time is now Cat, it's not that id, just that 1 npc in game.
    Somewhere else later on we can now say:

    Cat:SendChatMessage(12,0,"Hello world")

    because we have told the script what cat is, and so it can do stuff. You can play around with this yourself to see what you can do by defining people.

    local Time = 0

    We told the script that Time is now 0.
    We can do funny counting and such to the script now, for example every 1 second do
    Time = Time + 1
    because were saying that the variable, is it's self +1, so it's now going up 1 every second, then we can say
    if Time == 5 then
    else
    end

    So that we can controll when to do stuff like a register event.
    You can also do this as configurations.
    if Time == 1 then
    pUnit:SetFaction(1) -- Alliance
    else
    if Time == 2 then
    pUnit:SetFaction(2) -- Horde
    else
    end
    end

    And you can see what we can do if we keep going on, I'll leave variables here, and you can ask questions in a reply if you want.


    Section 4: Tips and Tricks

    Write it right:
    "Lua" (pronounced LOO-ah) means "Moon" in Portuguese. As such, it is neither an acronym nor an abbreviation, but a noun. More specifically, "Lua" is a name, the name of the Earth's moon and the name of the language. Like most names, it should be written in lower case with an initial capital, that is, "Lua". Please do not write it as "LUA", which is both ugly and confusing, because then it becomes an acronym with different meanings for different people. So, please, write "Lua" right!

    Lua not LUA. It’s a word not a shortened phrase such as WOMD – Weapons of mass destruction.

    -----------

    A huge list of events, it will say what the event is and what the id is, copied and pasted from my core.

    Code:
     enum QuestEvents
    {
        QUEST_EVENT_ON_ACCEPT = 1,
        QUEST_EVENT_ON_COMPLETE = 2,
        QUEST_EVENT_ON_CANCEL = 3,
        QUEST_EVENT_GAMEOBJECT_ACTIVATE = 4,
        QUEST_EVENT_ON_CREATURE_KILL  = 5,
        QUEST_EVENT_ON_EXPLORE_AREA = 6,
        QUEST_EVENT_ON_PLAYER_ITEMPICKUP = 7,
        QUEST_EVENT_COUNT,
    };
    
    enum CreatureEvents
    {
        CREATURE_EVENT_ON_ENTER_COMBAT        = 1,
        CREATURE_EVENT_ON_LEAVE_COMBAT        = 2,
        CREATURE_EVENT_ON_TARGET_DIED        = 3,
        CREATURE_EVENT_ON_DIED        = 4,
        CREATURE_EVENT_ON_TARGET_PARRIED        = 5,
        CREATURE_EVENT_ON_TARGET_DODGED        = 6,
        CREATURE_EVENT_ON_TARGET_BLOCKED        = 7,
        CREATURE_EVENT_ON_TARGET_CRIT_HIT        = 8,
        CREATURE_EVENT_ON_PARRY        = 9,
        CREATURE_EVENT_ON_DODGED        = 10,
        CREATURE_EVENT_ON_BLOCKED        = 11,
        CREATURE_EVENT_ON_CRIT_HIT        = 12,
        CREATURE_EVENT_ON_HIT        = 13,
        CREATURE_EVENT_ON_ASSIST_TARGET_DIED        = 14,
        CREATURE_EVENT_ON_FEAR        = 15,
        CREATURE_EVENT_ON_FLEE        = 16,
        CREATURE_EVENT_ON_CALL_FOR_HELP        = 17,
        CREATURE_EVENT_ON_LOAD        = 18,
        CREATURE_EVENT_ON_REACH_WP        = 19,
        CREATURE_EVENT_ON_LOOT_TAKEN        = 20,
        CREATURE_EVENT_ON_AIUPDATE        = 21,
        CREATURE_EVENT_ON_EMOTE        = 22,
        CREATURE_EVENT_COUNT,
    };
    
    enum GameObjectEvents
    {
        GAMEOBJECT_EVENT_ON_CREATE            = 1,
        GAMEOBJECT_EVENT_ON_SPAWN            = 2,
        GAMEOBJECT_EVENT_ON_LOOT_TAKEN        = 3,
        GAMEOBJECT_EVENT_ON_USE                = 4,
        GAMEOBJECT_EVENT_AIUPDATE            = 5,
        GAMEOBJECT_EVENT_ON_DESPAWN            = 6,
        GAMEOBJECT_EVENT_COUNT,
    };
    
    enum PlayerObjectEvents
    {
        PLAYEROBJECT_EVENT_DEALDAMAGE        = 1,
        PLAYEROBJECT_EVENT_COUNT,
    };
    
    enum GossipEvents
    {
        GOSSIP_EVENT_ON_TALK            = 1,
        GOSSIP_EVENT_ON_SELECT_OPTION    = 2,
        GOSSIP_EVENT_ON_END             = 3,
        GOSSIP_EVENT_COUNT,
    };
    
    enum RandomFlags
    {
        RANDOM_ANY           = 0,
        RANDOM_IN_SHORTRANGE = 1,
        RANDOM_IN_MIDRANGE   = 2,
        RANDOM_IN_LONGRANGE  = 3,
        RANDOM_WITH_MANA     = 4,
        RANDOM_WITH_RAGE     = 5,
        RANDOM_WITH_ENERGY   = 6,
        RANDOM_NOT_MAINTANK  = 7,
        RANDOM_COUNT,
    };
    These are the basics of Lua and how it works. If you have any questrions regarding Lua or what I have written, for example you don't understand it or have you done something correct/wrong, don't hesitate to reply then I will answer you

    Credits:
    - Me.
    - Lua website for quote.
    - GuaEngine for copy of events.

    Last edited by stoneharry; 06-24-2009 at 02:34 PM.

    Understanding Lua
  2. #2
    svedin's Avatar Contributor
    Reputation
    124
    Join Date
    Jun 2008
    Posts
    557
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very nice guide this will help alot of ppls

  3. #3
    Kaidos's Avatar Contributor
    Reputation
    148
    Join Date
    Jun 2008
    Posts
    324
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yay nice Stoneharry.

    Like the guide.

    - good explained, detailed and
    - answer the general problems is very nice for lua beginners

    +2+Rep of course!

  4. #4
    AngelSandy's Avatar Member
    Reputation
    19
    Join Date
    Jan 2009
    Posts
    330
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This sounds very nice, for beginners

    Can't +Rep yet I'm afraid.

  5. #5
    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)
    Nice guide. Not like the other 1 trillion lua guides out here. +Rep x2 if I can


  6. #6
    Linkn's Avatar Contributor
    Reputation
    90
    Join Date
    Mar 2009
    Posts
    296
    Thanks G/R
    0/0
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its very well written and explains a lot. Like Vindicated said, its not just the standard copy paste guide +Rep If i can

    EDIT: Gotta spread.
    Last edited by Linkn; 06-25-2009 at 01:31 AM.

  7. #7
    Link_S's Avatar Member
    Reputation
    125
    Join Date
    Dec 2008
    Posts
    293
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is required out here, I'm thinking about to make one of C++. As everyone thinks that C++ is the hardest thing you'd find in the emulation community, but it isn't.

    +Rep for this!
    Why do I need a signature?

  8. #8
    y2kss66's Avatar Member
    Reputation
    104
    Join Date
    Jan 2008
    Posts
    778
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    very spiffy. will help many including me!

    +Rep if I can!

  9. #9
    Flesh0209's Avatar Member
    Reputation
    3
    Join Date
    Jul 2009
    Posts
    62
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great job man im actuallly starting lua and i would ask you if you could help me with one of my script, well just look at it. actually.

    + Rep too this helped me alot already and will help others for sure. !!!!! =D

  10. #10
    Confucius's Avatar Panda of Worlds The Idiot

    CoreCoins Purchaser Authenticator enabled
    Reputation
    1389
    Join Date
    Oct 2007
    Posts
    2,590
    Thanks G/R
    267/283
    Trade Feedback
    7 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't know I annoyed you so much that it made you write a guide! lol jk

    Nice guide +Rep 2x if I can

Similar Threads

  1. Are you good at understanding .lua scripting? Please read!
    By Eratoc in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 02-26-2015, 02:56 AM
  2. Understanding The basics of Lua Locals!
    By Dibes in forum WoW EMU Guides & Tutorials
    Replies: 1
    Last Post: 07-12-2009, 12:08 AM
  3. How to understand Lua - The Basics
    By Nymphx in forum WoW EMU Guides & Tutorials
    Replies: 4
    Last Post: 06-02-2009, 03:15 PM
  4. LUA: Understanding Gossip Menus
    By SectorSeven in forum WoW EMU Guides & Tutorials
    Replies: 46
    Last Post: 08-16-2008, 09:27 AM
  5. Understand Opposing Faction Chat (Adanvced)
    By Matt in forum World of Warcraft Exploits
    Replies: 11
    Last Post: 08-09-2006, 02:47 AM
All times are GMT -5. The time now is 01:08 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