1. Howdy,

    Two projects that I would like to work on would involve saving character data (name, level, class, etc...) to a player generated list, similar to the friend and guild member lists. I'm therefore looking for information/resources that will describe how to design and build a dynamic database for WoW AddOns. Can anyone point me to such resources?

    Thank you,

    -z

    I never found a "good" resource on my own. Took hours and hours of scanning Google to put together several Resources to do it.

    First thing you need to do is add a saved variable to your TOC file:

     ## SavedVariables: MyAddonDataBase           --(This Saved the variable across all Characters)
     ## SavedVariablesPerCharacter: MyAddonDataBase --(Saves only to individual characters)
    

    Now, these variables are loaded after the end of your addon is. and isn't immediatly avaialble. So dont have any OnLoad event trigger then attempt to read from these variables. To determine when the variables are loaded you can use either "VARIABLES_LOADED" or "ADDON_LOADED" events.

    Then you need to create a table of Data. Tables are very useful, a bit tricky to learn, but well worth the effort. First you need to determine what information you want to save to your table. For this Example I'll do Name, Class, Race, Level. Before you can do anything to a table function you need to declare it with "MyAddonDataBase = {}". So I recommend that after your "VARIABLES_LOADED" triggers you do a "if not ( MyAddonDataBase ) then MyAddonDataBase = {}; end". Now tables are sorted into "Keys" and "Values". The Key is the name of an entry, its like the "First Name" text on a form you have to fill out, the "Value" is what you put in that box. YOu can create new keys at anytime and change values at anytime. But looking up a Key that doesn't exist will cause an error so be careful to make sure the information exists before you added it to any messages or perform a function on it.

    SO lets create this DataBase for "George", and lets say he is mouseover. You can do: MyAddonDataBase["George"] = { ["Name"] = "George", ["Level"] = UnitLevel("mouseover"), ["Race"] = UnitRace("mouseover"), ["Class"] = UnitClass("mouseover")} (Although you might not know his name when you create it so use "MyAddonDataBase[UnitName("mouseover")]" instead.

    you can also do it on individual lines like:

     MyAddonDataBase["George"] = { 
     ["Name"] = "George", 
     ["Level"] = UnitLevel("mouseover"), 
     ["Race"] = UnitRace("mouseover"), 
     ["Class"] = UnitClass("mouseover")
     }
    

    Now lets say you wanted to display his level you would look up the information by calling its key like this: "MyAddonDataBase["George"].Level" and you could change his level with: "MyAddonDataBase["George"].Level = NewValue" So the values "MyAddonDataBase["George"].Level" would be differn't from "MyAddonDataBase["Chris"].Level"

    to go though the tables you need to understand the difference between named, and numerical keys. named keys such as "George" appear randomly in the table with no order once so ever. Numberic such as 1, 2, 3 do the same thing but you can look them up in order by using their numbers.

    So lets say you wanted to print out the entire database in your chat log: you use the command "for i,v in pairs(TableName) do", where "TableName" in this case would be "MyAddonDataBase", "i" is going to represent each key in the database, while "v" will represent each value. so that "v.Name" will always be the name of the player in "MyAddonDataBase[i].Name"

     for i,v in pairs(MyAddonDataBase) do 
     print("Name:", v.Name, "Level", v.Level, v.Race, v.Class)
     end
    

    the output may be: George 80 Tauren Shaman Chris 79 Troll Warrior Bobby 80 Gnome Mage

    I hope this helps you, check out WoWWiki for more help or email me anytime: mirrikat45@gmail.com

    MyAddon.lua:

     function MyAddon_OnEvent(arg1, EventName, Prefix)
     if ( EventName == "ADDON_LOADED" ) and ( Prefix == "MyAddonName" ) then
     if not ( MyAddonDataBase ) then MyAddonDataBase = {}; end
     end
     end
    
     function MyAddon_OnMouseover(arg1)
     UnitName = GameTooltip:GetUnit()
     MyAddonDataBase[UnitName] = { ["Name"] = UnitName, ["Level"] = UnitLevel("mousover"), ["Race"] = UnitRace("mouseover"), ["Class"] = UnitClass("mouseover") }
     end
    
     function MyAddon_PrintDataBase()
     for i,v in pairs(MyAddonDataBase) do 
     print("Name:", v.Name, "Level", v.Level, v.Race, v.Class)
     end
     end
    
     GameTooltip:HookScript("OnTooltipSetUnit", MyAddon_OnMouseover)
    

    MyAddon.toc:

     ## Title: MyAddon
     ## Interface: 30000
     ## SavedVariables: MyAddonDataBase
     MyAddon.lua
    

    I never found a "good" resource on my own. Took hours and hours of scanning Google to put together several Resources to do it.

    Is there any particular reason that the chapters that detail SavedVariables in our book not sufficient for you?

    First thing you need to do is add a saved variable to your TOC file:

     ## SavedVariables: MyAddonDataBase           --(This Saved the variable across all Characters)
     ## SavedVariablesPerCharacter: MyAddonDataBase --(Saves only to individual characters)
    

    Now, these variables are loaded after the end of your addon is. and isn't immediatly avaialble. So dont have any OnLoad event trigger then attempt to read from these variables. To determine when the variables are loaded you can use either "VARIABLES_LOADED" or "ADDON_LOADED" events.

    You should not be using VARIABLES_LOADED. The use of it is not for custom addons. Instead you should be using the ADDON_LOADED event, checking that the first argument matches the name of your addon.

    Then you need to create a table of Data. Tables are very useful, a bit tricky to learn, but well worth the effort. First you need to determine what information you want to save to your table. For this Example I'll do Name, Class, Race, Level. Before you can do anything to a table function you need to declare it with "MyAddonDataBase = {}". So I recommend that after your "VARIABLES_LOADED" triggers you do a "if not ( MyAddonDataBase ) then MyAddonDataBase = {}; end". Now tables are sorted into "Keys" and "Values". The Key is the name of an entry, its like the "First Name" text on a form you have to fill out, the "Value" is what you put in that box. YOu can create new keys at anytime and change values at anytime. But looking up a Key that doesn't exist will cause an error so be careful to make sure the information exists before you added it to any messages or perform a function on it.

    SO lets create this DataBase for "George", and lets say he is mouseover. You can do: MyAddonDataBase["George"] = { ["Name"] = "George", ["Level"] = UnitLevel("mouseover"), ["Race"] = UnitRace("mouseover"), ["Class"] = UnitClass("mouseover")} (Although you might not know his name when you create it so use "MyAddonDataBase[UnitName("mouseover")]" instead.

    you can also do it on individual lines like:

     MyAddonDataBase["George"] = { 
     ["Name"] = "George", 
     ["Level"] = UnitLevel("mouseover"), 
     ["Race"] = UnitRace("mouseover"), 
     ["Class"] = UnitClass("mouseover")
     }
    

    Now lets say you wanted to display his level you would look up the information by calling its key like this: "MyAddonDataBase["George"].Level" and you could change his level with: "MyAddonDataBase["George"].Level = NewValue" So the values "MyAddonDataBase["George"].Level" would be differn't from "MyAddonDataBase["Chris"].Level"

    to go though the tables you need to understand the difference between named, and numerical keys. named keys such as "George" appear randomly in the table with no order once so ever. Numberic such as 1, 2, 3 do the same thing but you can look them up in order by using their numbers.

    So lets say you wanted to print out the entire database in your chat log: you use the command "for i,v in pairs(TableName) do", where "TableName" in this case would be "MyAddonDataBase", "i" is going to represent each key in the database, while "v" will represent each value. so that "v.Name" will always be the name of the player in "MyAddonDataBase[i].Name"

     for i,v in pairs(MyAddonDataBase) do 
     print("Name:", v.Name, "Level", v.Level, v.Race, v.Class)
     end
    

    the output may be: George 80 Tauren Shaman Chris 79 Troll Warrior Bobby 80 Gnome Mage

    I hope this helps you, check out WoWWiki for more help or email me anytime: mirrikat45@gmail.com

    No offense, but all of this and more is covered in the book that is the purpose of this website. If you could please explain why that isn't sufficient, I would appreciate it.

    Haha, because I havn't read the book =P. But I'm going to order it for reference.

    Fair enough, hence my asking =)

    Thanks for your answer Mirrikat45, I think you have cleared up some of the conceptual roadblocks for me, I'll start working with the concepts you have presented. :)

    And to answer your question jnwhiteh...

    Is there any particular reason that the chapters that detail SavedVariables in our book not sufficient for you?

    I didn't actually get to those chapters because I felt the itch to start working on my own AddOns by Chapter 16-17. Also, when I started looking for some help from the book in regards to databases, I couldn't find anything listed in the index for such. It never dawned on me that "Saved Variables" would cover such concepts. In retrospect, SavedVariables does make sense semantically, but I've never heard that term used to describe database like concepts during any of my CS education. Perhaps future editions could list "Databases... see SavedVariables"?

    Thanks for your help guys, though I may be back with more questions. ;)

    -z

    Unfortunately we have little to no control over the index, but I'll see what I can do. To be fair, it's not a "database" in any terms either.. I don't know that that would be incredibly useful to anyone. Having a title that says "Saving data across sessions" should be sufficient, but I also believe something like that existed already. If not, I'll try to add it.

    Now you have me confused... "Saving data across sessions" is exactly what I want to do. I would like to build a table containing the character names, class, level, etc, so that it can be later accessed for display and/or modification. In a general sense, wouldn't you call that a "database"? I don't really know what else to call it. Perhaps I'm just not understanding a technical difference?

    So now I'm able to sit down with my copy of the book and I'm trying to look up "SavedVariables" and "Saving data across sessions", but none of the chapter titles stand out at me as covering this topic. I thought that maybe it might be covered in the variable section of "Exploring Lua Basics" entitled "Understanding Values and Variables" or possibly under "Working with Tables" but I don't see anything. What page should I go to for this concept?

    Thanks for your help,

    -z

    The SavedVariables directive is in Chapter 9, but it doesn't look like any of the other chapters really cover it. I'll look further, but we'll definitely make sure the next edition of the book covers it in a bit more detail.

    wow my post just got wiped out somehow...

    anyway, I just was saying that I understand now and thanks for the help.

    -z