Okay I have been working on a custom HUD for a while and after getting help for the local variables for the primary and secondary ammo (Thanks wakeboarderCWB!) I now have a new problem, every time I die the HUD disappears completely and it doesn't come back even after I respond. Does anyone know what the problem might be?
Here is the code:
[lua]function HideHudBits( name )
if ( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo" or name == "CHudPoisonDamageIndicator") then
return false
end
end
hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits )
surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" )
hook.Add ( "HUDPaint", "D9HudPaint", function()
local hudAmmo = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
local hudAmmo2 = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetSecondaryAmmoType())
local Current_Ammo = LocalPlayer():GetActiveWeapon():Clip1()
local hudPlayer = LocalPlayer()
local hudHealth = hudPlayer:Health()
local hudArmor = hudPlayer:Armor()
surface.SetTextColor( 255, 0, 0, 255 )
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/4.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudHealth )
surface.SetTextColor(221, 111, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/5.8) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudArmor )
surface.SetTextColor(0, 255, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/8.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo )
surface.SetTextColor(0, 255, 255, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/18) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo2 )
draw.RoundedBox(0, 5, ScrH() -180 -20, hudHealth, 15, Color(255,0,0,255))
draw.RoundedBox(0, 5, ScrH() -240 -20, hudArmor, 15, Color(221,111,0,255))
draw.RoundedBox(0, 5, ScrH() -300 -20, hudAmmo, 15, Color(0, 255, 0, 255))
draw.RoundedBox(0, 5, ScrH() -360 -20, hudAmmo2, 15, Color(0, 255, 255, 255))
end)
[/lua]
try adding [lua]if !LocalPlayer():Alive() then return end[/lua] to the hudpaint hook
It's on a SWEP, right? If so of course it'll go away after you die because you no longer have the weapon.
[QUOTE=jrj996;31092527]It's on a SWEP, right? If so of course it'll go away after you die because you no longer have the weapon.[/QUOTE]
Its not on a SWEP this is a custom HUD that will replace the HL2 HUD. I don't know why it keeps disappearing after I die, when it had variables, text, and bars for the health and armor it came back after I respawned.
Taking a long shot here, but try this.
[lua]function HideHudBits( name )
if ( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo" or name == "CHudPoisonDamageIndicator") then
return false
end
end
hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits )
surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" )
hook.Add ( "HUDPaint", "D9HudPaint", function()
if LocalPlayer():Alive() then
local hudAmmo = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
local hudAmmo2 = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetSecondaryAmmoType())
local Current_Ammo = LocalPlayer():GetActiveWeapon():Clip1()
local hudPlayer = LocalPlayer()
local hudHealth = hudPlayer:Health()
local hudArmor = hudPlayer:Armor()
surface.SetTextColor( 255, 0, 0, 255 )
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/4.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudHealth )
surface.SetTextColor(221, 111, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/5.8) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudArmor )
surface.SetTextColor(0, 255, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/8.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo )
surface.SetTextColor(0, 255, 255, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/18) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo2 )
draw.RoundedBox(0, 5, ScrH() -180 -20, hudHealth, 15, Color(255,0,0,255))
draw.RoundedBox(0, 5, ScrH() -240 -20, hudArmor, 15, Color(221,111,0,255))
draw.RoundedBox(0, 5, ScrH() -300 -20, hudAmmo, 15, Color(0, 255, 0, 255))
draw.RoundedBox(0, 5, ScrH() -360 -20, hudAmmo2, 15, Color(0, 255, 255, 255))
end
end)[/lua]
Haven't tried it, but it may work.
[QUOTE=techtuts0;31092208]try adding [lua]if !LocalPlayer():Alive() then return end[/lua] to the hudpaint hook[/QUOTE]
[QUOTE=wakeboarderCWB;31096640]Taking a long shot here, but try this.
(Code)
Haven't tried it, but it may work.[/QUOTE]
I'm thinking you're erroring on the lines where you're getting the ammo. Both of these will just hide the HUD when you're dead. They will keep it from erroring, but if you want to show the HUD when you're dead you will need to do some checks to make sure that the player's active weapon is valid (Use [b][url=http://wiki.garrysmod.com/?title=G.ValidEntity]G.ValidEntity [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]) before you try to get it's ammo.
No I don't want the HUD to show up when I'm dead, I want it to show up after I respawn. It disappears after I die for the first time in-game and it doesn't come back after I respawn.
It was failing because when the player dies, their active weapon returns a [B]NULL entity[/B]. Added a check for this, seems to work:
[lua]
function HideHudBits( name )
if ( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo" or name == "CHudPoisonDamageIndicator") then
return false
end
end
hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits )
surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" )
hook.Add( "HUDPaint", "D9HudPaint", function()
if !(LocalPlayer()&&LocalPlayer():Alive()) then return end
if !(LocalPlayer():GetActiveWeapon() && IsValid(LocalPlayer():GetActiveWeapon())) then return end
local hudAmmo = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
local hudAmmo2 = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetSecondaryAmmoType())
local Current_Ammo = LocalPlayer():GetActiveWeapon():Clip1()
local hudPlayer = LocalPlayer()
local hudHealth = hudPlayer:Health()
local hudArmor = hudPlayer:Armor()
surface.SetTextColor( 255, 0, 0, 255 )
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/4.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudHealth )
surface.SetTextColor(221, 111, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/5.8) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudArmor )
surface.SetTextColor(0, 255, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/8.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo )
surface.SetTextColor(0, 255, 255, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/18) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo2 )
draw.RoundedBox(0, 5, ScrH() -180 -20, hudHealth, 15, Color(255,0,0,255))
draw.RoundedBox(0, 5, ScrH() -240 -20, hudArmor, 15, Color(221,111,0,255))
draw.RoundedBox(0, 5, ScrH() -300 -20, hudAmmo, 15, Color(0, 255, 0, 255))
draw.RoundedBox(0, 5, ScrH() -360 -20, hudAmmo2, 15, Color(0, 255, 255, 255))
end)
[/lua]
TheFreeman I tried out what you suggested, but in the end I kept getting an error that said that there was an unexpected symbol on line 10 near '!' and it wouldn't work at all.
[QUOTE=thefreeman193;31152529]It was failing because when the player dies, their active weapon returns a [B]NULL entity[/B]. Added a check for this, seems to work:
[lua]
function HideHudBits( name )
if ( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo" or name == "CHudPoisonDamageIndicator") then
return false
end
end
hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits )
surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" )
hook.Add( "HUDPaint", "D9HudPaint", function()
if !(LocalPlayer()&&LocalPlayer():Alive()) then return end
if !(LocalPlayer():GetActiveWeapon() && IsValid(LocalPlayer():GetActiveWeapon())) then return end
local hudAmmo = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
local hudAmmo2 = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetSecondaryAmmoType())
local Current_Ammo = LocalPlayer():GetActiveWeapon():Clip1()
local hudPlayer = LocalPlayer()
local hudHealth = hudPlayer:Health()
local hudArmor = hudPlayer:Armor()
surface.SetTextColor( 255, 0, 0, 255 )
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/4.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudHealth )
surface.SetTextColor(221, 111, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/5.8) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudArmor )
surface.SetTextColor(0, 255, 0, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/8.5) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo )
surface.SetTextColor(0, 255, 255, 255)
surface.SetTextPos( 9, (ScrH()/2) + (ScrH()/18) )
surface.SetFont("D9NonHumans")
surface.DrawText( hudAmmo2 )
draw.RoundedBox(0, 5, ScrH() -180 -20, hudHealth, 15, Color(255,0,0,255))
draw.RoundedBox(0, 5, ScrH() -240 -20, hudArmor, 15, Color(221,111,0,255))
draw.RoundedBox(0, 5, ScrH() -300 -20, hudAmmo, 15, Color(0, 255, 0, 255))
draw.RoundedBox(0, 5, ScrH() -360 -20, hudAmmo2, 15, Color(0, 255, 255, 255))
end)
[/lua][/QUOTE]
Why you check if LocalPlayer() exists fascinates me, of course LocalPlayer() exists, after all who do you think is mean to see the hud? :P
[QUOTE=TheFallen(TF2);31177231]TheFreeman I tried out what you suggested, but in the end I kept getting an error that said that there was an unexpected symbol on line 10 near '!' and it wouldn't work at all.[/QUOTE]
Just tried that code and works fine for me :P Sure you havent accidently copied it wrong? And remember it needs to be [B]clientside[/B].
[QUOTE=Gfoose;31178260]Why you check if LocalPlayer() exists fascinates me, of course LocalPlayer() exists, after all who do you think is mean to see the hud? :P[/QUOTE]
Just in case the function doesn't return what it should, and it's good practise to run checks anyway :v:
Sorry, you need to Log In to post a reply to this thread.