• Custom HUD game crash
    20 replies, posted
Hello, I'm fairly new to LUA, and do not know a lot about it. I'm good with really basic things, and I recently decided that I want to learn it more. I am working on a HUD for DarkRP, and for some reason, every time I die, the game crashes :/ Currently, my HUD only displays the amount of health in the corner of the screen (yes, thats all i have so far) and when I went to test it, it crashed every time I died, and had to start my game back up. I have confirmed that it is the HUD that is causing the problem as well, because it works fine when I remove the code. Here's what I have so far: [CODE] function hidehud( myhud ) for k, v in pairs( {"CHudHealth", "CHudBattery"} ) do if( myhud == v ) then return false; end end end hook.Add("HUDShouldDraw", "myHudHider", hidehud); function CreateFont() surface.CreateFont("Health", {font = "ScoreboardText", size = 40, weight = 500, antialias = true}) end function GM:HUDPaint() CreateFont() self.BaseClass:HUDPaint() local ply = LocalPlayer() local HP = ply:Health() local ARM = ply:Armor() surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 20, 20 ) surface.SetFont("Health") surface.DrawText( HP ) surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 80, 80 ) surface.SetFont("Health") surface.DrawText( ARM ) end [/CODE]
You shouldn't create a font every time the HUDPaint hook is called!
How would I go about not making the font every time? I'm a noob, sorry.
Try removing self.BaseClass:HUDPaint() Also, don't create the font every frame, it is not needed, use the InitPostEntity hook, that way it'll create it when the player joins.
How do I stop the font from being created every frame?
[QUOTE=IMayhem;45384780]How do I stop the font from being created every frame?[/QUOTE] create the font in the InitPostEntity hook
[code] function hidehud( myhud ) for k, v in pairs( {"CHudHealth", "CHudBattery"} ) do if( myhud == v ) then return false; end end end hook.Add("HUDShouldDraw", "myHudHider", hidehud); function CreateFont() surface.CreateFont("Health", {font = "ScoreboardText", size = 40, weight = 500, antialias = true}) end hook.Add("InitPostEntity", "CreateFont", CreateFont) function GM:HUDPaint() local ply = LocalPlayer() local HP = ply:Health() local ARM = ply:Armor() surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 20, 20 ) surface.SetFont("Health") surface.DrawText( HP ) surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 80, 80 ) surface.SetFont("Health") surface.DrawText( ARM ) end [/code]
Still crashes :(
Which gamemode is this for?
[QUOTE=ms333;45384932]Which gamemode is this for?[/QUOTE] DarkRP
Untested: [lua] local HUDstohide = { ['CHudHealth'] = true, ['CHudBattery'] = true } hook.Add("HUDShouldDraw", "HideDefaultHUD", function(hudname) if HUDstohide[hudname] then return false end end) surface.CreateFont("Health", { font = "ScoreboardText", size = 40, weight = 500, antialias = true }) local ply = LocalPlayer() hook.Add('HUDPaint', 'MyCustomHUD', function() local HP = ply:Health() local ARM = ply:Armor() surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 20, 20 ) surface.SetFont("Health") surface.DrawText(HP) surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 80, 80 ) surface.SetFont("Health") surface.DrawText(ARM) end) [/lua]
[QUOTE=ms333;45384982]Untested: [lua] local HUDstohide = { ['CHudHealth'] = true, ['CHudBattery'] = true } hook.Add("HUDShouldDraw", "HideDefaultHUD", function(hudname) if HUDstohide[hudname] then return false end end) surface.CreateFont("Health", { font = "ScoreboardText", size = 40, weight = 500, antialias = true }) local ply = LocalPlayer() hook.Add('HUDPaint', 'MyCustomHUD', function() local HP = ply:Health() local ARM = ply:Armor() surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 20, 20 ) surface.SetFont("Health") surface.DrawText(HP) surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 80, 80 ) surface.SetFont("Health") surface.DrawText(ARM) end) [/lua][/QUOTE] HUD doesn't show up, and i get this error: [CODE][ERROR] gamemodes/darkrp/gamemode/modules/hud/cl_hud.lua:22: Tried to use a NULL entity! 1. Health - [C]:-1 2. fn - gamemodes/darkrp/gamemode/modules/hud/cl_hud.lua:22 3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 [/CODE]
[QUOTE=IMayhem;45385017]HUD doesn't show up, and i get this error: [CODE][ERROR] gamemodes/darkrp/gamemode/modules/hud/cl_hud.lua:22: Tried to use a NULL entity! 1. Health - [C]:-1 2. fn - gamemodes/darkrp/gamemode/modules/hud/cl_hud.lua:22 3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 [/CODE][/QUOTE] I'm guessing this is because ply isn't initialized when this is first called. Try this lazy fix: [code]local HUDstohide = { ['CHudHealth'] = true, ['CHudBattery'] = true } hook.Add("HUDShouldDraw", "HideDefaultHUD", function(hudname) if HUDstohide[hudname] then return false end end) surface.CreateFont("Health", { font = "ScoreboardText", size = 40, weight = 500, antialias = true }) hook.Add('HUDPaint', 'MyCustomHUD', function() local ply = LocalPlayer() local HP = ply:Health() local ARM = ply:Armor() surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 20, 20 ) surface.SetFont("Health") surface.DrawText(HP) surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 80, 80 ) surface.SetFont("Health") surface.DrawText(ARM) end)[/code]
[QUOTE=Braden1996;45385165]I'm guessing this is because ply isn't initialized when this is first called. Try this lazy fix: [code]local HUDstohide = { ['CHudHealth'] = true, ['CHudBattery'] = true } hook.Add("HUDShouldDraw", "HideDefaultHUD", function(hudname) if HUDstohide[hudname] then return false end end) surface.CreateFont("Health", { font = "ScoreboardText", size = 40, weight = 500, antialias = true }) hook.Add('HUDPaint', 'MyCustomHUD', function() local ply = LocalPlayer() local HP = ply:Health() local ARM = ply:Armor() surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 20, 20 ) surface.SetFont("Health") surface.DrawText(HP) surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 80, 80 ) surface.SetFont("Health") surface.DrawText(ARM) end)[/code][/QUOTE] Fixed errors, still crashes when I die.
[QUOTE=IMayhem;45385223]Fixed errors, still crashes when I die.[/QUOTE] Oh, it crashes when you die? [code] local HUDstohide = { ['CHudHealth'] = true, ['CHudBattery'] = true } hook.Add("HUDShouldDraw", "HideDefaultHUD", function(hudname) if HUDstohide[hudname] then return false end end) surface.CreateFont("Health", { font = "ScoreboardText", size = 40, weight = 500, antialias = true }) hook.Add('HUDPaint', 'MyCustomHUD', function() local ply = LocalPlayer() if !ply:Alive() then return end local HP = ply:Health() local ARM = ply:Armor() surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 20, 20 ) surface.SetFont("Health") surface.DrawText(HP) surface.SetTextColor( 200, 0, 0, 40 ) surface.SetTextPos( 80, 80 ) surface.SetFont("Health") surface.DrawText(ARM) end) [/code] There.
Found the problem, actually had nothing to do with the HUD after all. Was because my team was "Unassigned" when I died.
Perhaps a thankyou is in order from these chaps which gave you some custom code. Which was also done in their spare time. Even if your not going it use it etc..
[QUOTE=DubyxD;45387858]Perhaps a thankyou is in order from these chaps which gave you some custom code. Which was also done in their spare time. Even if your not going it use it etc..[/QUOTE] Lol, wasn't trying to be rude like "yea you guys suck it didn't work." Was just stating what fixed the problem in case anybody happens to come across the same problem.
"I found the issue guys...Thanks for the help and time that you took to try to help me anyway"
Let me get started on my 10 page thank you letter. Wasn't aware that what I said was so bad.
Not defending him or anything, but it's volunteer work for us developers to help people like him, he doesn't have to thank us, of coarse it's expected and is nice, but he doesn't actually need to. No one in this thread has done it, but I have seen a few people on the forum after helping someone posting a reply expecting them to say thanks, or getting angry for not saying thanks. Do people understand that they volunteer to help, you shouldn't be volunteering just to get a thanks. You should be volunteering because you want to help someone and/or yourself. Yes, I love it when people say thanks, and dislike it when they don't. But that's not my problem really.
Sorry, you need to Log In to post a reply to this thread.