Thread Tools Display Modes
05-21-21, 04:51 PM   #1
nibsrs
A Defias Bandit
Join Date: May 2021
Posts: 3
Exclamation Create Text Frame with Player's Health

Hey guys, I have zero knowledge in LUA scripting, however I am trying to know a bit more about it. I want to create a simple text on screen which will show the current player's health.
This is what I've done so far, but got stuck:

Code:
local f = CreateFrame("Frame", nil, UIParent)
	f:SetWidth(1)
	f:SetHeight(1)
	f:SetAlpha(1)
	f:SetPoint("CENTER", 0, 0)
	f.text = f:CreateFontString(nil,"ARTWORK")
	f.text:SetFont("Fonts\\FRIZQT__.TTF", 14, "OUTLINE")
	f.text:SetPoint("CENTER", 0, 0);
From here I don't know where to go, can someone help?
  Reply With Quote
05-21-21, 05:56 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Something simple:

Lua Code:
  1. local function UpdateHealth(self) -- Update the health text
  2.     local health = UnitHealth("player")
  3.     self.Text:SetText(health .. '/' .. self.healthMax)
  4. end
  5. local function UpdateHealthMax(self) -- Update max. health value
  6.     self.healthMax = UnitHealthMax("player")
  7.     UpdateHealth(self)
  8. end
  9.  
  10. local f = CreateFrame("Frame", "nibsrsHealthText", UIParent)
  11. f:SetSize(5, 4)
  12. f:SetPoint("CENTER")
  13. f.Text = f:CreateFontString()
  14. f.Text:SetFontObject(GameFontNormal)
  15. f.Text:SetPoint("CENTER")
  16. f.Text:SetJustifyH("CENTER")
  17. f.Text:SetJustifyV("CENTER")
  18.  
  19. f:SetScript("OnEvent", function(self, event, ...) -- when registered events fire.
  20.     if event == "UNIT_HEALTH" then -- Fired when health changes
  21.         UpdateHealth(self)
  22.     elseif event == "UNIT_MAXHEALTH" then -- Fired when max. health changes
  23.         UpdateHealthMax(self)
  24.     end
  25. end)
  26. f:RegisterEvent("UNIT_HEALTH") -- register the events to be used (when health changes happen)
  27. f:RegisterEvent("UNIT_MAXHEALTH")
  28. UpdateHealthMax(f) -- initialise the health text
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 05-21-21 at 06:00 PM.
  Reply With Quote
05-21-21, 06:07 PM   #3
nibsrs
A Defias Bandit
Join Date: May 2021
Posts: 3
Originally Posted by Fizzlemizz View Post
Something simple:

Lua Code:
  1. local function UpdateHealth(self) -- Update the health text
  2.     local health = UnitHealth("player")
  3.     self.Text:SetText(health .. '/' .. self.healthMax)
  4. end
  5. local function UpdateHealthMax(self) -- Update max. health value
  6.     self.healthMax = UnitHealthMax("player")
  7.     UpdateHealth(self)
  8. end
  9.  
  10. local f = CreateFrame("Frame", "nibsrsHealthText", UIParent)
  11. f:SetSize(5, 4)
  12. f:SetPoint("CENTER")
  13. f.Text = f:CreateFontString()
  14. f.Text:SetFontObject(GameFontNormal)
  15. f.Text:SetPoint("CENTER")
  16. f.Text:SetJustifyH("CENTER")
  17. f.Text:SetJustifyV("CENTER")
  18.  
  19. f:SetScript("OnEvent", function(self, event, ...) -- when registered events fire.
  20.     if event == "UNIT_HEALTH" then -- Fired when health changes
  21.         UpdateHealth(self)
  22.     elseif event == "UNIT_MAXHEALTH" then -- Fired when max. health changes
  23.         UpdateHealthMax(self)
  24.     end
  25. end)
  26. f:RegisterEvent("UNIT_HEALTH") -- register the events to be used (when health changes happen)
  27. f:RegisterEvent("UNIT_MAXHEALTH")
  28. UpdateHealthMax(f) -- initialise the health text
Hey, thank you so much! However, the text isn't showing up Any clue why?
  Reply With Quote
