Thread Tools Display Modes
06-24-17, 04:42 AM   #1
Asalas77
A Murloc Raider
Join Date: May 2017
Posts: 9
SavedVariables table with unknown keys

Hey I'm trying to write a simple addon that would count how many times a player casted Nourish. I want to store data in a table indexed by played names, but the table is not being saved to SavedVariables.

I defined a SavedVariable in .toc file:

Code:
## SavedVariables: NourishCounter
And here's my addon:

Lua Code:
  1. local frame = CreateFrame("FRAME", "AddonFrame");
  2. frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
  3. frame:RegisterEvent("ADDON_LOADED");
  4. local function eventHandler(self, event, ...)
  5.      if event == "ADDON_LOADED" and arg1 == "NourishAddon" then
  6.         if not NourishCount then NourishCount = {} end
  7.     end
  8.  
  9.     args = {...};
  10.     if args[2] == "SPELL_HEAL" then
  11.         if(args[10] == "Nourish") then
  12.             caster = args[4];
  13.             if NourishCount[caster] then
  14.                 NourishCount[caster] = NourishCount[caster] + 1;
  15.             else
  16.                 NourishCount[caster] = 1;
  17.             end
  18.             print(caster .. " count: " .. NourishCount[caster]);
  19.         end
  20.     end
  21. end
  22. frame:SetScript("OnEvent", eventHandler);

Within one login period the counter works fine, but the NourishCounter in saved vars file is always nil. I tried using a regular number variable for just my own casts and it is saved correctly. I believe the problem is that the table keys are not defined when creating the table for the first time, but I don't know how to work around that.
  Reply With Quote
06-24-17, 04:47 AM   #2
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Your SV name is 'NourishCounter', not 'NourishCount'

Lua Code:
  1. local playerName = UnitName("player");
  2.  
  3. local frame = CreateFrame("FRAME", "AddonFrame");
  4. frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
  5. frame:RegisterEvent("ADDON_LOADED");
  6.  
  7. local function eventHandler(self, event, ...)
  8.     if event == "ADDON_LOADED" and ... == "NourishAddon" then
  9.         if not NourishCounter then NourishCounter = {} end
  10.  
  11.         self:UnregisterEvent("ADDON_LOADED");
  12.     elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
  13.         local _, subEvent, _, _, sourceName, _, _, _, _, _, _, _, spellName = ...;
  14.  
  15.         if subEvent == "SPELL_HEAL" then
  16.             if sourceName == playerName and spellName == "Nourish" then
  17.                 NourishCounter[sourceName] = NourishCounter[sourceName] and NourishCounter[sourceName] + 1 or 1;
  18.             end
  19.         end
  20.  
  21.         print(sourceName .. " count: " .. NourishCounter[sourceName]);
  22.     end
  23. end
  24.  
  25. frame:SetScript("OnEvent", eventHandler);

I haven't tested it, but guess it should work fine.

Last edited by Layback_ : 06-24-17 at 05:14 AM.
  Reply With Quote
06-24-17, 05:05 AM   #3
Asalas77
A Murloc Raider
Join Date: May 2017
Posts: 9
Oh wow, how did I miss that :P

Thank you.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » SavedVariables table with unknown keys

Thread Tools
Display Modes

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