Results 1 to 2 of 2

Thread: LUA, How do I make my own Global Variable Space the correct way

  1. #1
    Senior Member skozzy's Avatar
    Join Date
    Jan 2008
    Location
    Brisbane
    Age
    55
    Posts
    525
    Thanks
    31
    Thanked 19 Times in 16 Posts
    Rep Power
    217
    Reputation
    114

    Default LUA, How do I make my own Global Variable Space the correct way

    I am fairly new and self trained with LUA addons for games. So my learning is limited to mistakes and fixing them and trawling over others people code. Fun hey.

    Anyhow, I want to know the correct way to put my variables in the global space without fear of changing some global variable from the game client.

    If I do varA = 10 for my addon and the client uses varA for its own functions then I know I am doing something wrong.
    Until now I just be creative in variable names like BLOBvarA = 10 and hope that isnt used by the game client.

    I've read through a lot of stuff from WOW lua to many of the LUA Tutorial sites and for my level of understanding it goes from jibberish to JIBBERISH real fast.

    I was of the understanding if I want my own global space I do something like:
    MyAddonGlobalSpace = {}
    then if I want to use varA I do MyAddonGlobalSpace.varA = 10
    But for some things I have used this with I get nothing back. At times i can do DEFAULT_CHAT_FRAME:AddMessage(MyAddonGlobalSpace.v arA) and it prints 10, other time prints nothing at all.
    This is even more often a problem when I use true and false.
    But later I see examples where people put dots inside the brackets like {...} but no explanation what the dots do.

    So basically this one addon I am working on has a timer and about 10 or so variables which I want to keep in memory while the game client is running, so I am using global variables for those, and I have a bunch of temp variables which I use with local.

    I have seen =_G used in some examples but cant see the correct way of using it either.

    So can someone dumb it down for me how to correctly have my own space in the global environment for variables.



Look Here ->
  • #2
    Senior Member

    Join Date
    Apr 2012
    Location
    14 Wombat Cres, Goanna Heights NSW
    Posts
    1,409
    Thanks
    734
    Thanked 1,154 Times in 578 Posts
    Rep Power
    605
    Reputation
    20643

    Default

    I've not heard of LUA before but I just looked over "" I found on google and it doesn't look so different to other languages?

    A global variable must be declared outside of any function, if a variable is declared inside a function it is "local" to that function and cannot be used outside of that function.

    It looks like a duplicate global variable in LUA will simply overwrite the previously declared variable, so if the game client declares the global variable:
    Code:
    varA = "GAME OVER"
    and you later declare:
    Code:
    varA = 10
    then you will no doubt find the end of the game displaying "10", so as you already stated, best to use only unique variables. Most IDEs that you write code within will warn you of a conflict before they compile the program but LUA doesn't seem to declare variables in the same way other languages do?

    In other languages you have to declare the type of variable, like whether it's an int, double, string, boolean, etc., but it appears all variables in LUA are tables, whether populated with integers, strings, booleans or whatever?

    Anyway, to declare your global variable, you would simply have outside of any function:
    Code:
    MyAddonGlobalSpace = {}
    then later, within a function you can populate it with something like:
    Code:
    function read_controller(button)
         if button == A then
              MyAddonGlobalSpace = "up"
         end
         else if button == B then
              MyAddonGlobalSpace = "down"
         end
    end
    Then if you later called:
    Code:
    DEFAULT_CHAT_FRAME:AddMessage(MyAddonGlobalSpace)
    The output would depend on which button was pressed within the function.

    Or you can simply initialise the variable when you first declare it:
    Code:
    MyAddonGlobalSpace = {"up","down","left","right"}
    Note that in any other language, using "else if" would mean you must initialise the variable when you declare it (since it's possible that neither condition exists), whereas when using just "else" you wouldn't.

    Andrew

  • Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •