Table Structure in a Saved Variable
I'm writting an addon that creates a savedvariable with a table of all addons and if they are enabled or disabled. The addon works, but I am not understanding the how the table structure is created. What I expexcted and what is written are two entirely different things. If someone could shed some light on this, it would be greatly appreciated. Following is the code involved and a snippet of the saved variable.
Lua Code:
Lua Code:
Ignore the comments, I am experimenting with code and a slash command. Lua Code:
What I was expecting in the saved variable is the addon name and if it is loaded/enabled. The first few entries confirm this, but some addons do not have true or false, it lists the title as seen by the last few entries. I am not sure why this is happening. Also, where do the commented out numbers come from? The -- [1] etc... Thanks and I hope everyone is enjoying the holidays no matter how you celebrate it. |
The numbers come from when the game exports a table (SavedVariable) without labels specified for keys
Code:
{ Code:
{ Code:
{ Using ADDON_LOADED (with your addon as the execution marker) will show all the addons that have been loaded prior to yours (and if they were succesful or not) and addons that are still to be loaded (after yours) but at this point, it is undetermined if they will load. You can use ADD_LOADED to check for addons that load during play (Load On Demand) ie. register the event after you've done your check at PLAYER_LOGIN. Edit: Quote:
Code:
AddonListDB[i] = {title,loaded} |
Thanks for clarifying things for me. I'm reworking my code, but have a question on how to handle nil for a table value. Is there a Lua function or some other way to trap nil and replace it with a string value that would work in a table? I've tried GoogleFu, but I only found confusing information.
|
Code:
local TestTable = { In your code you could still do: Code:
if not AddonListDB[i].loaded then |
Thanks again for your replies, I realized that I should stop asking questions when I am tired, They make less sense to everyone.
I may have not wrote the addon correctly, it works sort of. Here are more questions that hopefully will set off the light bulb above my head. :) I really have tried Google, but I welcome links to the appropriate topic. 1. How could I replace the nil value in loaded with a string that a table would like for this code? In the event the addon was not loaded. Lua Code:
2. I've tried a lot of different methods to set labels for table entries, with no luck. What am I missing so that I could have something like this: Lua Code:
3. It has dawned on me that I might need to use the local saved variable instead of the one listed in the .TOC. This line seems essential. Lua Code:
4. Finally, is there any advantage to using Lua table methods like table.insert versus savedvariable[i] = "something"? |
Lua Code:
This is a sort of shortcut on how Lua parses. This is called a ternary operator. If loaded is true or has a value, that will be used. If loaded is false or nil, it will go to the second part, which is also false, which is what it will use. |
Code:
TestTable = { Code:
print(TestTable.KeyName) Code:
local key = "KeyName" Code:
addonlistdb = AddonListDB Code:
addonlistdb.loaded = true tinsert allows you to assign a position and re-order if required whereas using savedvariable[i] = "something" will overwrite the i key if it exists. As an aside your code: Code:
addonlistdb = AddonListDB Code:
variable = {} if you do addonlistdb = AddonListDB before the table is created then addonlistdb will be forever nil (unless you re-assign it). if you do AddonListDB = {} after addonlistdb = AddonListDB (asuming AddonListDB existed) addonlistdb will be pointing to the old table until it is garbage collected. A simple example of table pointers: Code:
/run local t = {} local v = t print("A", t, v) v.k1 = 12 print("B", t.k1, v.k1) t = {} print("C", t, v) |
Solution!
OK, so after much reading, more Googling and so much trial and error, I have working code.
Lua Code:
This generates the following saved variable file. Lua Code:
It currently only triggers on PLAYER_LOGIN, but I'll work on ADDON_LOADED later. I admit that I am slightly puzzled how the SetScript works in this case. I am more used to the usual SetScript("OnEvent", Function). Lua has multiple ways of doing things and for some reason, I find the language harder to get a handle on than other languages. Any suggestions are still welcome. This is a good experience for me to learn more about tables and saved variables. Thanks again. |
Just register ADDON_LOADED alongside PLAYER_LOGIN. OnEvent fires whenever any of the events registered to the frame happen.
I guess if you're doing it with separate functions for each event then you would also need to write the same one but for ADDON_LOADED. Though, for what you're doing, I'm honestly not sure why it needs to be separate from your OnEvent script. (What does your script at lines 6-9 actually do, anyway?) |
The SetScript is creating the function "in situ" rather than calling another function. Either way works although the SetScript("script", function) is better if multiple scripts can use the same function.
If you mean the Code:
if self[event] then return self[event](self, ...) Code:
if JWFrame["PLAYER_LOGIN"] then return JWFrame["PLAYER_LOGIN"](self, ...) Code:
function JWFrame:PLAYER_LOGIN() |
Quote:
Code:
if not JWFrame.ADDON_LOADED then |
I should probably also point out, in case you aren't aware, that adding your onevent (or any) functions to your frame
Code:
function JWFrame:PLAYER_LOGIN() |
Quote:
|
If you want to keep using the table lookup approach, you could use a local table rather than the frame.
Lua Code:
|
Just having come to my seneses, unless you are looking up the information in-game during a session, you can run the process just once at PLAYER_LOGOUT.
Lua Code:
|
Quote:
AddonListDB = AddonListDB or {} addonlistdb = AddonListDB In any event, I really appreciate your help. |
Quote:
Consider, you login one day with 24 addons, you exit. The next day you decide you only need 17 of them. Keeping the SavedVariable information from the previous day intact means that slots 18-24 will contain stale and possibly duplicate (possibly incorrect) information as they won't be overwritten using the new list of only 17 addons. Wiping the table each time means you have current data. With the code you've given, Code:
local addonlistdb Code:
AddonListDB = AddonListDB or {} |
Quote:
I forgot the golden rule when asking for help, tell them what you are trying to do. So, my grand idea is thr following: 1. Get list of all addons at PLAYER_LOGOUT (I liked your suggestion). 2. Next time you logout of the game, compare your current addon state (enabled or disabled) to the old list. 3. Update the date/time stamp (see code below, minor modification) if the current addon state is different from the old data. 4. NOT IMPLEMENTD YET: At defined intervals, remind player that an addon has not been used for x period of time, say 30 days. 5. This helps the player decide if they still want the addon installed. Why? I frequently install and addon and eventually stop using it, I'd like a reminder to uninstall it due to lack of use. Lua Code:
Lua Code:
I need to work up the compare logic and then later the reminder mechanism. Is this a good approach to what I am trying to accomplish? Maybe there are better ways to do this. |
Quote:
You can make a lot of assumtions about the definition of how "useful" an addon is if it is just for your own use. Some addon configs are seperate addons that are LOD but may be "useful" occaisionally. Addons that are tagged "LoadWith" might not load every time but might still be "useful". Every addon that loads by default may not be "useful". Sorry, it's the devil's advocate in me, you may well have this covered. |
Quote:
Do variables created inside a local function need the local? My thought for the date is compare current addon load state with the savedvariable and update if it is different (i.e. loaded and not loaded). I'll worry about the LOD and LoadWith later. If they are both disabled, do not update the date/time. This will give me a semi accurate idea of when it was last loaded. For the most part, this should work except for what we mentioned. I'm trying this code for the logic, but I'm not quite getting it right. Lua Code:
|
All times are GMT -6. The time now is 05:02 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI