Thread Tools Display Modes
04/26/24, 12:24 PM   #1
VessVelendas
Join Date: Apr 2024
Posts: 5
savedVariables returning nil

I'm having trouble getting my saved variables to work...

In the following, "if GOLDMetrics.savedVariables" is returning nil despite happening after I create my savedVariables, resulting in "function expected instead of nil." Thing is, it's also not showing the "no saved variables" message I've put under else. I'm not sure what's going on.

Code:
GOLDMetrics = GOLDMetrics or {}
GOLDMetrics.name = "GOLDMetrics"
GOLDMetrics.variableVersion = 2

-- ---------------------------------------------------------------------------------------------------------------------------------
-- LIBRARIES
-- ---------------------------------------------------------------------------------------------------------------------------------

-- LibChatMessage
local chat = LibChatMessage.Create("GOLDMetrics", "GOLDMetrics")

-- LibAddonMenu
local LAM = LibAddonMenu2
local saveData = {}
local GOLDPanel = "GOLDMetrics"

local GOLDpanelData = {

    type = "panel",
    name = "GOLDMetrics",
	displayName = "GOLDMetrics",
    author = "VessVelendas",
	version = "1.0",
	slashCommnad = "/GOLDMetrics",
	registerforRefresh = true,
	
}

local GOLDoptionsTable = {
    [1] = {
        type = "header",
        name = "Where you started...",
        width = "full",
    },
	[2] = {
        type = "description",
        title = nil,
        text = "Date Initialized:",
        width = "half",
    },
	[3] = {
        type = "description",
        title = nil,
        text = "00/00/0000",
        width = "half",
    },
	[4] = {
        type = "description",
        title = nil,
        text = "Initial Gold:",
        width = "half",
    },
	[5] = {
        type = "description",
        title = nil,
        text = "100,000,000",
        width = "half",
    },
	[6] = {
        type = "header",
        name = "Where you are now...",
        width = "full",
    },
	[7] = {
        type = "description",
        title = nil,
        text = "Current Date:",
        width = "half",
    },
	[8] = {
        type = "description",
        title = nil,
        text = "00/00/0000",
        width = "half",
    },
	[9] = {
        type = "description",
        title = nil,
        text = "Current Gold:",
        width = "half",
    },
	[10] = {
        type = "description",
        title = nil,
        text = "100,000,000",
        width = "half",
    },
	[11] = {
        type = "description",
        title = nil,
        text = "Goal (Daily):",
        width = "half",
    },
	[12] = {
        type = "description",
        title = nil,
        text = "100,000,000",
        width = "half",
    },
	[13] = {
        type = "description",
        title = nil,
        text = "Daily Goal Fulfilled?",
        width = "half",
    },
	[14] = {
        type = "description",
        title = nil,
        text = "NO",
        width = "half",
    },
	[15] = {
        type = "description",
        title = nil,
        text = "Daily Difference:",
        width = "half",
    },
	[16] = {
        type = "description",
        title = nil,
        text = "-000",
        width = "half",
    },
	[17] = {
        type = "header",
        name = "Settings",
        width = "full",
    },
	[18] = {
        type = "description",
        title = nil,
        text = "NOTE: The formula is Current Gold + Slider Value.",
        width = "full",
    },
	[19] = {
        type = "slider",
        name = "Daily Goal",
        tooltip = "Amount to add to your current gold",
        min = 0,
        max = 1000000,
        step = 1000,
        getFunc = function() return 3 end,
        setFunc = function(value) d(value) end,
        width = "full",
        default = 500000,
    },
	[20] = {
        type = "checkbox",
        name = "Ignore Changes",
        tooltip = "Transactions that happen while this is checked won't be counted.",
        getFunc = function() return true end,
        setFunc = function(value) d(value) end,
        width = "full"
    }
}

-- ---------------------------------------------------------------------------------------------------------------------------------
-- SAVEDVARIABLES
-- ---------------------------------------------------------------------------------------------------------------------------------

-- Defaults
GOLDMetrics.Default = {
	
	FirstInit = 0,
	InitDate = 0,
	InitGold = 0,
	CurDay = 0,
	CurGold = 0,
	GoalAmount = 0,
	CurGoal = 0,
	GoalFulfilled = 0,
	Difference = 0
	
}

