View Single Post
03-06-13, 09:28 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by shalom69 View Post
One thing that was good about using xml is that the book I am using showed how to bind the keys and get them to show up in the default WOW keybindings menu. Is there a way to do that with lua as well?
You still need a dedicated Bindings.xml file to add a binding to the default Key Bindings window, but that has to be separate from any other XML content you may have (like creating frames) so you may as well not have any other XML content.

If you use a Bindings.xml file, you do not need to (and should not) call SetBinding manually from your addon. Once the user binds a key in the Key Bindings window, WoW itself will handle assigning that binding at login.

Originally Posted by shalom69 View Post
Your advice on using local for everything is noted. As I noted in the other thread, it's monkey see monkey do at this point as I'm learning. The examples I saw didn't specify "local action" and "local success" so monkey didn't type that. Semantically, "local" means only applying to that particular addon right? You're right that there's nothing in my addon that needs to be global in that case.
That's why I always yell at people for posting code with global variables. Even if they only mean it as a "quick example", other people may not realize it's not code that should be copied and pasted as-is.

Semantically, "local" means the variable only exists in that particular scope. If you write "local var = 2" at the top of your file, then you can access the variable "var" anywhere in that file to get the value 2. If you write the same line inside a function, then the variable only exists inside that function. If you define a variable as local inside a loop, it only exists inside the loop, etc.

A variable defined at the top of a function is available inside a loop that's also inside the same function. You can access variables that are local to higher scopes, but not to lower scopes. Imagine a set of Russian nesting dolls made out of one-way mirror glass. You can see out of them, but not into them. If you're in the 3rd doll from the oustide, you can see your own doll, and the two bigger dolls, but you can't see any dolls that might be inside yours.

Here are some examples:

Code:
local var = 2

local function PrintVar()
    local var = "two"
    print(var)
end

print(PrintVar())
print(var)
... will print "two" and then 2, not 2 and 2, and not "two" and "two".

Code:
local unit = "player"

for i = 1, GetNumSubgroupMembers() do
     print(unit)
     local unit = "party"..i
     print(unit)
end

print(name)
... will (in a full party) print "player", "party1", "party2", "party3", "party4", and then "player" again.

Now, if you remove the "local" declaration inside the loop:

Code:
local unit = "player"

for i = 1, GetNumSubgroupMembers() do
     print(unit)
     unit = "party"..i -- no longer local to this scope
     print(unit)
end

print(name)
... then you're using the "unit" variable from the higher-up scope, so you'll get "player", "party1", "party2", "party3", "party4" -- and then "party4" again instead of "player" like in the previous example.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 03-06-13 at 09:31 PM.
  Reply With Quote