Why doesnt the font string update?
This is my .toc file
## Title: LastLoginTracker ## Version: 1.0 ## Interface: 90005 ## Author: ## SavedVariablesPerCharacter: LastLoginTrackerDB LastLoginTracker.lua and here is the .lua file -- LastLoginTracker.lua local frame = CreateFrame("Frame") frame:RegisterEvent("PLAYER_LOGIN") -- Function to initialize or load saved last login time local function InitializeLastLogin() if not LastLoginTrackerDB then LastLoginTrackerDB = {} end if not LastLoginTrackerDB.lastLoginTime then LastLoginTrackerDB.lastLoginTime = time() end end -- Function to update and display last login time local function UpdateLastLogin() LastLoginTrackerDB.lastLoginTime = time() local formattedTime = date("%H:%M:%S", LastLoginTrackerDB.lastLoginTime) print("Last Login: " .. formattedTime) frame.LastLoginTrackerTextLabel:SetText("Last Login: " .. formattedTime) end -- Create a simple form with a button local frame = CreateFrame("Frame", "LastLoginTrackerFrame", UIParent, "BasicFrameTemplate") frame:SetSize(250, 100) frame:SetPoint("CENTER") frame:Hide() frame.title = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight") frame.title:SetPoint("CENTER", frame.TitleBg, "CENTER", 0, 0) frame.title:SetText("Last Login Tracker") -- Create a button to update last login time local LastLoginTrackerButton = CreateFrame("Button", nil, frame, "GameMenuButtonTemplate") LastLoginTrackerButton:SetSize(200, 25) LastLoginTrackerButton:SetPoint("CENTER", frame, "CENTER", 0, 0) LastLoginTrackerButton:SetText("Update Last Login") LastLoginTrackerButton:SetNormalFontObject("GameFontNormal") -- Create a text label to display last login time LastLoginTrackerTextLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight") LastLoginTrackerTextLabel:SetPoint("CENTER", frame, "CENTER", 0, -20) LastLoginTrackerTextLabel:SetText("Last Login: Not tracked") -- Set up event handling for the button LastLoginTrackerButton:SetScript("OnClick", function(self, button, down) UpdateLastLogin() end) -- Event handler for PLAYER_LOGIN frame:SetScript("OnEvent", function(self, event, arg1) if event == "PLAYER_LOGIN" then InitializeLastLogin() end end) -- Slash command to show/hide the form SLASH_LASTLOGINTRACKER1 = "/lastlogin" SlashCmdList["LASTLOGINTRACKER"] = function() if frame:IsShown() then frame:Hide() else frame:Show() end end When I click on the button the font string LastLoginTrackerTextLabel isn't updated. I'm doing something wrong but can't figure out what it is. I thought it might be related to local vs global variable scoping but I'm new enough to lua that I can't figure it out. Any help would be appreciated. |
Install BugGrabber and BugSack so you can see the errors you are getting.
You've created LastLoginTrackerTextLabel as a global variable Code:
LastLoginTrackerTextLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight") Three problems: 1. UpdateLastLogin can't access frame because it is a local variable created AFTER the function has been declared ie. it is out-of-scope 2. Once one is fixed you would need to change LastLoginTrackerTextLabel to be a sub-key of frame. 3. You're redeclaring the frame variable to create a second unnecessary frame (well, the first is unnecessary as LastLoginTrackerFrame can do event registration/handling). The code adjusted to pass self:GetParent() (The parent of the button which is frame) to the functions and use that. Also remove the first frame as the as you were registering the event on that one but setting the OnEvent script on the second, LastLoginTrackerFrame (through redeclaring the local frame variable) meaning PLAYER_LOGIN would never have fired. Lua Code:
|
Thank you so much for the help.
I have a question about the code. Does self:GetParent() return the frame? Where would I find documentation for GetParent()? Your help is very appreciated. |
Yes. Were you see self in the function arguments of a SetScript that will be a reference to the frame itself. In this case, self is the button and the :GetParent() being the buttons parent frame (LastLoginTrackerFrame), set as the third parameter of CreateFrame()
Widget API |
Thanks again. Your explanations are very clear.
|
This won't affect your code, but the game will tell you that your addon is outdated unless you update the following line in your .toc file:
Code:
## Interface: 10200
|
Thanks
Thanks for the reminder!
I should have updated the interface # before I posted it. |
Quote:
Previously, they only updated the Interface version when they made a breaking change to the API. Lately, they've been updating it every patch regardless. |
All times are GMT -6. The time now is 10:06 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI