+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 28
Like Tree2Likes

  Click here to go to the first Rift Team post in this thread.   Thread: A question for the addon authors in general...

  1. #1
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default A question for the addon authors in general...

    I am just wondering why so many authors are not using local variables, local functions, and especially localizing their addon? I'm not talking about translations. I'm talking about
    Code:
    MyAddon = {} -- global, and bad because it could cause conflicts
    local MyAddon = {} -- local, and problem solved
    local MyAddon, privateTable = ... -- best solution
    I see an alarming amount of the first example in code files.

    For libarary files, this isn't a big deal, but for addons themselves, not making a local reference is a bad, bad idea that is asking for all kinds of trouble. What if two addons have the same global name reference? It can, and will happen, as more and more addons are written. There might be some exceptions that are required, such as addons with modules, like KBM and Mark-It, but as a rule, it should not be done.

    Should an author need to pass data between files within the same addon, use the third example's privateTable. Just like any other variables, you can name them whatever you wish; I am just being generic.

    In my defence, because someone will point out that I am not actively writing addons: no, I am not, but that doesn't mean I am not allowed to cringe, nor am I disallowed to look at others' code. I am just asking, and I am not trying to start a flame war.

  2. #2
    Rift Disciple
    Join Date
    Mar 2011
    Posts
    130

    Default

    I will speak for myself here:

    I did it either out of "lazyness", out of simply not thinking about it and at first lack of knowledge about the scope of variables in lua.

    It brings me a couple of questions though.

    can my function be called by other add-ons if I don't use the "local" flag before the function name ?

    local function foo()
    print("foo")
    end

    function foo2()
    print("foo2")
    end

  3. #3
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default

    Quote Originally Posted by Frostshizzle View Post
    can my function be called by other add-ons if I don't use the "local" flag before the function name ?
    Only if your function is global. And if you need that functionality, call the function
    Code:
    MyAddon:DoSomething()
    where MyAddon is global. Best examples of this are in the libraries being created; their function names are something like
    Code:
    Library.LibSomething:DoSomething()
    You should never, ever, with zero exceptions, have simply
    Code:
    function DoSomething(foo)
        print(foo)
    end
    To use Battlestar Galactica, if you did that, you would frakk up huge! That MUST be a local function.

    Zorba probably can add to this, but try this chunk of code
    Code:
    local MyAddon, privateTable = ...
    local otherAdddonTable = Inspect.Addon.Detail("OtherAddonIdentifier")
    privateTable.otherAddonPrivateTable = otherAddonTable.privateTable -- you would have to look this up manually, I think
    local pOT = privateTable.otherAddonPrivateTable -- just don't want to type that again!
    
    local function DoSomething(pOT)
        print(pOT)
    end
    With your own addons, just the privateTable to share data, and use the first line in the top of your files. The catch is if the other author does not use the privateTable and then you are screwed. You can access another addon's saved variables by referencing whatever they are called in their toc inside your toc.

  4. #4
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default

    Simplified: since you want to be able to print "foo" in any file, you don't even need to use MyAddon(foo) as a global. Put "foo" into the private table.
    Code:
    -- file A
    local MyAddon, privateTable = ...
    privateTable.foo = "dog"
    
    -- file B
    local _, privateTable = ... -- unless you need MyAddon as a var, in which case put it in
    local foo = privateTable.foo
    local function(foo)
        print(foo)
    end
    ----> dog
    
    OR
    local function(privateTable.foo)
        print(privateTable.foo)
    end
    ----> dog
    Last edited by Lorandii; 02-10-2012 at 06:19 PM. Reason: optional solution

  5. #5
    Rift Disciple
    Join Date
    Mar 2011
    Posts
    130

    Default

    Trying to understand. What's that privatetable for in the first place ?

  6. #6
    Rift Disciple
    Join Date
    Mar 2011
    Posts
    130

    Default

    I saw that I had a couple "global" functions in my add-on

    I tried adding "local" before the function... got a truckload of errors now. => calling global function_name (a nil value)
    Last edited by Frostshizzle; 02-10-2012 at 07:00 PM.

  7. #7
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default

    Quote Originally Posted by Frostshizzle View Post
    Trying to understand. What's that privatetable for in the first place ?
    Passing data between files in the same addon, usually localization, but it can be any data.

  8. #8
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default

    Quote Originally Posted by Frostshizzle View Post
    I saw that I had a couple "global" functions in my add-on

    I tried adding "local" before the function... got a truckload of errors now. => calling global function_name (a nil value)
    Without knowing which addon you are mentioning, I can't look and see, but I'm guessing you have something like fileA.lua, fileB.lua, have defined a function in fileA.lua and call it in fileB.lua. When you made it local in fileA, it is local to that file, and fileB.lua can no longer see it. To fix that, use the local MyAddon, privateTable = ... at the top of every file, then name your functions in fileA.lua
    Code:
    function MyAddon:DoSomething()
    in which case if you call just like that in fileB, it will work.

  9. #9
    Rift Disciple
    Join Date
    Mar 2011
    Posts
    130

    Default

    Quote Originally Posted by Lorandii View Post
    Without knowing which addon you are mentioning, I can't look and see, but I'm guessing you have something like fileA.lua, fileB.lua, have defined a function in fileA.lua and call it in fileB.lua. When you made it local in fileA, it is local to that file, and fileB.lua can no longer see it. To fix that, use the local MyAddon, privateTable = ... at the top of every file, then name your functions in fileA.lua
    Code:
    function MyAddon:DoSomething()
    in which case if you call just like that in fileB, it will work.
    Everything is in the same file at the moment. I don't use any libraries.

    Reason I asked if it was possible to call my functions from another add-on is I don't want to risk anyone "calling" my functions from another add-on, as this could wreak havoc in my saved variables.

    My add-on is not public "yet"

    Edit: I think I nailed most of the bugs caused by local functions.

    I'll have to deal with global variables now.
    Last edited by Frostshizzle; 02-10-2012 at 08:02 PM.

  10. #10
    Champion
    Join Date
    Jun 2011
    Posts
    561

    Default

    Thanks a lot for pointing this out. I learned LUA by trial & error and am missing some concepts of the language alltogether. My problem was in fact that calling a function from one file to the other so I ditched the local / global concept cause I couldn't figure out who to do it. Will rewrite all of my addons.

    You're always welcome to point out other areas of improvements in my addons

    Cheers
    N.

  11. #11
    Rift Disciple
    Join Date
    Mar 2011
    Posts
    130

    Default

    Thanks indeed.

  12. #12
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default

    Two things about me are "broken", one of which is my sarcasm detector. I won't tell you the other LOL. Hopefully you guys aren't being sarcastic! Naifu, you probably won't have to rewrite your entire addons, just do some tweaking.

    I was only pointing this out, and asking about it because you could have a situation where one or more of the following happen:
    • a global var that should be local with different values gets changed in multiple addons, causing breakage
    • a global function that should be local conflicts with another addon or worse, the base UI
    • it makes squashing bugs harder than it needs to be
    Oh, one other tip: without the local MyAddon, privateTable = ... at the beginning of each file, then calling
    Code:
    MyAddon:SomeFunction()
    is still global. Much less likely to cause conflicts, but still possible.

    As a side note, while I am not a stickler, I know people who are, but Lua is Portuguese for "moon" and is not an acronym "LUA". Any time someone says that on the wowace forums, they get (unfairly, I think) jumped on over there.


    Back on topic, I hope my question and help did, in fact, help some people. Really, because I use many of the addons by many of you, and I'd hate to see massive failures and hands in the air due to frustration.
    Last edited by Lorandii; 02-11-2012 at 03:26 PM.

  13. #13
    Rift Disciple
    Join Date
    Mar 2011
    Posts
    130

    Default

    Two things about me are "broken", one of which is my sarcasm detector
    I wasn't being sarcastic if this reassures you. I'm doing QA for a living. Sure I can program, but I'm no engineer. So ANY input to help me do better code is welcomed !

    And if I were to be sarcastic, I would have said , thanks and not change a single line of code. (Which I indicated the contrary earlier)

    fyi: English is my 2nd language, so I may have some expressions that doesn't sound exactly as I intend them to. I hate to resort to that excuse, but i have to admit that in some rare occasion, I do have to.
    All in all your help is very appreciated.
    Last edited by Frostshizzle; 02-11-2012 at 05:19 PM.

  14. #14
    Champion
    Join Date
    Jun 2011
    Posts
    561

    Default

    No worries. I meant every word the way I wrote it. Already nkDebug is released with everything private and nkAdvisor will follow today.

    Cheers
    N.

  15. #15
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default

    Quote Originally Posted by Frostshizzle View Post
    Everything is in the same file at the moment. I don't use any libraries.
    I'm sorry, I totally missed this. If everything is in one file, then the first line of code should be
    Code:
    local MyAddon = {}
    You can skip the privateTable if you don't use it. The null var or underscore only needs to be listed if you are marking a specific entry in order.
    Code:
    local MyAddon = ...
    local MyAddon, _ = ...
    local MyAddon = {}
    Are the same thing, except in the second case you are defining a variable _ and making its value == nil. That is redundant, and if you did that hundreds of times, or several addons all did that, it would eventually eat CPU and memory.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

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