• Print

Author Topic: HUD Code Makes Me Crash  (Read 5308 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
HUD Code Makes Me Crash
« on: November 09, 2014, 11:37:22 am »
I wanted to start working with HUDs yesterday, so I found a video on YouTube for it and then fixed the code he showed (His code was outdated and produced errors)

After making it work with GM13, I realized whenever I was on my server running the code for a few minutes, I would crash out of GMod with no errors or anything

I get no errors on the code and my game shows no errors when I crash
My GMod will just suddenly close

I'm sure this code is making me crash, because when I moved it to another folder where the code wouldn't run, I stopped crashing

Code: Lua
  1. //include( "shared.lua" )
  2.  
  3. hook.Add( "HUDShouldDraw", "hidethehud", function( name )
  4.         if (name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo") then return false end end )
  5.  
  6. function PaintZeeHud()
  7.         local ply = LocalPlayer()
  8.         local HP = ply:Health()
  9.         local ARM = ply:Armor()
  10.         local NewScrH = (ScrH() - 50 )
  11.        
  12.         draw.RoundedBox( 10, 10, NewScrH - 30, 75, 75, Color( 87, 81, 81, 255 ) )
  13.         surface.CreateFont( "ZeeFont", {
  14.         font = "ScoreboardText",
  15.         size = 40,
  16.         weight = 255,
  17.         antialias = false,
  18.         underline = false,
  19.         italic = false,
  20.         })
  21.         surface.SetTextColor( 0, 0, 255, 255 )
  22.         if (ply:Health() == 100) then
  23.                 surface.SetTextPos( 20, NewScrH - 14)
  24.         elseif (ply:Health() == 0) then
  25.                 surface.SetTextPos( 39, NewScrH - 14 )
  26.         else
  27.                 surface.SetTextPos( 30, NewScrH - 14 )
  28.         end
  29.         surface.SetFont( "ZeeFont" )
  30.         surface.DrawText( HP )
  31. end
  32. hook.Add( "HUDPaint", "PaintZeeHUDHook", PaintZeeHud )

Does anyone know what I did wrong?
Line 1 is commented out because I didn't create a shared.lua file yet

Offline Dog

  • Newbie
  • *
  • Posts: 17
  • Karma: 1
Re: HUD Code Makes Me Crash
« Reply #1 on: November 11, 2014, 07:49:08 pm »
I didn't know if that was a default font or not so I went with Arial. The HUD worked for me after I changed the function name and removed a hook:
Code: Lua
  1. //include( "shared.lua" )
  2.  
  3. hook.Add( "HUDShouldDraw", "hidethehud", function( name )
  4.         if (name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo") then return false end end )
  5.  
  6. function GM:HUDPaint()
  7.         local ply = LocalPlayer()
  8.         local HP = ply:Health()
  9.         local ARM = ply:Armor()
  10.         local NewScrH = (ScrH() - 50 )
  11.  
  12.         draw.RoundedBox( 10, 10, NewScrH - 30, 75, 75, Color( 87, 81, 81, 255 ) )
  13.         surface.CreateFont( "ZeeFont", {
  14.         font = "Arial",
  15.         size = 40,
  16.         weight = 255,
  17.         antialias = false,
  18.         underline = false,
  19.         italic = false,
  20.         })
  21.         surface.SetTextColor( 0, 0, 255, 255 )
  22.         if (ply:Health() == 100) then
  23.                 surface.SetTextPos( 20, NewScrH - 14)
  24.         elseif (ply:Health() == 0) then
  25.                 surface.SetTextPos( 39, NewScrH - 14 )
  26.         else
  27.                 surface.SetTextPos( 30, NewScrH - 14 )
  28.         end
  29.         surface.SetFont( "ZeeFont" )
  30.         surface.DrawText( HP )
  31. end

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: HUD Code Makes Me Crash
« Reply #2 on: November 12, 2014, 07:50:17 pm »
Getting this error on that code
Why would GM: not be recognized?

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: HUD Code Makes Me Crash
« Reply #3 on: November 12, 2014, 08:23:31 pm »
Ouch Zmaster, learn to copy/paste text from console :P
I've little to no experience with gamemode, but probably cause GM has to be registered in some way.
Had some oddity like that with a script many years ago.
Had to go from GM to "gamemode" or something like that.
GAMEMODE perhaps.
SOrry, don't fully remember.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: HUD Code Makes Me Crash
« Reply #4 on: November 13, 2014, 12:33:21 pm »
Okay...
Changed GM to GAMEMODE and no longer got an error

However, after a minute or two of severe lag on my computer (was getting this lag before I came here for help), GMod will crash.

Again, no errors
I just start lagging a ton, then my game closes and the lag stops

I've tried reinstalling my game and removing unnecessary addons and even verified my game cache, but the problem is still occuring

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: HUD Code Makes Me Crash
« Reply #5 on: November 13, 2014, 04:16:54 pm »
Did you try "Dog's" code, not using a custom font?
If not, try that.
If that works, investigate to see if a font can/should be created outside of HUDPaint, then used within it.
I would imagine creating a font every frame draw, even the same one, might not be the proper way to do it.
(Again, I'm inexperienced here...I've never worked with GUI in Gmod)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Dog

  • Newbie
  • *
  • Posts: 17
  • Karma: 1
Re: HUD Code Makes Me Crash
« Reply #6 on: November 13, 2014, 07:41:10 pm »
Okay, so a non-laggy code I have got:
Code: Lua
  1. include( "shared.lua" )
  2.  
  3. hook.Add( "HUDShouldDraw", "hidethehud", function( name )
  4.         if (name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo") then return false end end )
  5.  
  6. surface.CreateFont( "ZeeFont", {
  7.         font = "Arial",
  8.         size = 40,
  9.         weight = 255,
  10.         antialias = false,
  11.         underline = false,
  12.         italic = false,
  13.         })
  14.  
  15. function GAMEMODE:HUDPaint()
  16.         local ply = LocalPlayer()
  17.         local HP = ply:Health()
  18.         local ARM = ply:Armor()
  19.         local NewScrH = (ScrH() - 50 )
  20.  
  21.         draw.RoundedBox( 10, 10, NewScrH - 30, 75, 75, Color( 87, 81, 81, 255 ) )
  22.         surface.SetTextColor( 0, 0, 255, 255 )
  23.         if (ply:Health() == 100) then
  24.                 surface.SetTextPos( 20, NewScrH - 14)
  25.         elseif (ply:Health() == 0) then
  26.                 surface.SetTextPos( 39, NewScrH - 14 )
  27.         else
  28.                 surface.SetTextPos( 30, NewScrH - 14 )
  29.         end
  30.         surface.SetFont( "ZeeFont" )
  31.         surface.DrawText( HP )
  32. end
  33.  

To import a font you can just make a simple addon(that's how I did it) and then it'll download to the client.
Code: Lua
  1. if SERVER then
  2.         resource.AddSingleFile("resource/fonts/<font file name here>.ttf")
  3. end
« Last Edit: November 13, 2014, 07:47:39 pm by Dog »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: HUD Code Makes Me Crash
« Reply #7 on: November 13, 2014, 07:56:26 pm »
So it does seem like creating a font every frame is what is causing the issue.
My educated guessing sometimes works. :)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: HUD Code Makes Me Crash
« Reply #8 on: November 14, 2014, 05:47:59 am »
haha

I didn't even think about that, but now that you mention it, it would make sense to make the font outside of the GAMEMODE:HudPaint

Thanks for the help, JamminR and Dog

  • Print