Thread Tools Display Modes
01-19-10, 06:01 PM   #1
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
Question Saving Game Wide Total Gold to SavedVariables.

i am trying to build an array of toons i have on all servers and there money. i studied Titan Panels Server total gold per fraction, was closest to what i am looking for but i am trying to get all fractions all servers to one list.
so far

in toc i added to SavedVariables
Code:
## SavedVariables: ServerEarnings,GTgameArray
to exsisting .lua of guildtax i wrote
Code:
local GTgameArray = UnitName("player").." :: "..GetMoney("player");
  if (ServerEarnings == nil) then
    ServerEarnings = {};
  else
  local ServerEarnings = {};
    for index, in pairs(GTgameArray) do
      if (not GTgameArray) then 
      GTgameArray={};
    end
    table.insert(GTgameArray, index);
  end
end

hoping someone could lead me the right direction here i seem to be not understanding this method.
__________________
  Reply With Quote
01-19-10, 06:10 PM   #2
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
i am loading this during OnLoad and have tried loading after.
__________________
  Reply With Quote
01-19-10, 06:35 PM   #3
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,934
this is the same mistake I made. The table variables you associate in the toc as saved variables cannot be local variables in the lua file but instead classed as global.

One of the first lines in any of my WTF data file addons is the following:

MySavedVars = MySavedVars or {}

This way it will use whatever has been previously stored or create a new table. Repeat for whatever tables you have within that. Do the same for variables so that you have a set of defaults to use if it cannot find any data values.

Then throughout the code you can use the various values to set and get as appropriate.

So try removing the local part in front of GTgameArray and similarly off of the others.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
01-19-10, 06:38 PM   #4
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Originally Posted by Drshow View Post
i am loading this during OnLoad and have tried loading after.
Also, saved variables aren't loaded until ADDON_LOADED fires with an arg1 of your addon.
  Reply With Quote
01-21-10, 04:39 PM   #5
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
so far

so far no luck
__________________
  Reply With Quote
01-21-10, 05:37 PM   #6
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
this i see is gonna be a feat

well i can see this is going to be a feat.
  • Im going to create a table with 50 string slots "max number of characters one is allowed to create".
  • Each character will be table.insert into the next available number.
  • Before insert i will have to filter to see if added, and if added, then only the money will be updated. str.replace i suppose lol.
ill post again when i have something working.
__________________
  Reply With Quote
01-21-10, 05:47 PM   #7
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Lua can do string-keyed tables:

Code:
--assuming your list of names is "db"

local name = UnitName("player")
db[name] = (db[name] or 0) + x
You might want to do "player name - realm name" also, in case someone has the same name on two servers.

Last edited by Akryn : 01-21-10 at 05:50 PM.
  Reply With Quote
01-21-10, 06:02 PM   #8
Katae
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Jun 2007
Posts: 208
It's really not as difficult as you may perceive.

MyGold.toc
Code:
## Interface: 30300
## SavedVariables: MYGVARS
MyGold.lua
MyGold.lua
Code:
local realm, char = GetRealmName(), UnitName'player'
local f = CreateFrame'frame'
f:RegisterEvent'PLAYER_LOGIN'
f:RegisterEvent'ADDON_LOADED'
f:RegisterEvent'PLAYER_MONEY'
f:SetScript("OnEvent", function(_, event, addon)
    if event == "ADDON_LOADED" and addon == "MyGold" then
        if not MYGVARS then MYGVARS = {} end
        if not MYGVARS[realm] then MYGVARS[realm] = {} end
    elseif event ~= "ADDON_LOADED" then
        MYGVARS[realm][char] = GetMoney()
    end
end)
That's the gist of it, make sure your saved vars table name is unique because it's global.

Result:
Code:
MYGVARS = {
     ["Realm"] = {
         ["ToonA"] = 5834933,
         ["ToonB"] = 939282,
     }
}

Last edited by Katae : 01-22-10 at 02:28 PM.
  Reply With Quote
01-21-10, 07:49 PM   #9
Recluse
A Cliff Giant
 
Recluse's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 70
I have an addon which I made for myself that does this exact thing (also time played and and level statistics). It's quite alpha (needs optimizing), but works perfectly.

