View Single Post
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