Thread Tools Display Modes
11-25-23, 08:31 PM   #1
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
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.
  Reply With Quote
11-26-23, 09:39 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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")
But you're accessing it in UpdateLastLogin() as if it was created as a sub-key of frame

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:
  1. -- LastLoginTracker.lua
  2.  
  3. -- Function to initialize or load saved last login time
  4. local function InitializeLastLogin()
  5.     if not LastLoginTrackerDB then
  6.         LastLoginTrackerDB = {}
  7.     end
  8.  
  9.     if not LastLoginTrackerDB.lastLoginTime then
  10.         LastLoginTrackerDB.lastLoginTime = time()
  11.     end
  12. end
  13.  
  14. -- Function to update and display last login time
  15. local function UpdateLastLogin(self)
  16.     LastLoginTrackerDB.lastLoginTime = time()
  17.     local formattedTime = date("%H:%M:%S", LastLoginTrackerDB.lastLoginTime)
  18.     print("Last Login: " .. formattedTime)
  19.     self.LastLoginTrackerTextLabel:SetText("Last Login: " .. formattedTime)
  20. end
  21.  
  22. -- Create a simple form with a button
  23. local frame = CreateFrame("Frame", "LastLoginTrackerFrame", UIParent, "BasicFrameTemplate")
  24. frame:SetSize(250, 100)
  25. frame:SetPoint("CENTER")
  26. frame:Hide()
  27. frame:RegisterEvent("PLAYER_LOGIN")
  28.  
  29. frame.title = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  30. frame.title:SetPoint("CENTER", frame.TitleBg, "CENTER", 0, 0)
  31. frame.title:SetText("Last Login Tracker")
  32.  
  33. -- Create a button to update last login time
  34. frame.LastLoginTrackerButton = CreateFrame("Button", nil, frame, "GameMenuButtonTemplate")
  35. frame.LastLoginTrackerButton:SetSize(200, 25)
  36. frame.LastLoginTrackerButton:SetPoint("CENTER", frame, "CENTER", 0, 0)
  37. frame.LastLoginTrackerButton:SetText("Update Last Login")
  38. frame.LastLoginTrackerButton:SetNormalFontObject("GameFontNormal")
  39.  
  40. -- Create a text label to display last login time
  41. frame.LastLoginTrackerTextLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  42. frame.LastLoginTrackerTextLabel:SetPoint("CENTER", frame, "CENTER", 0, -20)
  43. frame.LastLoginTrackerTextLabel:SetText("Last Login: Not tracked")
  44.  
  45. -- Set up event handling for the button
  46. frame.LastLoginTrackerButton:SetScript("OnClick", function(self, button, down)
  47.     UpdateLastLogin(self:GetParent())
  48. end)
  49.  
  50. -- Event handler for PLAYER_LOGIN
  51. frame:SetScript("OnEvent", function(self, event, arg1)
  52.     if event == "PLAYER_LOGIN" then
  53.         InitializeLastLogin()
  54.     end
  55. end)
  56.  
  57. -- Slash command to show/hide the form
  58. SLASH_LASTLOGINTRACKER1 = "/lastlogin"
  59. SlashCmdList["LASTLOGINTRACKER"] = function()
  60.     frame:SetShown(not frame:IsShown())
  61. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-26-23 at 09:43 AM.
  Reply With Quote
11-26-23, 10:20 AM   #3
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
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.
  Reply With Quote
11-26-23, 10:35 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-26-23 at 10:38 AM.
  Reply With Quote
11-26-23, 11:03 AM   #5
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
Thanks again. Your explanations are very clear.
  Reply With Quote
11-27-23, 02:44 AM   #6
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
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
The .toc versions for the games are, as of this writing:
  • 10200 for retail
  • 30403 for Wrath
  • 10500 for Classic Era and hardcore
  Reply With Quote
11-27-23, 01:33 PM   #7
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
Thanks

Thanks for the reminder!
I should have updated the interface # before I posted it.
  Reply With Quote
11-27-23, 04:53 PM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,325
Originally Posted by myrroddin View Post
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
The .toc versions for the games are, as of this writing:
  • 10200 for retail
  • 30403 for Wrath
  • 10500 for Classic Era and hardcore
The numbers for Retail and ClassicEra are incorrect. A good rule of thumb is to take the version number and put it in a ABBCC format. For example, ClassicEra is version 1.14.4 right now, so the Interface version should be 11404. Retail is 10.1.7, so this would be Interface 100107.

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.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 11-27-23 at 04:56 PM.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Why doesnt the font string update?

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