Link: http://pastey.net/131781
__________________
We'd be together, but only diamonds last forever...
  Reply With Quote
01-22-10, 02:19 PM   #10
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
ya im thinking i really comlicated it.

Originally Posted by Katae View Post
It's really not as difficult as you may perceive.

im trying yours now and oh my god, so much simpler that what i was trying to do.
__________________
  Reply With Quote
01-23-10, 01:02 AM   #11
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
OK so far so good one last request. I was like to add all the values of ["Money"], "per character", together to make ["ALLmoney"]

SavedVariable in .lua
Code:
GTgameArray = {
 ["Dragonblight"] = {
  ["Alliance"] = {
   ["Drshamy"] = {
    ["Money"] = "325265963",
    ["Assets"] = "19081105",
    ["Copper"] = "63|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t",
    ["Copper1"] = "63",
    ["Silver1"] = "59",
    ["Gold1"] = "32526",
    ["Gold"] = "32526|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t",
    ["Silver"] = "59|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t",
   },
   ["Foxyknight"] = {
    ["Silver1"] = "10",
    ["Assets"] = "273321",
    ["Silver"] = "10|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t",
    ["Copper1"] = "20",
    ["Money"] = "384991020",
    ["Gold1"] = "38499",
    ["Gold"] = "38499|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t",
    ["Copper"] = "20|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t",
   },
   ["Drshadows"] = {
    ["Silver"] = "5|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t",
    ["Assets"] = "10466841",
    ["Copper"] = "12|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t",
    ["Copper1"] = "12",
    ["Money"] = "955110512",
    ["Gold"] = "95511|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t",
    ["Gold1"] = "95511",
    ["Silver1"] = "5",
   },
  },
 },
}
__________________
  Reply With Quote
01-23-10, 03:25 AM   #12
Recluse
A Cliff Giant
 
Recluse's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 70
Originally Posted by Drshow View Post
OK so far so good one last request. I was like to add all the values of ["Money"], "per character", together to make ["ALLmoney"] SavedVariable in .lua
Well, I'm a bit confused why you store your integer values as strings, and even more confused why store the gsc both separately and as strings with icon references - this could be done on the code side, when it needs to render the data for display. BUT regardless, it's what ya chose to do. Not knocking it, just really baffled is all. To each their own.

lua Code:
  1. -- inside PLAYER_MONEY handler, after updating your table with current character's money.
  2. local total_money = 0
  3. for key, realm in pairs(GTgameArray) do
  4.     -- Because ALLgold will be at same table level as realm names with your current table layout.
  5.     if key ~= "ALLgold" then
  6.         for _, faction in pairs(realm) do
  7.             for _, character in pairs(faction) do
  8.                 -- Because you have strings instead of integers, convert to numer here.
  9.                 total_money = total_money + tonumber(character["Money"])
  10.             end
  11.         end
  12.     end
  13. end
  14. GTgameArray["ALLgold"] = total_money

Something like this, I imagine you are trying to do?
__________________
We'd be together, but only diamonds last forever...
  Reply With Quote
01-27-10, 01:19 AM   #13
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
Ok here it is, need alil crit.

Its not pretty by any means, i ran into 2 problems.
1 having to removing text string formating. In doing so i created another problem i over looked for a short time. Some times the text strings only have one digit EG. 2 gold 2 silver 2 copper.

i removed the formating and got
222 when reading format i got 2 silver 22 copper lol.

so as you commented earlier, is why i did the way i did. i had to first cache string, filter for each denomination. do alil math to get
20202 or 2 gold 2 silver 2 copper. I know there has to be a more effictient way to do this, i just didnt see it

Code:
function GTpars*********()
  if (gameArrayGold == nil) then
  gameArrayGold = {};
  end
  -- NOTICE This function is copyright 2010 - 2017 Shawn E. Carter, [email protected]
  -- GETS ALL MONEY AND REMOVES FORMATING.
  -- 95511|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t 
  -- 5|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t 
  -- 12|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t
  -- 95511|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t 5|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t 12|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t
  -- =========================================
  local TOTAL_GOLD_ACQUIRED = "328"
  local txt = GetStatistic(TOTAL_GOLD_ACQUIRED);
  -- =========================================
  local iscopper = txt:match("%d+|TInterface\\MoneyFrame\\UI%-CopperIcon:0:0:2:0|t");
  if (iscopper == nil or iscopper == "") then
  iscopper = "0";
  end
  local issilver = txt:match("%d+|TInterface\\MoneyFrame\\UI%-SilverIcon:0:0:2:0|t");
  if (issilver == nil or issilver == "") then
  issilver = "0";
  end
  local isgold = txt:match("%d+|TInterface\\MoneyFrame\\UI%-GoldIcon:0:0:2:0|t");
  if (isgold == nil or isgold == "") then
  isgold = "0";
  end
  local iscopper1 = iscopper:match("%d+");
  if (iscopper1 == nil or iscopper1 == "") then
  iscopper1 = "0";
  end
  local issilver1 = issilver:match("%d+");
  if (issilver1 == nil or issilver1 == "") then
  issilver1 = "0";
  end
  local isgold1 = isgold:match("%d+");
  if (isgold1 == nil or isgold1 == "") then
  isgold1 = "0";
  end
  local DEmoneyLife = iscopper1 + (issilver1*100) + (isgold1*10000);
  local charmoney = "Assets"
  local charlifemoney = "Money"
  local charspent = "Spent"
  local charlifegold = "Gold"
  local charlifesilver = "Silver"
  local charlifecopper = "Copper"
  local charlifegold1 = "Gold1"
  local charlifesilver1 = "Silver1"
  local charlifecopper1 = "Copper1"
  -- local GTspent = 0 - GetMoney("player");
  -- =========================================
  --local realm, char, = GetRealmName(), UnitName'player';
  local realm = GetCVar("realmName");
  local curchar = UnitName("player");
  local faction = UnitFactionGroup("Player");
  local stackGT = GetCVar("realmName").."::"..UnitFactionGroup("Player").."\\"..UnitName("player");
  -- =========================================
  if (gameArrayGold[stackGT] == nil) then
  gameArrayGold[stackGT] = GetMoney("player");
  end
    
  if (GTgameArray == nil) then
  GTgameArray = {};
  end
  if (GTgameArray[realm] == nil) then 
  GTgameArray[realm] = {};
  end
  if (GTgameArray[realm][faction] == nil) then
  GTgameArray[realm][faction] = {};
  end
  if (GTgameArray[realm][faction][curchar] == nil) then
  GTgameArray[realm][faction][curchar] = {};
  end
  if (GTgameArray[realm][faction][curchar][charmoney] == nil) then
  GTgameArray[realm][faction][curchar][charmoney] = GetMoney("player").."";
  else
  GTgameArray[realm][faction][curchar][charmoney] = GetMoney("player").."";
  end
  -- if (GTgameArray["ALLassets"] == nil) then
  -- GTgameArray["ALLassets"] = 0
  -- end
  -- ========================================= copper copper1
  if (GTgameArray[realm][faction][curchar][charlifecopper] == nil) then
  GTgameArray[realm][faction][curchar][charlifecopper] = iscopper.."";
  else
  GTgameArray[realm][faction][curchar][charlifecopper] = iscopper.."";
  end
   if (GTgameArray[realm][faction][curchar][charlifecopper1] == nil) then
   
   GTgameArray[realm][faction][curchar][charlifecopper1] = iscopper1.."";
   else
   GTgameArray[realm][faction][curchar][charlifecopper1] = iscopper1.."";
   end
  -- =========================================
  if (GTgameArray[realm][faction][curchar][charlifesilver] == nil) then
  GTgameArray[realm][faction][curchar][charlifesilver] = issilver.."";
  else
  GTgameArray[realm][faction][curchar][charlifesilver] = issilver.."";
  end
   if (GTgameArray[realm][faction][curchar][charlifesilver1] == nil) then
   GTgameArray[realm][faction][curchar][charlifesilver1] = issilver1.."";
   else
   GTgameArray[realm][faction][curchar][charlifesilver1] = issilver1.."";
   end
  -- =========================================
  if (GTgameArray[realm][faction][curchar][charlifegold] == nil) then
  GTgameArray[realm][faction][curchar][charlifegold] = isgold.."";
  else
  GTgameArray[realm][faction][curchar][charlifegold] = isgold.."";
  end
   if (GTgameArray[realm][faction][curchar][charlifegold1] == nil) then
   GTgameArray[realm][faction][curchar][charlifegold1] = isgold1.."";
   else
   GTgameArray[realm][faction][curchar][charlifegold1] = isgold1.."";
   end
 -- =========================================
   -- if (GTgameArray[realm][faction][curchar][charspent] == nil) then
   -- GTgameArray[realm][faction][curchar][charspent] = GTspent.."";
   -- else
   -- GTgameArray[realm][faction][curchar][charspent] = GTspent.."";
   -- end
   -- =========================================
  if (GTgameArray[realm][faction][curchar][charlifemoney] == nil) then
  GTgameArray[realm][faction][curchar][charlifemoney] = DEmoneyLife.."";
  else
  GTgameArray[realm][faction][curchar][charlifemoney] = DEmoneyLife.."";
  end
 local total_money = 0
 if (total_money == nil) then
 total_money = 0
 end
 local total_Assets = 0
 if (total_Assets == nil) then
 total_Assets = 0
 end
 if (GTgameArray["ALLgold"] == nil) then
 GTgameArray["ALLgold"] = 0;
 end
 if (GTgameArray["ALLassets"] == nil) then
 GTgameArray["ALLassets"] = 0;
 end
 for key, realm in pairs(GTgameArray) do
  -- Because ALLgold will be at same table level as realm names with your current table layout.
  if key ~= "ALLgold" then
   for _, faction in pairs(realm) do
    for _, character in pairs(faction) do
     -- Because you have strings instead of integers, convert to numer here.
     total_money = total_money + tonumber(character["Money"]);
     GTgameArray["ALLgold"] = total_money;
    end
   end
   end
  if key ~= "ALLassets" then
   for _, faction in pairs(realm) do
    for _, character in pairs(faction) do
     -- Because you have strings instead of integers, convert to numer here.
     total_Assets = total_Assets + tonumber(character["Assets"]);
     GTgameArray["ALLassets"] = total_Assets;
    end
   end
  end
 end
 convertedGameGold3 = GetCoinTextureString(total_money, " ");
 GGAquired:SetText(""..convertedGameGold3.."");
