• HUD Crashing Players
    10 replies, posted
I started working on a HUD yesterday out of boredom and I have, what I consider, a good starting HUD. The issue is, after about 5 minutes of being on the server people crash. I've tested it on both SP and MP. Any help would be greatly appreciated. No errors are given client side or server side. [lua] local function NewCrosshair() local x = ScrW() / 2 local y = ScrH() / 2 surface.SetDrawColor( 0, 200, 0, 255 ) local gap = 10 local length = gap + 15 surface.DrawLine( x - length, y, x - gap, y ) surface.DrawLine( x + length, y, x + gap, y ) surface.DrawLine( x, y - length, x, y - gap ) surface.DrawLine( x, y + length, x, y + gap ) surface.DrawCircle( ScrW()/2, ScrH()/2, 30, 255,255,255,255 ) end local function Triflux() surface.CreateFont("Triflux1", { font = "Default", size = 24, weight = 1000, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = false, additive = false, outline = false, }) surface.CreateFont("Triflux2", { font = "Default", size = 24, weight = 300, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = false, additive = false, outline = false, }) surface.SetFont( "Triflux1" ) surface.SetTextColor( 0, 0, 255, 255 ) surface.SetTextPos( ScrW()/25, ScrH()/25 ) surface.DrawText("Triflux Gaming") surface.SetFont("Default") surface.SetTextColor( 0, 0, 255, 255 ) surface.SetTextPos( ScrW()/25, ScrH()/15 ) surface.DrawText(LocalPlayer():SteamID()) end local RemoveDefaultsTable = { ["CHudHealth"] = true, ["CHudBattery"] = true, ["CHudSuitPower"] = true, } local function RemoveDefaults( element ) if RemoveDefaultsTable[ element ] then return false end end hook.Add( "HUDShouldDraw", "RemoveDefaults", RemoveDefaults ) local Config = {} Config.W = 260 Config.H = 85 Config.PosX = 10 Config.PosY = 675 Config.HUDColorTable = Color(255, 255, 255, 255) Config.ElementBackground = Color(149, 165, 166, 255) Config.HealthColorTable = Color(150, 0, 0, 255) Config.ArmorColorTable = Color(0, 150, 200, 255) Config.ColorText1 = Color(255, 255, 255, 255) local function HUDHealth() local HealthDrawY = Config.PosY + 85 - 55 draw.RoundedBox( 5, Config.PosX + 10, HealthDrawY, Config.W - 20, 22, Config.ElementBackground ) local DrawValue = LocalPlayer():Health() or 0 local EchoValue = LocalPlayer():Health() or 0 local DrawWidth = Config.W - 20 if DrawValue < 0 then DrawValue = 0 elseif DrawValue > 100 then DrawValue = 100 end if DrawValue != 0 then draw.RoundedBox( 5, Config.PosX + 10, HealthDrawY, DrawWidth * DrawValue / 100, 22, Config.HealthColorTable ) end local DrawText = "Health: %"..EchoValue local DrawTextX = Config.PosX + DrawWidth/2 local DrawTextY = HealthDrawY+2 draw.DrawText( DrawText, "Default", DrawTextX, DrawTextY, Config.ColorText1, 1 ) end local function HUDArmor() local ArmorDrawY = Config.PosY + 85 - 32 draw.RoundedBox( 5, Config.PosX + 10, ArmorDrawY, Config.W - 20, 22, Config.ElementBackground ) local DrawValue = LocalPlayer():Armor() or 0 local EchoValue = LocalPlayer():Armor() or 0 local DrawWidth = Config.W - 20 if DrawValue < 0 then DrawValue = 0 elseif DrawValue > 100 then DrawValue = 100 end if DrawValue != 0 then draw.RoundedBox( 5, 10 + 10, ArmorDrawY, DrawWidth * DrawValue / 100, 22, Config.ArmorColorTable ) end local DrawText = "Armor: %"..EchoValue local DrawTextX = Config.PosX + DrawWidth/2 local DrawTextY = ArmorDrawY+2 draw.DrawText( DrawText, "Default", DrawTextX, DrawTextY, Config.ColorText1, 1 ) end local function HUDBase() draw.RoundedBox(10, Config.PosX, Config.PosY, Config.W, Config.H, Config.HUDColorTable) surface.SetFont( "Triflux2" ) surface.SetTextColor( 0, 0, 255, 255 ) surface.SetTextPos( Config.PosX + 5, Config.PosY ) surface.DrawText(LocalPlayer():Nick()) end local function Killcounter() w = 85 h = 100 x = 10 y = 300 draw.RoundedBox( 0, x, y, w, h, Color( 0, 0, 0, 100 ) ) surface.SetFont("Default") local tW, tH = surface.GetTextSize("Stats") draw.DrawText( "Stats:", "Default", x + 5, y + 8, color_white ) draw.DrawText( "Kills: "..LocalPlayer():Frags(), "Default", x + 5, y + (tH) + 8, color_white ) draw.DrawText( "Deaths:"..LocalPlayer():Deaths(), "Default", x + 5, y + (tH*2) + 8, color_white ) draw.DrawText( "K/D Ratio:"..math.Round(LocalPlayer():Frags()/LocalPlayer():Deaths(), 1), "Default", x + 5, y + (tH*3) + 8, color_white ) end local function TDMHUDPaint() NewCrosshair() Triflux() HUDBase() HUDHealth() HUDArmor() Killcounter() end hook.Add( "HUDPaint", "TDMHUDPaint", TDMHUDPaint ) [/lua]
don't create fonts more than once (move them outside of the triflux function)
[QUOTE=PortalGod;47417787]don't create fonts more than once (move them outside of the triflux function)[/QUOTE] I added the fonts to their own function, now I get this error. [code][ERROR] lua/autorun/client/crosshair.lua:56: 'Triflux1' isn't a valid font 1. SetFont - [C]:-1 2. Triflux - lua/autorun/client/crosshair.lua:56 3. v - lua/autorun/client/crosshair.lua:171 4. unknown - lua/includes/modules/hook.lua:84 [/code] Here's the function: [lua] local function CreateFonts() surface.CreateFont("Triflux1", { font = "Default", size = 24, weight = 1000, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = false, additive = false, outline = false, }) surface.CreateFont("Triflux2", { font = "Default", size = 24, weight = 300, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = false, additive = false, outline = false, }) end [/lua]
Just out of curiosity, you're sure you're actually calling that function, right?
[QUOTE=TFA;47417842]Just out of curiosity, you're sure you're actually calling that function, right?[/QUOTE] My bad. That's what I forgot to do for that part. Thank you for reminding me. Edit: Even after creating the new function for the fonts, players still crash.
Where are you calling the function?
[QUOTE=James xX;47417924]Where are you calling the function?[/QUOTE] In this function. [lua] local function TDMHUDPaint() NewCrosshair() CreateFonts() Triflux() HUDBase() HUDHealth() HUDArmor() Killcounter() end hook.Add( "HUDPaint", "TDMHUDPaint", TDMHUDPaint ) [/lua]
[QUOTE=Subject_Alpha;47417943]In this function. [lua] local function TDMHUDPaint() NewCrosshair() CreateFonts() Triflux() HUDBase() HUDHealth() HUDArmor() Killcounter() end hook.Add( "HUDPaint", "TDMHUDPaint", TDMHUDPaint ) [/lua][/QUOTE] So you're creating the fonts over and over again just like we said you shouldn't. Call the function in the global scope (outside the hook).
[QUOTE=James xX;47417946]So you're creating the fonts over and over again just like we said you shouldn't. Call the function in the global scope (outside the hook).[/QUOTE] How would you recommend I call the function? As in, what should I use to call the function?
[QUOTE=Subject_Alpha;47418053]How would you recommend I call the function? As in, what should I use to call the function?[/QUOTE] [lua] CreateFonts() -- I recommend you call it outside the function, like I said. local function TDMHUDPaint() NewCrosshair() Triflux() HUDBase() HUDHealth() HUDArmor() Killcounter() end hook.Add( "HUDPaint", "TDMHUDPaint", TDMHUDPaint ) [/lua]
[QUOTE=James xX;47418090][lua] CreateFonts() -- I recommend you call it outside the function, like I said. local function TDMHUDPaint() NewCrosshair() Triflux() HUDBase() HUDHealth() HUDArmor() Killcounter() end hook.Add( "HUDPaint", "TDMHUDPaint", TDMHUDPaint ) [/lua][/QUOTE] Works perfectly now! Thanks so much.
Sorry, you need to Log In to post a reply to this thread.