surface lowering fps / crashing game

Hey, i have started making a hud as a little project of mine. I have encountered an issue where my hud is now lowering my fps from an average of 200
to about 30 - 50. I’m not sure if this is something about surface i’ve never been told before but here is a snip of my code:


	surface.SetDrawColor(54,217,252)
	surface.DrawRect(0,ScrH()-200,350,200)
	-- Name Bar
	surface.SetDrawColor(0,0,0)
	surface.DrawRect(0,ScrH()-200,350,25)
	surface.SetFont("fliq_medi")
	surface.SetTextPos(0,ScrH()-200)
	surface.SetTextColor(Color(255,255,255))
	surface.DrawText("test")

Is this because i’m using surface or something else?

That shouldn’t cause that amount of FPS loss but you don’t need to call ScrH 3 times and you should be caching your colours in the main chunk.

Not to seem like a dumbass, what do you mean by this

The main chunk is just another way of saying global scope. If you don’t know what that is then you should read up on scopes.

Also, make sure you’re not using surface.CreateFont or any VGUI inside any HUDPaint hooks!

Here’s a good rule of thumb. Keep anything that doesn’t need constant updating out of the HUDPaint hook, see if you can remove anything and then see if you can increase performance. If performance is really really shit check if you have any model rendering or Derma VGUI stuff inside a Think or HUDPaint hook, I’ve made that mistake before.

This snippet of code is probably not the cause of your fps loss. Show us more.

Ok thanks for all your replies, I figured it was when i made a font it was inside the hudpain, but if i use it anywhere else i get an error:


[ERROR] addons/fliq_hud/lua/autorun/cl_fonts.lua:1: attempt to index global 'surface' (a nil value)
  1. unknown - addons/fliq_hud/lua/autorun/cl_fonts.lua:1


Where abouts should i be making the font? I currently have it


include("path to the file")

You’re running it on the server and client.


if CLIENT then
include("cl_fonts.lua")

local RemoveElms = {"DarkRP_HUD", "DarkRP_LocalPlayerHUD"}
local function HUDShouldDraw(elm)
	if table.HasValue(RemoveElms,elm) then return false end
end
hook.Add("HUDShouldDraw","HUDShouldDraw",HUDShouldDraw)
hook.Add("HUDPaint","fliq_hud",function()
	local sw = ScrW()
	local sh = ScrH()
	local mat = Material("materials/fliq_hud/main.png")
	local plyn = LocalPlayer():Nick()
	local plym = LocalPlayer():getDarkRPVar("money")
	-- Main Box
	surface.SetDrawColor(255,255,255)
	surface.SetMaterial(mat)
	surface.DrawTexturedRect(0,sh-150,300,150)
	-- Name Bar
	surface.SetTextColor(Color(255,255,255))
	surface.SetTextPos(0,sh-150)
	surface.SetFont("FmediInfo")
	surface.DrawText(plyn)
	-- Money Bar
	surface.SetFont("FsmallInfo")
	surface.SetTextPos(50,sh-100)
	surface.DrawText("Money: ".. plym)
end)
end

It should be client tho?

Well I found the source of the lag: you’re calling Material every frame. But that code shouldn’t cause that error. The error is because you’re running cl_fonts.lua on the server. If you’re going to include it then just keep it in “/lua/”.

Ok, i’ve never tried making a hud before i’ve only dabbled around with ents. thanks for the info, so hudpaint is kind of like a think func? called so many timed each tick / frame?

HUDPaint is called every frame.

Ok, thanks for the help.