-- ---------------------------------------------------------------------------------------------------------------------------------
-- CODE BEGINS HERE
-- ---------------------------------------------------------------------------------------------------------------------------------

-- When Player is Loaded
function GOLDMetrics.PlayerLoaded()

	GOLDMetrics.savedVariables = ZO_SavedVars:NewAccountWide("GOLDMetricsVar", GOLDMetrics.variableVersion, nil, GOLDMetrics.Default, GetWorldName())
	chat:Print("SavedVariables created")
	
	-- LibAddonMenu
	LAM:RegisterAddonPanel("GOLDMetrics", GOLDpanelData)
	LAM:RegisterOptionControls("GOLDMetrics", GOLDoptionsTable)
	
	-- I want to update SavedVariables on load so this is commented out
	-- EVENT_MANAGER:UnregisterForEvent(GOLDMetrics.name, EVENT_ADD_ON_LOADED)
	
	GOLDMetrics.Init()
	
end

function GOLDMetrics.Init()

	-- Only if first init
	if GOLDMetrics.savedVariables then
	
		if GOLDMetrics.savedVariables.FirstInit == 0 or nil then
		
			GOLDMetrics.savedVariables.InitDate = GetDateElementsFromTimestamp(GetTimeStamp())
			GOLDMetrics.savedVariables.CurDay = GetDateElementsFromTimestamp(GetTimeStamp())
			
			-- Reset Goal Completion
			GOLDMetrics.savedVariables.GoalFulfilled = 0
			chat:Print("RESET Goal Fulfillment")
			
			-- Reset Difference
			GOLDMetrics.savedVariables.Difference = 0
			chat:Print("RESET Difference")
			
			-- Update Money
			curMoney = GetCurrentMoney()
			GOLDMetrics.savedVariables.CurGold = curMoney
			chat:Print("UPDATE Current Gold")
			
			-- Update Goal
			GOLDMetrics.savedVariables.CurGoal = GOLDMetrics.savedVariables.CurGold + GOLDMetrics.savedVariables.GoalAmount
			curGoal2 = GOLDMetrics.savedVariables.CurGoal
			chat:Print("UPDATE Daily Goal")
			
			GOLDMetrics.savedVariables.FirstInit = 1
		
			chat:Print("Initialized")
		
		end
		
		-- Check the date
		oldYear, oldMonth, oldDay = GetDateElementsFromTimestamp(GOLDMetrics.savedVariables.CurDay)
		year, month, day = GetDateElementsFromTimestamp(GetTimeStamp())
			
		if day ~= oldDay then
			
			chat:Print("Date has changed")
			
			-- Reset Goal Completion
			GOLDMetrics.savedVariables.GoalFulfilled = 0
			chat:Print("RESET Goal Fulfillment")
			
			-- Reset Difference
			GOLDMetrics.savedVariables.Difference = 0
			chat:Print("RESET Difference")
			
			-- Update Money
			curMoney = GetCurrentMoney()
			GOLDMetrics.savedVariables.CurGold = curMoney
			chat:Print("UPDATE Current Gold")
			
			-- Update Goal
			GOLDMetrics.savedVariables.CurGoal = GOLDMetrics.savedVariables.CurGold + GOLDMetrics.savedVariables.GoalAmount
			CurGoal2 = GOLDMetrics.savedVariables.CurGoal
			chat:Print("UPDATE Daily Goal")
			
		else
			
			chat:Print("Date has NOT changed")
			
			-- Update Money
			curMoney = GetCurrentMoney()
			GOLDMetrics.savedVariables.CurGold = curMoney
			chat:Print("UPDATE Current Gold")
			
		end
		
	else
	
		chat:Print("No savedVariable")
	
	end

end

-- EVENT_MANAGER:RegisterForEvent(GOLDMetrics.name, EVENT_ADD_ON_LOADED, GOLDMetrics.AddOnLoaded)
EVENT_MANAGER:RegisterForEvent(GOLDMetrics.name, EVENT_PLAYER_ACTIVATED, GOLDMetrics.PlayerLoaded)
-- EVENT_MANAGER:RegisterForEvent("GOLDMetrics", EVENT_MONEY_UPDATE, GOLDMetrics.Update)
  Reply With Quote
