![]() |
Quote:
I this a lot of the problems I'm having here relate to my PC - it's not amazingly quick (it's 2 years old and wasn't state-of-the-art then!!) - but things like this and the issues I raised with 'losing' macros etc. (in another thread) would be explained by my PC just being a bit slow... Not a problem for you guys I'm guessing BUT you might want to bear-in-mind that some of our USERS may have simiarly slow PCs Example My NowCarrying addon wants to ignore all the initial BAG_UPDATE events that people are spammed with on login - so I put in a manual delay of 0.5 sec before it starts to check them. Worked fine for me - (a test suggested I only needed to wait about 0.2sec but I added a bit anyway) but a few people said they were seeing the 'spam' caused by those events so I increased it to 1sec Then I get WOTLK and I started to see the spam myself so I increased it to 2 secs!! So far so good - no-one has reported the spam since that change - but it just shows that your addons can behave differently on slower PCs/newer builds of WoW in ways you don't expect :( p.s. Mik - thanks for the Frame example code - I've nicked it wholesale :) |
Carrying on the savedvariables thing...
I have to initialise my SVs at some point - where the user hasn't previously 'saved' them. All my addons upto now have done this at the highest level - e.g. Code:
MYSV = "" What someone suggested here earlier is that what I SHOULD be doing is more like Code:
function MYVARIABLESLOADEDEVENT () I ask mainly because I have a VERY rare bug whereby MYSV is being reset to "" by something - and the ONLY place it's reset is at the top of the .LUA file (as in the first example). Why on EARTH this would happen anytime other than login/reload I've no idea - but as MYSV is never set to anything else, anywhere else - I don't know what else it could be! |
There are definitely some timing consideration to keep in mind, but I'm fairly certain GetSpellInfo is valid before the first addon is loaded.
Also, I know you're just posting pseudocode to a degree, but you should try to avoid using global functions unless you really need to, which in the case of your event callbacks you no longer need to since you are passing them directly via :SetScript instead of calling them globally out of an XML file. Anyways on to the saved variables. There really isn't a need to initialize them at the highest level. Just construct your code such that the functions that rely on saved variables aren't called until they are valid. If you're using event callbacks that make use of your SV, then don't register them until after your SV are loaded or initialized via the ADDON_LOADED. There are many ways to do it, but here is an example of one way. Code:
local function OnAddonLoaded(self) |
Quote:
Quote:
function MyFrame.onevent(... rather than function MYMOD_onevent(... which I assume is seen as being the proper way? |
Ah yes, I didn't know you were trying to use GetSpellInfo with a spell name. In that case, it won't work until your spell book is updated (which is signaled with SPELLS_CHANGED). Also, it also won't retrieve information about a spell name that doesn't exist in your spell book at any time.
On the functions, yes it's better to create a "namespace" for your mod and define all publicly accessible functions/data as a part of it. MyMod = {} function MyMod.PubliclyAccessibleFunction(args...) -or- function MyMod:PubliclyAccessibleFunction(args...) Event handlers however rarely need to be called outside of in response to an event and therefore can almost always by a completely local function like I demonstrated in the previous code. |
Quote:
|
Quote:
|
Back on the SV thing - taking this code that Mik posted earlier
Code:
local function OnAddonLoaded(self) Once it's been saved and reloaded it will exist - but until that time it doesn't and thus you'll get an error? |
Quote:
Pay attention to the fact that the OnBagUpdate will NEVER be called prior to the loading or initialization of the SV because it's not registered until AFTER they are already loaded or initialized. What I posted is fully functional as is. Try it out. Open your bags and move something. You'll get a message. MyMod.toc Code:
## Interface: 20400 Use all the code I posted earlier in post #23. |
So if we take this function
Code:
function test() I assume this would not be the case if we declared Code:
local function test() |
Quote:
You decide to create an event handler called OW_OnEvent. Another mod author comes along and decides to create a mod that has the initials OW and predictably create his event handler to be called....you guessed it, OW_OnEvent. Oops whoever loaded first is now hosed because both XML files try to call OW_OnEvent which just got overwritten. On the other hand, had they both been namespaced under their mod's name, there would be no problem. There are also performance reasons too, but that would required an explanation of how lua works internally, and that is better left to a book. |
Quote:
e.g. OW = {}; OW.myfunc = ..... and someone else declares the same namespace/table - same problem? :) |
Quote:
Code:
local function test() |
This was mentioned a couple of times, but I haven't seen the OP address it and I think it may be the key to a solution here:
Does PLAYER_ENTERING_WORLD work? Yes, you only want the initialization to happen once and PEW can be called multiple times per session, but you can simply unregister the event as part of the initialization. Example: Code:
MyAddon = {} |
Quote:
|
Quote:
|
So a variable declared in a LOCAL function is still GLOBAL!? That's surprising and it means my function from earlier needs to be messy...
Code:
function test() Code:
function test() |
Quote:
e.g. Code:
local eventFrame = CreateFrame("Frame","OBI") Code:
local eventFrame = CreateFrame("Frame","OBI") |
Yes. That's correct. Although I don't know how adding one additional line of code to make your variables local to the function (as they should be) makes it messy.
On the frame question yes, but realize that nothing outside of the current file would then be able to access those functions because the frame is local to the chunk. |
Thanks for the time mik - very informative :)
On the Frame thing - and using it as your 'namespace' - surely you could define your frame as a global for that purpose? Then that means ALL your frame's events etc are global i guess and you're back where you didn't want to be? :) |
All times are GMT -6. The time now is 09:32 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI