Function questions
So i have a function like that:
Code:
function PVPSound_UpdateSoundEffectEngine(self, elapsed) local addon, ns = ... local PVPSound = { } ns.PVPSound = PVPSound Code:
function PVPSound:UpdateSoundEffectEngine(self, elapsed) Also why cant call the function like that with the SetScript? |
Just some code, maybe it helps:
Code:
local test = {} -- local example table If you want a local function you do local function onupdate(self, elapsed) ... end and now you can SetScript directly to "onupdate", and it is a local function. The benefit of keeping everything to your "ns" for example is that all the files can access those functions trough the namespace. |
Quote:
lua Code:
|
Quote:
Code:
local function OnUpdate(self, elapded) |
I guess you could, but why would you?
You're creating a function just to call another function that does all the stuff (I presume). As long as your PVPSound table is local, the UpdateSoundEffectEngine function will also be local and you can set it as the script handler. Lua Code:
|
Quote:
Well the PVPSound table is a local, but its a namespace shared local between my addon's files. |
Just keep in mind that the word "self" is what ever caller calls the function.
For example: Code:
local t = {a = 5} Also declaring a function, it's important to remember that: Code:
function t:test() print(self) end -- "self" is the object "t" Code:
frame:SetScript("OnUpdate", t.test) For instance your function ns:OnUpdate(elapsed) end if you frame:SetScript("OnUpdate", ns.OnUpdate) then when the function is called the "self" object will refer to "frame" and not "ns", so if you try to for instance call another function you should use ns:OtherFunc() and not self:OtherFunc() like you might expect. Sorry for messy post. |
Quote:
Code:
function t:test(self) |
Quote:
|
You would replace the "self" (the "t" object reference) with what ever argument you pass in the function as you run it.
|
Quote:
Code:
local _, ns = ... Code:
local _, ns = ... Code:
PVPSoundEffectSoundEngineFrame:SetScript("OnUpdate", function(self, elapsed) Also, when you set a list of variables with the same name, when you're done, there is only one copy of the variable, and it contains the last value set: Code:
local var, var, var, var = 1, 2, "cat", "dog" Code:
function object:method(self) Code:
function objec*****thod(self, self) |
Quote:
Quote:
Code:
local PVPSoundEffectSoundEngineFrame = CreateFrame("Frame", "PVPSoundEffectSoundEngineFrame") Quote:
|
On a slightly unrelated note, the following:
Quote:
Code:
if PS_EnableAddon and PS_SoundEffect then |
Quote:
Code:
if something then Code:
if something ~= nil and something ~= false then |
Why would PS_EnableAddon ever have a string value, when it only ever gets set to true/false? But each to their own I guess :P
|
Quote:
|
If it's a global, then the "PS_" prefix seems a bit short to identify an addon. There is probably quite a few with the "PS" initials. Perhaps use the full addon name for the global prefix, to avoid collisions with other addons.
|
Code smarter, not harder.
|
Quote:
We must code smarter, not harder. Faster, not smarter. And forever looping, looping, LOOPING towards freedom! |
Does your handler really need to be run outside of the frame triggering it by OnUpdate? If not, it may be easier to use a different syntax.
For example, this method of defining a function also works. Code:
Func = function(args) Using that method, you can dynamically create a function just for the frame's OnUpdate handler. Code:
Frame:SetScript("OnUpdate",function(self,elapsed) |
All times are GMT -6. The time now is 01:57 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI