Concommand show/hide a custom hud

I’m curious on how to make your custom hud toggleable between concommands like hud_turnoff hud_turnon

I habitually do something like this:



local enabled = true

local function drawHUD()
	-- draw here
end

hook.Add( "HUDPaint", "drawHUD", function() drawHUD() end )

concommand.Add( "hud_toggle",
	function()
		enabled = not enabled
		if enabled == true then
			hook.Add( "HUDPaint", "drawHUD", function() drawHUD() end )
		else
			hook.Remove( "HUDPaint", "drawHUD" )
		end
	end
)


Or you could also do something like this, a little less efficient:



local enabled = true

hook.Add( "HUDPaint", "drawHUD",
    function()
        if enabled == true then
            -- draw here
        end
    end
)

concommand.Add( "hud_toggle",
	function()
		enabled = not enabled
	end
)


Oh god.

The first method is technically more efficient because you’re deleting and adding the hook when needed. But in your code the enabled = not enabled should be the last thing in the function body and your hook.Add function doesn’t need the function() drawHUD() end. You are just making a lambda function that calls another function which is pointless. Just pass drawHUD, no parenthesis, as the third argument to hook.Add.

The second method works just fine and isn’t actually bad in terms of efficiency at all.