05-21-21, 06:29 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Originally Posted by nibsrs View Post
Hey, thank you so much! However, the text isn't showing up Any clue why?
You're dead?

It should be gold text in the middle of your screen even if it's just 0/0.

If you're comming from an initial login rather than a /reload you can change the inital text setting to the PLAYER_LOGIN event.

You can change the "UNIT_HEALTH" to "UNIT_HEALTH_FREQUENT" if your doing this in Classic (leave it as is for BCC).
Lua Code:
  1. local function UpdateHealth(self) -- Update the health text
  2.     local health = UnitHealth("player")
  3.     self.Text:SetText(health .. '/' .. self.healthMax)
  4. end
  5. local function UpdateHealthMax(self) -- Update min./max. health values
  6.     self.healthMax = UnitHealthMax("player")
  7.     UpdateHealth(self)
  8. end
  9.  
  10. local f = CreateFrame("Frame", "nibsrsHealthText", UIParent)
  11. f:SetSize(5, 4)
  12. f:SetPoint("CENTER")
  13. f.Text = f:CreateFontString()
  14. f.Text:SetFontObject(GameFontNormal)
  15. f.Text:SetPoint("CENTER")
  16. f.Text:SetJustifyH("CENTER")
  17. f.Text:SetJustifyV("CENTER")
  18.  
  19. f:SetScript("OnEvent", function(self, event, ...)
  20.     if event == "UNIT_HEALTH" then -- Fired when health changes
  21.         UpdateHealth(self)
  22.     elseif event == "PLAYER_LOGIN" or event == "UNIT_MAXHEALTH" then -- Fired when max. health changes
  23.         UpdateHealthMax(self)
  24.     end
  25. end)
  26. f:RegisterEvent("UNIT_HEALTH") -- register the events to be used
  27. f:RegisterEvent("UNIT_MAXHEALTH")
  28. f:RegisterEvent("PLAYER_LOGIN") -- Fires only once before the "PLAYER_ENTERING_WORLD" event.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 05-21-21 at 06:40 PM.
  Reply With Quote
05-21-21, 06:48 PM   #5
nibsrs
A Defias Bandit
Join Date: May 2021
Posts: 3
Originally Posted by Fizzlemizz View Post
You're dead?

It should be gold text in the middle of your screen even if it's just 0/0.

If you're comming from an initial login rather than a /reload you can change the inital text setting to the PLAYER_LOGIN event.

You can change the "UNIT_HEALTH" to "UNIT_HEALTH_FREQUENT" if your doing this in Classic (leave it as is for BCC).
Lua Code:
  1. local function UpdateHealth(self) -- Update the health text
  2.     local health = UnitHealth("player")
  3.     self.Text:SetText(health .. '/' .. self.healthMax)
  4. end
  5. local function UpdateHealthMax(self) -- Update min./max. health values
  6.     self.healthMax = UnitHealthMax("player")
  7.     UpdateHealth(self)
  8. end
  9.  
  10. local f = CreateFrame("Frame", "nibsrsHealthText", UIParent)
  11. f:SetSize(5, 4)
  12. f:SetPoint("CENTER")
  13. f.Text = f:CreateFontString()
  14. f.Text:SetFontObject(GameFontNormal)
  15. f.Text:SetPoint("CENTER")
  16. f.Text:SetJustifyH("CENTER")
  17. f.Text:SetJustifyV("CENTER")
  18.  
  19. f:SetScript("OnEvent", function(self, event, ...)
  20.     if event == "UNIT_HEALTH" then -- Fired when health changes
  21.         UpdateHealth(self)
  22.     elseif event == "PLAYER_LOGIN" or event == "UNIT_MAXHEALTH" then -- Fired when max. health changes
  23.         UpdateHealthMax(self)
  24.     end
  25. end)
  26. f:RegisterEvent("UNIT_HEALTH") -- register the events to be used
  27. f:RegisterEvent("UNIT_MAXHEALTH")
  28. f:RegisterEvent("PLAYER_LOGIN") -- Fires only once before the "PLAYER_ENTERING_WORLD" event.
Sorry, it was my mistake lol Thank you so much!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Create Text Frame with Player's Health

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