Hud

Another help support post, but this time i need with my hud theres something wrong when ever i boot it up.


function HUDHide(myhud)
	for k, v in pairs{"CHudHealth","CHudBattery"} do
		if myhud == v then return false end
	end
end
hook.Add("HUDShouldDraw","HudHide",HUDHide)

function GM:HUDPaint()
	self.BaseClass:HUDPaint()
	local ply = LocalPlayer()
	local HP  = LocalPlayer():Health()
	local ARM = LocalPlayer():Armor()
	
	surface.CreateFont("ScoreboardText", 40, 180, false, false, "MyFont")
	surface.SetTextColor( 20, 180, 50, 50, 255)
	surface.SetTextPos( 20,20 )
	surface.SetFont( "MyFont" )
	surface.DrawText( HP )
end

Gives me a hole bunch of errors saying theres with


surface.CreateFont("ScoreboardText", 40, 180, false, false, "MyFont")[HR][/HR]

You are using the surface.CreateFont parameters incorrectly, see here:

Also, you’re putting the CreateFont in a HUDPaint hook and, unless you’re doing it for a gamemode, making the hook wrong.

By creating a font in the HUD hook ( which runs over and over, once per frame drawn ), you’re creating the font over and over until you run out of memory. A similar thing can happen if you use Material( ) inside of HUDPaint or other hooks that repeat.

Additionally, don’t use a table.HasValue approach on HUDShouldDraw ( difference between 15 second run time compared to 0.08 second when using direct access ). Lua is a language of tables, and they can be very optimized :slight_smile:

Proper HUD Creation ( just shows checking for mandatories and implementing HUDShouldDraw into your huds ):
https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html