end
__________________
  Reply With Quote
01-27-10, 01:23 AM   #14
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
and ty again for the snippet :)

Originally Posted by recluse View Post
Well, I'm a bit confused why you store your integer values as strings, and even more confused why store the gsc both separately and as strings with icon references - this could be done on the code side, when it needs to render the data for display. BUT regardless, it's what ya chose to do. Not knocking it, just really baffled is all. To each their own.
Something like this, I imagine you are trying to do?

n ty for snippet by the way work perfect. and its scalable.
__________________
  Reply With Quote
01-27-10, 01:37 AM   #15
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
This is a sample of the string i had to filter or match for each. In browsing do you see a better way todo. "and as per Character Icons ? im going to have to say oops lol, i really didnt know. It was the first naming scheme off the top of my head.
Code:
95511|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t 5|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t 12|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t
__________________
  Reply With Quote
01-27-10, 01:40 AM   #16
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
Originally Posted by Drshow View Post
This is a sample of the string i had to filter or match for each. In browsing do you see a better way todo. "and as per Character Icons ? im going to have to say oops lol, i really didnt know. It was the first naming scheme off the top of my head.
Code:
95511|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t 5|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t 12|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t


Or did you mean why am i caching them to savedvar? if so i was pulling all local but i seemed to having intermitten nil errors, like creating a new character ect. I had asked in forums if there was a way to avoid n most replys where to cache it n filter n pull n set from cache not local. Would this be wrong ?
__________________
  Reply With Quote
01-27-10, 04:45 PM   #17
Drshow
A Cyclonian
 
Drshow's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 49
Mornign everyone who its morning for, bah for everyone else,
Code:
L O L
__________________
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Saving Game Wide Total Gold to SavedVariables.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off