odd text size glitch

when I reach zero health, the “drawLine” is moved on the x axis. the drawLine’s x position is based off of the health text’s width… here’s the code:



surface.CreateFont("CustomHUDFont", {
	font = "Arial",
	size = 13,
	weight = 500
})

local function drawOutlinedRect(x, y, w, h, color1, color2)
	surface.SetDrawColor(color1)
	surface.DrawRect(x + 1, y + 1, w - 2, h - 2)
	surface.SetDrawColor(color2)
	surface.DrawOutlinedRect(x, y, w, h)
end

local function drawLine(startx, starty, endx, endy, color)
	surface.SetDrawColor(color)
	surface.DrawLine(startx, starty, endx, endy)
end

local function getTextSizeW(text)
	local textWidth, textHeight = surface.GetTextSize(text)

	surface.SetFont("CustomHUDFont")
	return textWidth
end

local function getTextSizeH(text)
	local textWidth, textHeight = surface.GetTextSize(text)

	surface.SetFont("CustomHUDFont")
	return textHeight
end

local function drawText(text, x, y, color)
	surface.SetFont("CustomHUDFont")
	surface.SetTextColor(color.r, color.g, color.b, color.a)
	surface.SetTextPos(x, y)
	surface.DrawText(text)
end

local function hudPaint()
	local Health = LocalPlayer():Health()
	local HealthText = "Health: " .. Health
	local HealthTextSizeW = getTextSizeW(HealthText)
	local HealthTextSizeH = getTextSizeH(HealthText)
	local HealthTextColor
	local PrimaryColor = Color(50, 50, 50, 250)
	local SecondaryColor = Color(175, 175, 175, 255)

	-- Base
	drawOutlinedRect(0, 0, ScrW(), 20, PrimaryColor, SecondaryColor)

	-- Health
	if (Health < 0) then
		Health = 0
	end

	if (Health > 49) then
		HealthTextColor = Color(0, 255, 0, 255)
	elseif (Health > 24) then
		HealthTextColor = Color(255, 255, 0, 255)
	elseif (Health >= 0) then
		HealthTextColor = Color(255, 0, 0, 255)
	end

	drawLine(HealthTextSizeW + 4, 1, HealthTextSizeW + 4, 19, SecondaryColor)
	drawText("Health: ", 2, 3.5, Color(255, 255, 255, 255))
	drawText(Health, getTextSizeW("Health: "), 3.5, HealthTextColor)
end


here’s a video of it fucking up in action (you can see where it’s fucking up in the upper left hand side):
[video]https://youtu.be/6dCmDyiYNg4[/video]

ps, sorry for the music… I was listening to it and I didn’t feel like editing the video

You need to set the font before you get the text size.

The reason the line is going so far to the right is because you’re setting Health to 0 after you calculate the width, when the health can actually be -50 or something less than 0. But since you’re setting it to 0 after you do the width calculations, the health is shown as 0, but is in reality -80 or something.

So I’m guessing I’d do



local function getTextSizeW(text)
	surface.SetFont("CustomHUDFont")

	local textWidth, textHeight = surface.GetTextSize(text)

	return textWidth
end

local function getTextSizeH(text)
	surface.SetFont("CustomHUDFont")

	local textWidth, textHeight = surface.GetTextSize(text)

	return textHeight
end