04/26/24, 01:27 PM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,998
Your EVENT_ADD_ON_LOADED is commented so I assume your addon never loads properly.

Use EVENT_ADD_ON_LOADED to Init once, it will fire for each addon so make sure you check for your addon''s name and after that unregister this event again!
In there register the EVENT_PLAYER_ACTIVATED event (this will fire again and again, if not unregistered, on EACH zone change with loading screen, teleport etc.).


Basically addons should always start like this, unless you need another usecase. Else your addons will init before other addons/dependencies like LibAddonMenu and LibChatMessage etc. are loaded and initialized!


Lua Code:
  1. EVENT_MANAGER:RegisterForEvent(GOLDMetrics.name, EVENT_ADD_ON_LOADED, function(eventId, addonName)
  2.    --any of the other addons currently loading?
  3.    if addonName ~=  GOLDMetrics.name then return end
  4.  
  5.    --my addon laoding
  6.    EVENT_MANAGER:UnregisterForEvent(GOLDMetrics.name, EVENT_ADD_ON_LOADED)
  7.  
  8.    --Init saved vars etc.
  9.    local settings = ZO_SavedVars:New*
  10.    --->Before EVENT_ADD_ON_LOADED the savedvars will be nil! So only assign them here, afterwards
  11.  
  12.    --Register other events
  13.    EVENT_MANAGER:RegisterForEvent(GOLDMetrics.name, EVENT_PLAYER_ACTIVATED, function(eventId, primary)
  14.     ----After chat has been initialized and player loaded into the ingame world
  15.    end)
  16. end)

Last edited by Baertram : 04/26/24 at 01:33 PM.
  Reply With Quote
04/26/24, 01:43 PM   #3
VessVelendas
Join Date: Apr 2024
Posts: 5
I haven't been able to get on addon loaded to work. When I put stuff under it, that stuff is never triggered, even if I remove the if statement I use to gate if from being triggered by other addons.
  Reply With Quote
04/26/24, 03:03 PM   #4
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,998
You got a typo in there then, or did something else wrong (filenames of lua in your txt got a typo or whatever).

Code:
if addonName ~= GOLDMetrics.name then return end
Watch out that the name in GOLDMetrics.name is compared here with your Addon's folder and txt file name in live/AddOns!
So if "GOLDMetrics" is not your folder and txt file name you need to change for the EVENT_ADD_ON_LOADED the name chekc to your folder & txt file name!!!



