Getting a Player Avatar to work correctly on a HUD.
6 replies, posted
I am creating a HUD for my server that I play with friends on, and i am trying to show the Player's Avatar in the bottom left corner.
This is how it looks:
[IMG]https://www.dropbox.com/s/foxl4m9489lbbxw/Game.jpg?dl=1[/IMG]
The problem comes when trying to change it. I have a ConVar to disable the HUD, but the Avatar stays when i change it.
[IMG]https://www.dropbox.com/s/colml5nspc2u8cu/Disabled.jpg?dl=1[/IMG]
It also still shows it when i hold the Camera SWEP
[IMG]https://www.dropbox.com/s/ihzhiihfp5ijsg7/Camera.jpg?dl=1[/IMG]
Here is the actual function that creates it (Cutting out un-related code):
[CODE]function DrawHealth()
if ONEUSE == 0 then -- So it doesn't create shit tons of avatars
local Avatar = vgui.Create( "AvatarImage")
Avatar:SetSize( 64, 64 )
Avatar:SetPos( 15, ScrH() - 80 )
Avatar:SetPlayer( LocalPlayer(), 64)
Avatar:ParentToHUD()
ONEUSE = 1
end
end[/CODE]
And it is called in here:
[CODE]hook.Add( "HUDPaint", "customhud_drawhud", function() -- Create the function that will call our drawing functions to draw the HUD.
if GetConVar( "customhud_enabled" ):GetBool() then -- Only if we want it on
DrawHealth()
DrawAmmo()
end
end )[/CODE]
Store the avatar in a global variable rather than a local one, then you can delete it.
[editline]15th October 2016[/editline]
local Avatar
function DrawHealth()
if ONEUSE == 0 then -- So it doesn't create shit tons of avatars
Avatar = vgui.Create( "AvatarImage")
Avatar:SetSize( 64, 64 )
Avatar:SetPos( 15, ScrH() - 80 )
Avatar:SetPlayer( LocalPlayer(), 64)
Avatar:ParentToHUD()
ONEUSE = 1
end
end
hook.Add( "HUDPaint", "customhud_drawhud", function() -- Create the function that will call our drawing functions to draw the HUD.
if GetConVar( "customhud_enabled" ):GetBool() then -- Only if we want it on
DrawHealth()
DrawAmmo()
else
Avatar:Remove()
ONEUSE = 0
end
end )
Cool, that works for removing it, and i managed to get it to bring it back by detecting if it was valid when true, and if not, setting ONEUSE = 0, however, that still doesn't hide it while i'm holding the camera.
It wont hide with camera because its not a HUD element, it's a GUI element.
[editline]15th October 2016[/editline]
Your options are:
Paint the AvatarImage manually
Hardcode the panel to hide when the camera swep is up
Obey return value of the [url]http://wiki.garrysmod.com/page/GM/HUDShouldDraw[/url]
AHA! I did it! Oh... you responded. XD. I figured it out by removing the Avatar while the player is holding "gmod_camera"
[editline]15th October 2016[/editline]
Thank you for your help! Probably one of the more complicated things i have tried to do in GMod LUA so far.
The worst option, but whatever suits you.
Oh, yeah, sorry, i already did it so i didn't read the suggestions, but im going to try HUDShouldDraw.
Sorry, you need to Log In to post a reply to this thread.