Incorrect usage of Think()?

After coding nearly all day long today, my HUD is close to being done! However, there’s another issue.



// Right panel

local INFO = vgui.Create("DPanel", HUDF)

INFO:Dock(FILL)

function INFO:Think()
function INFO:Paint(w,h)
	
	surface.SetDrawColor(Color(0,0,0,150))
	surface.DrawRect(10,10,230,140)
	
	//Heart icon
	surface.SetDrawColor(Color(250,128,114))
	surface.SetMaterial(hearticon)
	surface.DrawTexturedRect(15,15,32,32)
	
	//Armor icon
	surface.SetDrawColor(Color(65,105,225))
	surface.SetMaterial(shieldicon)
	surface.DrawTexturedRect(15,50,32,32)

	//Health bar	
	if health > 0 then
	
	draw.RoundedBox(3,52,28,health * 1.8,5,Color(250,128,114))
	
	elseif health > 100 then
	draw.RoundedBox(3,52,28,180,5,Color(250,128,114))
	
	else
	end
	
	//Armor bar
	draw.RoundedBox(3,52,68,armor * 1.8,5,Color(65,105,225))
end
end


Basically what I am trying to do is prevent my HUD from glitching out when someone has more than 100 health.
Also, because of roundedbox, at 0 health it still shows a small drawing of only the roundings, so I’m trying to tell lua that if the health is not bigger than 0 it wont do anything.
Then I used the Think() version to check for updates in health constantly, but it does not work for some reason.

Help’d be appreciated!

Yours sincerely,
BremFM

You only need the Paint hook, it updates every frame.

For some reason it doesn’t, because as soon as I changed my original code to this it stopped updating the health bar. (only thing I added was the if elseif else statements, I added the Think function later because I thought that may be the solution.

[editline]13th December 2016[/editline]

Okay, so I removed the if statements again, aswell the Think() function, and it appears as if this causes the issue;




// Armor/Health text

local ARMORH = vgui.Create("DLabel", INFO)

ARMORH:SetFont("armhea")
ARMORH:SetPos(29,50)
ARMORH:SetSize(32,32)
ARMORH:CenterHorizontal()

function ARMORH:Think()

	ARMORH:SetPos(29,50)
	ARMORH:CenterHorizontal()
	ARMORH:SetText(armor .. "/100")
end


local HEALTHH = vgui.Create("DLabel", INFO)

HEALTHH:SetFont("armhea")
HEALTHH:SetPos(29,15)
HEALTHH:SetSize(32,32)
HEALTHH:CenterHorizontal()

function HEALTHH:Think()

	HEALTHH:SetPos(29,15)
	HEALTHH:CenterHorizontal()
	HEALTHH:SetText(health .. "/100")
end




it appears as if the Paint hook stops updating as soon as I draw something else ON TOP of it? (although the text isn’t updating either)

Because you’re hooking a DLabel.

I don’t get it.

wher are the variables health and armor defined

and u really shouldn’t be doing any of this stuff like this inside think hooks of the vgui elements: just add a think hook and update the text from there if the health has changed?

At the top of my script.




local health = LocalPlayer():Health()
local armor = LocalPlayer():Armor()





local INFO = vgui.Create("DPanel", HUDF)

INFO:Dock(FILL)


function INFO:Paint(w,h)
	
	surface.SetDrawColor(Color(0,0,0,150))
	surface.DrawRect(10,10,230,140)
	
	//Heart icon
	surface.SetDrawColor(Color(240,128,128))
	surface.SetMaterial(hearticon)
	surface.DrawTexturedRect(15,15,32,32)
	
	//Armor icon
	surface.SetDrawColor(Color(65,105,225))
	surface.SetMaterial(shieldicon)
	surface.DrawTexturedRect(15,50,32,32)

	//Health bar
	draw.RoundedBox(3,52,28,health * 1.8,5,Color(240,128,128))
	
	//Armor bar
	draw.RoundedBox(3,52,68,armor * 1.8,5,Color(65,105,225))
end


Still does not work for some odd reason. I removed the elseif statements etc., basically removed everything I added down to the core and it still does not update upon taking dmg, while this exact same script did update earlier (before adding the elseif etc.)

That’s because you are calling LocalPlayer:Health() once. You need to call it every time in the Paint hook if you want it to update.

Omg I’m so dumb :goodjob:

Thank you!

When setting size or position you should be using the PerformLayout function on the parent, like frame would set the armor and health bar pos in frames own PerformLayout.