Im trying to find a correct way to make a OnUpdate while using Ace-Addon. Right now im creating a frame, setting a OnUpdate, then im redeclaring self inside the OnUpdate function. Im guessing this isnt how your supposed to do it?
Wait, no, you changed the case of a single letter. That's not going to bite you in the ass ever...
What he said. I had to read the code four times to figure out why it wasn't exploding.
Keep in mind that the most important parser of code isn't the scripting engine, it's other programmers. Don't write for the compiler, write for your colleagues. Somebody you're going to want to not maintain that code anymore. When that day arrives, the parser isn't going to take over for you, and other programmers will only do so if they can make sense of what you write. :-) Clarity wins out over almost everything else.
Phanx, your second and third examples will fail because the function doesn't exist when you set the script. Rejigger the order and you'll be fine.
OP, while using self is good for speed, adding a function call just to get it assigned implicitly negates any performance boost you get from using self. Simply put, it's an unnecessary, wasteful call. Your first priority with OnUpdates should always be to make them as fast as possible, do as little as you can in the call.
It makes everything easier to me to be able to see self, and its not as easily broken...I used the follow wrapper to make it work.
local frame = CreateFrame("Frame", "SkullMeFrame")
SkullMe:Onupdate() is the actual function, SkullMe:OnUpdate is the wrapper
if you want to wrap a function like that, it's much easier to just add an anonymous function instead.
frame:SetScript("OnUpdate", function(frame, elapsed) SkullMe:OnUpdate(elapsed) end )
not sure if that's exactly what you're looking for, but that replaces the frame reference with your SkullMe table... you could also call SkullMe:OnUpdate(frame,elapsed) if you wanted to know what frame fired off the event...
So the question becomes, Do i reference a global 5-10 times each frame or do I wrap the function to redefine a local or do I use self = SkullMe. Bah time to os.clock everything.
Edit: You cant os.clock in WoW. Thats pretty lame or im missing something.
Don't wrap your function.
And localize the global you are accessing in your function.
Example, you want to call the global function Bar() several times
frame.elapsed = 0
frame:SetScript("OnUpdate", function(frame, elapsed)
-- Throtle OnUpdate to run every 0.2 secs max
if frame.elapsed > 0.2 then
frame.elapsed = 0 -- Reset timer
local Bar = Bar -- Grab global Bar and assign it to the local Bar
-- Do stuff
frame.result = Bar() + SkullMe.Foo() -- Access local Bar
frame.elapsed = frame.elapsed + elapsed
"local blah = blah" is a very common lua construct to get a local reference to a global, used for optimization. Common targets are frequently used functions such as pairs() tinsert() and Blizzard API functions that relate to your addon. You will often see some of these locals used in file scope, such as "local pairs = pairs" at the very top of a Lua file.
The point here is, if at the top of the file, you have
SkullMe = blahblahblah -- Assigns blahblahblah to global SkullMe
local SkullMe = SkullMe -- Assigns the global SkullMe to a local SkullMe.
-- Rest of code in the file will access the local file scope SkullMe,
-- including the frame's OnUpdate script because of scope resolution rules.
If you want to access the global while in the scope of a local, you use _G["SkullMe"] or _G.SkullMe
Okay so im probably going to get 50 lashes with a wet noodle for asking this. If im going to set a local SkullMe, Can't I just rename it self while im at it?...Anywhere self would be used in my code and it isnt defined it would default to my file scope local.
Also did that question about local self = SkullMe at the top of the file break some forbidden rule or is that a reasonable solution in the future..I mean it doesnt SEEM that bad but I have a feeling theres a problem with doing that.
At least i havent tryed to save _G yet as a saved var.