Hey there, looking for some assistance dealing with a little problem I have with AceDB. I'll try to illustrate.
My addon's designed to hold a list of messages in a table and pick a random one to send periodically. Using AceDB, this list is pre-populated with some default messages (2 in this case). The user can modify the table and add, delete or change messages at will.
This works fine if the user has entered the same number (or more) messages than the default has given. But if the user deletes all but one message, AceDB will add back in the second default message the next time the user logs in or reloads. In most cases, this is the expected and appropriate behavior for how the defaults should work, but obviously the strategy is not working well in this case.
I have a band-aid fix on it right now... Every time the user modifies the message table, I capture and store the number of message they saved. Then, every time the addon is loaded, I check if there are more default messages than the user had previously saved, and delete all the extra default messages. Code looks like this:
[PHP]
if LWB.db.profile.usePhrasesCount and #LWB.dbDefaults.profile.usePhrases > LWB.db.profile.usePhrasesCount then
local t = {}
for i=1,LWB.db.profile.usePhrasesCount do
t[i] = LWB.db.profile.usePhrases[i]
end
LWB.db.profile.usePhrases = t
end
[/PHP]
The solution seems to work fine, but it's a lot of code, especially when I have to repeat it for every table of messages. I'm sure this isn't the first time that a user is supposed to have been able to delete some of the defaults in a table without having them come back, so I'm curious if there's a better way to handle this situation. Thanks for any feedback you can provide :-)
if the user can delete a "default" value then its not a default value. defaults are just that, values that get used when there is none there.
what you may want to do is have another variable, say phrase_init, with a value of false, during your mods init (after the acedb is registered) you check if its false and if it is then you add those two "sample" messages and set it to true, that way the users gets some "sample" messages on startup that wont come back if they delete them.
Ya, in a way, I want it to behave like "sample" like you described. But in most ways, I still want it to behave like a default value. Like when the user resets their profile, if all messages are removed, or if the user creates a new profile, AceDB performs exactly as I want it to. I could probably duplicate all those mechanics without AceDB, but it's probably easier for me to deal with AceDB and and do some tweaks to work around the one behavior I don't like.
I'll play with some options and see what I can come up with. Thanks :-)
MyAddon:CheckDefaultMessages()
if #self.db.profile.messages == 0 then
self.db.profile.messages[1] = "This is a sample message."
self.db.profile.messages[2] = "This is another sample message."
end
end
Call your addon's CheckDefaultMessages method whenever (a) the user removes a message, (b) the active profile is changed, or (c) your addon's DB is initialized.
Obviously I'm a bit of a novice at this. until a few moments ago, I didn't know how to make that function run when the profile changed... in fact, I just learned what a "callback" was. That makes this much easier, thanks for pointing me in the right direction. In fact, this solution is even better since it can add the defaults back in even before the client is reloaded, by making it run when a user changes (potentially removing) the messages.
Cheers, and thanks both of you!
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
My addon's designed to hold a list of messages in a table and pick a random one to send periodically. Using AceDB, this list is pre-populated with some default messages (2 in this case). The user can modify the table and add, delete or change messages at will.
This works fine if the user has entered the same number (or more) messages than the default has given. But if the user deletes all but one message, AceDB will add back in the second default message the next time the user logs in or reloads. In most cases, this is the expected and appropriate behavior for how the defaults should work, but obviously the strategy is not working well in this case.
I have a band-aid fix on it right now... Every time the user modifies the message table, I capture and store the number of message they saved. Then, every time the addon is loaded, I check if there are more default messages than the user had previously saved, and delete all the extra default messages. Code looks like this:
[PHP]
if LWB.db.profile.usePhrasesCount and #LWB.dbDefaults.profile.usePhrases > LWB.db.profile.usePhrasesCount then
local t = {}
for i=1,LWB.db.profile.usePhrasesCount do
t[i] = LWB.db.profile.usePhrases[i]
end
LWB.db.profile.usePhrases = t
end
[/PHP]
The solution seems to work fine, but it's a lot of code, especially when I have to repeat it for every table of messages. I'm sure this isn't the first time that a user is supposed to have been able to delete some of the defaults in a table without having them come back, so I'm curious if there's a better way to handle this situation. Thanks for any feedback you can provide :-)
what you may want to do is have another variable, say phrase_init, with a value of false, during your mods init (after the acedb is registered) you check if its false and if it is then you add those two "sample" messages and set it to true, that way the users gets some "sample" messages on startup that wont come back if they delete them.
Ya, in a way, I want it to behave like "sample" like you described. But in most ways, I still want it to behave like a default value. Like when the user resets their profile, if all messages are removed, or if the user creates a new profile, AceDB performs exactly as I want it to. I could probably duplicate all those mechanics without AceDB, but it's probably easier for me to deal with AceDB and and do some tweaks to work around the one behavior I don't like.
I'll play with some options and see what I can come up with. Thanks :-)
Additional function:
Call your addon's CheckDefaultMessages method whenever (a) the user removes a message, (b) the active profile is changed, or (c) your addon's DB is initialized.
Cheers, and thanks both of you!