Lua Code:
  1. GOLDMetrics = GOLDMetrics or {}
  2. GOLDMetrics.name = "GOLDMetrics"
  3. GOLDMetrics.variableVersion = 2
  4.  
  5. -- ---------------------------------------------------------------------------------------------------------------------------------
  6. -- LIBRARIES
  7. -- ---------------------------------------------------------------------------------------------------------------------------------
  8.  
  9. -- LibChatMessage
  10. local chat = LibChatMessage.Create("GOLDMetrics", "GOLDMetrics")
  11.  
  12. -- LibAddonMenu
  13. local LAM = LibAddonMenu2
  14. local saveData = {}
  15. local GOLDPanel = "GOLDMetrics"
  16.  
  17. local GOLDpanelData = {
  18.  
  19.     type = "panel",
  20.     name = "GOLDMetrics",
  21.     displayName = "GOLDMetrics",
  22.     author = "VessVelendas",
  23.     version = "1.0",
  24.     slashCommnad = "/GOLDMetrics",
  25.     registerforRefresh = true,
  26.  
  27. }
  28.  
  29. local GOLDoptionsTable = {
  30.     [1] = {
  31.         type = "header",
  32.         name = "Where you started...",
  33.         width = "full",
  34.     },
  35.     [2] = {
  36.         type = "description",
  37.         title = nil,
  38.         text = "Date Initialized:",
  39.         width = "half",
  40.     },
  41.     [3] = {
  42.         type = "description",
  43.         title = nil,
  44.         text = "00/00/0000",
  45.         width = "half",
  46.     },
  47.     [4] = {
  48.         type = "description",
  49.         title = nil,
  50.         text = "Initial Gold:",
  51.         width = "half",
  52.     },
  53.     [5] = {
  54.         type = "description",
  55.         title = nil,
  56.         text = "100,000,000",
  57.         width = "half",
  58.     },
  59.     [6] = {
  60.         type = "header",
  61.         name = "Where you are now...",
  62.         width = "full",
  63.     },
  64.     [7] = {
  65.         type = "description",
  66.         title = nil,
  67.         text = "Current Date:",
  68.         width = "half",
  69.     },
  70.     [8] = {
  71.         type = "description",
  72.         title = nil,
  73.         text = "00/00/0000",
  74.         width = "half",
  75.     },
  76.     [9] = {
  77.         type = "description",
  78.         title = nil,
  79.         text = "Current Gold:",
  80.         width = "half",
  81.     },
  82.     [10] = {
  83.         type = "description",
  84.         title = nil,
  85.         text = "100,000,000",
  86.         width = "half",
  87.     },
  88.     [11] = {
  89.         type = "description",
  90.         title = nil,
  91.         text = "Goal (Daily):",
  92.         width = "half",
  93.     },
  94.     [12] = {
  95.         type = "description",
  96.         title = nil,
  97.         text = "100,000,000",
  98.         width = "half",
  99.     },
  100.     [13] = {
  101.         type = "description",
  102.         title = nil,
  103.         text = "Daily Goal Fulfilled?",
  104.         width = "half",
  105.     },
  106.     [14] = {
  107.         type = "description",
  108.         title = nil,
  109.         text = "NO",
  110.         width = "half",
  111.     },
  112.     [15] = {
  113.         type = "description",
  114.         title = nil,
  115.         text = "Daily Difference:",
  116.         width = "half",
  117.     },
  118.     [16] = {
  119.         type = "description",
  120.         title = nil,
  121.         text = "-000",
  122.         width = "half",
  123.     },
  124.     [17] = {
  125.         type = "header",
  126.         name = "Settings",
  127.         width = "full",
  128.     },
  129.     [18] = {
  130.         type = "description",
  131.         title = nil,
  132.         text = "NOTE: The formula is Current Gold + Slider Value.",
  133.         width = "full",
  134.     },
  135.     [19] = {
  136.         type = "slider",
  137.         name = "Daily Goal",
  138.         tooltip = "Amount to add to your current gold",
  139.         min = 0,
  140.         max = 1000000,
  141.         step = 1000,
  142.         getFunc = function() return 3 end,
  143.         setFunc = function(value) d(value) end,
  144.         width = "full",
  145.         default = 500000,
  146.     },
  147.     [20] = {
  148.         type = "checkbox",
  149.         name = "Ignore Changes",
  150.         tooltip = "Transactions that happen while this is checked won't be counted.",
  151.         getFunc = function() return true end,
  152.         setFunc = function(value) d(value) end,
  153.         width = "full"
  154.     }
  155. }
  156.  
  157. -- ---------------------------------------------------------------------------------------------------------------------------------
  158. -- SAVEDVARIABLES
  159. -- ---------------------------------------------------------------------------------------------------------------------------------
  160.  
  161. -- Defaults
  162. GOLDMetrics.Default = {
  163.  
  164.     FirstInit = 0,
  165.     InitDate = 0,
  166.     InitGold = 0,
  167.     CurDay = 0,
  168.     CurGold = 0,
  169.     GoalAmount = 0,
  170.     CurGoal = 0,
  171.     GoalFulfilled = 0,
  172.     Difference = 0
  173.  
  174. }
  175.  
  176. -- ---------------------------------------------------------------------------------------------------------------------------------
  177. -- CODE BEGINS HERE
  178. -- ---------------------------------------------------------------------------------------------------------------------------------
  179.  
  180. -- When Player is Loaded
  181. function GOLDMetrics.PlayerLoaded()
  182.  
  183.  
  184. end
  185.  
  186. function GOLDMetrics.Init()
  187.  
  188.     -- Only if first init
  189.     if GOLDMetrics.savedVariables then
  190.  
  191.         if GOLDMetrics.savedVariables.FirstInit == 0 or nil then
  192.  
  193.             GOLDMetrics.savedVariables.InitDate = GetDateElementsFromTimestamp(GetTimeStamp())
  194.             GOLDMetrics.savedVariables.CurDay = GetDateElementsFromTimestamp(GetTimeStamp())
  195.  
  196.             -- Reset Goal Completion
  197.             GOLDMetrics.savedVariables.GoalFulfilled = 0
  198.             chat:Print("RESET Goal Fulfillment")
  199.  
  200.             -- Reset Difference
  201.             GOLDMetrics.savedVariables.Difference = 0
  202.             chat:Print("RESET Difference")
  203.  
  204.             -- Update Money
  205.             curMoney = GetCurrentMoney()
  206.             GOLDMetrics.savedVariables.CurGold = curMoney
  207.             chat:Print("UPDATE Current Gold")
  208.  
  209.             -- Update Goal
  210.             GOLDMetrics.savedVariables.CurGoal = GOLDMetrics.savedVariables.CurGold + GOLDMetrics.savedVariables.GoalAmount
  211.             curGoal2 = GOLDMetrics.savedVariables.CurGoal
  212.             chat:Print("UPDATE Daily Goal")
  213.  
  214.             GOLDMetrics.savedVariables.FirstInit = 1
  215.  
  216.             chat:Print("Initialized")
  217.  
  218.         end
  219.  
  220.         -- Check the date
  221.         oldYear, oldMonth, oldDay = GetDateElementsFromTimestamp(GOLDMetrics.savedVariables.CurDay)
  222.         year, month, day = GetDateElementsFromTimestamp(GetTimeStamp())
  223.  
  224.         if day ~= oldDay then
  225.  
  226.             chat:Print("Date has changed")
  227.  
  228.             -- Reset Goal Completion
  229.             GOLDMetrics.savedVariables.GoalFulfilled = 0
  230.             chat:Print("RESET Goal Fulfillment")
  231.  
  232.             -- Reset Difference
  233.             GOLDMetrics.savedVariables.Difference = 0
  234.             chat:Print("RESET Difference")
  235.  
  236.             -- Update Money
  237.             curMoney = GetCurrentMoney()
  238.             GOLDMetrics.savedVariables.CurGold = curMoney
  239.             chat:Print("UPDATE Current Gold")
  240.  
  241.             -- Update Goal
  242.             GOLDMetrics.savedVariables.CurGoal = GOLDMetrics.savedVariables.CurGold + GOLDMetrics.savedVariables.GoalAmount
  243.             CurGoal2 = GOLDMetrics.savedVariables.CurGoal
  244.             chat:Print("UPDATE Daily Goal")
  245.  
  246.         else
  247.  
  248.             chat:Print("Date has NOT changed")
  249.  
  250.             -- Update Money
  251.             curMoney = GetCurrentMoney()
  252.             GOLDMetrics.savedVariables.CurGold = curMoney
  253.             chat:Print("UPDATE Current Gold")
  254.  
  255.         end
  256.  
  257.     end
  258.  
  259. end
  260. function GOLDMetrics.AddOnLoaded(eventId, addonName)
  261.     if addonName ~= GOLDMetrics.name then return end
  262.     EVENT_MANAGER:UnRegisterForEvent(GOLDMetrics.name, EVENT_ADD_ON_LOADED)
  263.  
  264.     GOLDMetrics.savedVariables = ZO_SavedVars:NewAccountWide("GOLDMetricsVar", GOLDMetrics.variableVersion, nil, GOLDMetrics.Default, GetWorldName())
  265.     chat:Print("SavedVariables created")
  266.  
  267.     -- LibAddonMenu
  268.     LAM:RegisterAddonPanel("GOLDMetrics", GOLDpanelData)
  269.     LAM:RegisterOptionControls("GOLDMetrics", GOLDoptionsTable)
  270.  
  271.     GOLDMetrics.Init()
  272.  
  273.     --EVENT_MANAGER:RegisterForEvent(GOLDMetrics.name, EVENT_PLAYER_ACTIVATED, GOLDMetrics.PlayerLoaded)
  274.     EVENT_MANAGER:RegisterForEvent("GOLDMetrics", EVENT_MONEY_UPDATE, GOLDMetrics.Update)
  275. end
  276.  
  277. EVENT_MANAGER:RegisterForEvent(GOLDMetrics.name, EVENT_ADD_ON_LOADED, GOLDMetrics.AddOnLoaded)

Last edited by Baertram : 04/26/24 at 03:09 PM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » savedVariables returning nil


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