Go to Page... |
Thread Tools | Display Modes |
04-30-17, 06:49 AM | #1 |
(AceDB-3.0) I don't get where my codes are going wrong.
Hi all,
Sorry for keep asking a Ace3.0 related questions here. Take all and put together in one lump, Wowinterface is actually the most activated community to discuss this (even if it is a specific library related question). SO, I am currently trying to re-write one of my addon that tracks various data of player such as currency, money, class, etc and here's what I've got so far. ToonTracker.toc Code:
## Interface: 70200 ## Title: ToonTracker ## Author: Layback ## SavedVariables: ToonTrackerDB ToonTracker.lua currency.lua Lua Code:
currency.lua Lua Code:
And here's the output when I first log-in to the toon. class is well set, but neither money nor currency table are correctly filled with the actual values. (strangely currency table is properly formed with currencyIDs lol......) In order to fix this, I'll have to forcibly reload an ui, which is stupid Could I get some advice, please? Thank you! ---------------------------------------------------------------------------------------------------------------- EDIT Would that be because both :OnInitialize() functions are called simultaneously, which I don't think they are. If they ran at a time, accessing db in Currency:OnInitialize() should return nil, shouldn't it? Last edited by Layback_ : 04-30-17 at 07:13 AM. |
|
04-30-17, 02:12 PM | #2 |
Have you tried using :OnEnable()? I built my currency tracker during LK beta so my memory is a bit fuzzy but there are a fair number of things the client doesn't have right off the bat.
__________________
Addons ∙ Executive Assistant User Configurable To-Do List ∙ Legible Mail Choose the Font for Your Mail ∙ |
|
04-30-17, 08:04 PM | #3 | |
you mean :OnEnable() for module, not addon, right? I'll have a go and let you know ! |
||
04-30-17, 08:35 PM | #4 | |
The only change was Currency.lua Lua Code:
What would be cauinsg this, lol...? |
||
04-30-17, 08:40 PM | #5 |
OnInitialize() only fires once per addon or module, meaning it fires once for the addon, and once for each and every loaded module. Even if you /reloadui it still won't fire again. It would be the equivalent of registering for ADDON_LOADED (for saved variables) and PLAYER_ENTERING_WORLD (for miscellaneous other start up code) and then unregistering both events. OnInitialize() just makes that process a bit cleaner.
OnEnable() is somewhat like PEW, keeping in mind that it is never unregistered. Each time you /reloadui, OnEnable() fires. If you are not getting the correct currency information, that probably means the game cannot provide that information ... yet. With Ace3, you register events and library callbacks in OnEnable(). And handling events is always through a function, then register for PLAYER_MONEY in currency.lua's OnEnable(), and immediately call that function from OnEnable(). In your PLAYER_MONEY handler function, have a line that reads: Lua Code:
|
|
04-30-17, 10:10 PM | #6 | |
So, does the following look better?! Lua Code:
+ Another question AceEvent-3.0 has two ways of registering handler function to an event. First is like my code above, while the second one is passing as a argument on event registration. e.g: Lua Code:
Do they have any big difference? Last edited by Layback_ : 04-30-17 at 10:24 PM. |
||
05-01-17, 12:36 AM | #7 |
Looking better. Here's a couple of notes:
Lua Code:
Lua Code:
The callback method is convenient if there were, say, a few very similar events (ie: their args are virtually the same) and you wanted to handle all the events in the same function to make things easier on you when coding. For example, let's say there are three events. Since all events pass the event name as the first arg, I will list the args as expected. However, AceEvent knows this, and you do not need to account for this when registering.
Lua Code:
|
|
05-01-17, 06:42 AM | #8 |
Hi again myrroddin,
Thank you so much for such a detailed explanations! I really appreciate it !! |
|
05-01-17, 07:52 AM | #9 |
You are most welcome. Another tip: if you register a few events via the callback method, I did mention that they have to have similar args. That said, the args can be sequentially similar.
Let's say your three events return the following, besides the necessary event name itself:
Lua Code:
|
|
05-01-17, 08:12 AM | #10 |
Yet another tip, you can use UnregisterAllEvents() in OnDisable() rather than listing each one individually. Of course, if there were events you do not want unregistered when putting the addon into standby mode, you'd either have to register them again in OnDisable() or list all your events individually.
UnregisterAllEvents() takes no arguments, and it is slower, as AceEvent has to loop through all registered events to unregister them. However, unless you are registering dozens of events and unregistering them rapidly, nobody will ever know you have done this. Modern computers are just way too fast! Conversely, there is also RegisterAllEvents() which does exactly that: registers all events in the entire game. Unless you are a madman, don't do that! RegisterEvent, RegisterAllEvents, UnregisterEvent, and UnregisterAllEvents are all native to WoW. AceEvent only really offers passing the additional argument as something new. Also, AceEvent does not support RegisterUnitEvent, but UnregisterAllEvents will unregister those hooks. In other words, if you ever need to use RegisterUnitEvent, then do not use AceEvent. Do not load it via your toc, do not embed it in your AceAddon addon table creation line, just do not use AceEvent. Last edited by myrroddin : 05-01-17 at 08:19 AM. Reason: further thoughts |
|
05-01-17, 04:48 PM | #11 |
Hi myrroddin,
Thank you for extra tips! 1. What if I was to use a handler to figure out which event is called and pass the arguments like the following? Lua Code:
"PLAYER_MONEY" and "CURRENCY_DISPLAY_UPDATE" doesn't have an argument, but "CHAT_MSG_CURRENCY" and "GUILD_ROSTER_UPDATE" have some arguments which differ from each other. In that case, should I still consider making separate handler for those events? 2. To be honest, I still haven't decided whether I should keep using an AceEvent-3.0 or not |
|
05-01-17, 05:16 PM | #12 |
The main reason for separating event handling code is for your own readability. Long if-chains can be difficult to read, and separating the event can make the code neat.
I wouldn't recommend using AceEvent. It's not bad, it just doesn't do anything that you can't accomlish yourself with a couple of lines of code. (unless maybe if you want to use internal addon callbacks) Code:
local frame = CreateFrame("Frame") frame:SetScript("OnEvent", function(self, event, ...) Currency[event](Currency, ...) end) Code:
function Currency:EVENTX() end Currency.OnEventX = Currency.EVENTX
__________________
Grab your sword and fight the Horde! |
|
05-01-17, 06:26 PM | #13 |
What Lombra said. There are two main reasons to use AceEvent-3.0:
|
|
05-02-17, 07:15 PM | #14 |
Many thanks to Lombra and myrroddin.
Yeah, guess I should not use AceEvent-3.0 until I see the necessity Gotta change all my codes haha |
|
05-02-17, 07:45 PM | #15 |
Oh, and this might be the very last question.
It's not related to Ace3, but is kinda close to the topic of this thread(?). How do you guys think of functions being created within a function like the following? Lua Code:
The reason that I have done this is to prevent those 'bar' related event functions being created when the frame (bar) doesn't even exist, yet, and I would like to avoid wasting a memory by creating those functions in local which will never be used when module is disabled. However, I have seen a post saying that this kind of structure has performance issue as it takes more time than it does with creating a local functions. |
|
05-03-17, 02:23 AM | #16 |
That looks confusing to me.
You can blend Ace3 and vanilla WoW code easily enough. Lua Code:
Last edited by myrroddin : 05-03-17 at 02:25 AM. |
|
05-03-17, 04:25 AM | #17 |
Just edited codes based on your advice!
Lua Code:
1. Transferred RegisterEvent(...) functions to :OnEnable() function from InitObjects() function. 2. Added UnregisterAllEvents(...) function on :OnDisable() function. 3. Transferred InitObjects() function (which creates frames and other objects) call to :OnInitialize() function from :OnEnable() function. 4. On L#39, I've added self (bar) as an argument for just in case if I need them (which would be redundant as Power.bar is same anyways ). If I missed something, please let me know!! + About InitObject() function, I have been told that once it is declared as local at the top of .lua file, it functions as local, thus I didn't put local at L#63. |
|
05-04-17, 07:12 AM | #18 |
Here's one more question!!
How would you manage a module with more than one lua files? For example, my Unitframe (which I made with oUF) is made up of five different lua files called:
If the module is disabled, those 24 local functions and function calls on core.lua becomes useless, but occupies memory. What would be the best practice for this case? |
|
05-04-17, 01:08 PM | #19 |
You would load those lua files in your toc as usual. When the module is disabled, you are correct, their functionality also gets disabled, and Lua will garbage collect as usual.
Without more information, that is the best answer I can provide right now. |
|
05-04-17, 03:59 PM | #20 |
Hm... I'm still confused.
You mean that those local functions will also get collected by garbage collection? Here I've attached my addon. If you don't mind, could you please have a look at them when you have a spare time? (OnInitialize(), OnEnable() & OnDisable() functions for Unitframe module are not properly used, yet...) Thank you!! Last edited by Layback_ : 05-04-17 at 06:40 PM. |
|
WoWInterface » Developer Discussions » Lua/XML Help » (AceDB-3.0) I don't get where my codes are going wrong. |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|