Removing a HUD Element Drawn with vgui.Create() ??

So I’m basically trying to add the player’s avatar image to the HUD while the player is alive and have it not show when the player is dead. Unfortunately the avatar keeps getting redrawn so many times that after 10 or 15 minutes my game lags so bad it crashes out. As usual the Wiki is as useful as wearing sunscreen lotion at night, and the closest example I found with a potential solution, the person never shared it.

http://forum.facepunch.com/showthread.php?t=1110291 <-- was the post where the person did not share the solution
http://gmodwiki.net/Lua/VGUI/Elements/AvatarImage <-- I understand how to vgui.Create, but I have no idea how to remove what gets created.

I apparently haven’t got any idea what I’m doing.

This is my completely incorrect attempt to use one function to draw the panels and one function to “remove” them if the player returns not Alive():



function testframe()
	wrapper = vgui.Create("DPanel")
	wrapper:SetSize(136,136)
	wrapper:SetPos(200,200)
	
	avatarImg = vgui.Create("AvatarImage", avatarImg)
	avatarImg:SetSize(128,128)
	avatarImg:SetPos(4,4)
	avatarImg:SetPlayer(LocalPlayer(), 128)
end

hook.Add("HUDPaint", "avatarthing", testframe)

function RemoveVGUI()
	if not LocalPlayer():Alive() then
		avatarIMG:Remove()
	end
end	
hook.Add("KeyRelease", "RemoveVGUI", RemoveVGUI)


As I said I can very easily get the code to draw a DPanel and AvatarImage with no problems. It’s getting them to go away once I’ve drawn them that’s proving to be baffling.

Any assistance would be much appreciated.


function testframe()
	wrapper = vgui.Create("DPanel")
	wrapper:SetSize(136,136)
	wrapper:SetPos(200,200)
	
	avatarImg = vgui.Create("AvatarImage", wrapper) -- it has to be wrapper like you write it on the panel function
	avatarImg:SetSize(128,128)
	avatarImg:SetPos(4,4)
	avatarImg:SetPlayer(LocalPlayer(), 128)
end

hook.Add("HUDPaint", "wrapper")  --same here
function RemoveVGUI()
	if not LocalPlayer():Alive() then
		avatarIMG:Remove()
	end
end	
hook.Add("KeyRelease", "RemoveVGUI", RemoveVGUI)

Remove it from the HUDPaint hook and put it somewhere else so that it’s only created once.

Then when they die you can use :Hide() on the panel, and :Show() when they respawn. Use death and spawn hooks for that.

Umm try this untested.


function testframe()
	wrapper = vgui.Create("DPanel")
	wrapper:SetSize(136,136)
	wrapper:SetPos(200,200)
	
	avatarImg = vgui.Create("AvatarImage", wrapper) -- it has to be wrapper like you write it on the panel function
	avatarImg:SetSize(128,128)
	avatarImg:SetPos(4,4)
	avatarImg:SetPlayer(LocalPlayer(), 128)
end
function RemoveVGUI()
	if ( !LocalPlayer():Alive() )  then
		avatarImg:Hide()
	else
		avatarImg:Show()
	end
end	
hook.Add("PlayerSpawn", "RemoveVGUI", RemoveVGUI)

I’m pretty sure it works.



function testframe()
	wrapper = vgui.Create("DPanel")
	wrapper:SetSize(136,136)
	wrapper:SetPos(200,200)
	
	avatarImg = vgui.Create("AvatarImage", wrapper) -- it has to be wrapper like you write it on the panel function
	avatarImg:SetSize(128,128)
	avatarImg:SetPos(4,4)
	avatarImg:SetPlayer(LocalPlayer(), 128)
end
function RemoveVGUI()
	if ( !LocalPlayer():Alive() )  then
		avatarIMG:Hide()
	else
		avatarImg:Show()
	end
end	
hook.Add("PlayerSpawn", "RemoveVGUI", RemoveVGUI)


…gives the error, even though wrapper doesn’t have ‘local’ in front of it:
Attempt to index global ‘wrapper’ (a nil value)

Change


wrapper = vgui.Create("DPanel")


to


local wrapper = vgui.Create("DPanel")


Well that stopped the Lua errors, but it does not show anything at all on the HUD. Neither does making a stage object ‘wrapper = {}’ prior to the function()

Lua variables are case sensitive, avatarIMG is not the same as avatarImg

Right. Thanks for the catch, but I had already corrected that when I ran it. Still having the major lag issues and still no image display at all. :confused:


Panel:MakePopup( )

use this.

Resources,

Still no display. I’m going to try to pick apart a DarkRP HUD https://github.com/MainFighter/mf_hud_v1

That and a couple scoreboard scripts if I find any. It’s frustrating when things like this that should work end up not working at all.

On that note, why can’t GMod have a dependency walker of sorts where you can monitor code execution line-by-line to trace errors???

[editline]19th October 2014[/editline]

I couldn’t get this to work any other way, but my FPS drops gradually over time now.



function testframe()
	local wrapper = vgui.Create("DPanel")
	wrapper:SetSize(136,136)
	wrapper:SetPos(200,200)
	
	local avatarImg = vgui.Create("AvatarImage", wrapper) -- it has to be wrapper like you write it on the panel function
	avatarImg:SetSize(128,128)
	avatarImg:SetPos(4,4)
	avatarImg:SetPlayer(LocalPlayer(), 128)
	if (!LocalPlayer():Alive()) then
	avatarImg:SetVisible(false)
	else
	avatarImg:SetVisible(true)
	end
end
hook.Add("HUDPaint", "RemoveVGUI", testframe)


I saw this https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index9d7f.html … got to wondering if that would show and hide, and it does. But it still won’t work if the vgui.Create isn’t in the same function as the HUDPaint hook >.<

[editline]19th October 2014[/editline]

Changed HUDPaint to InitPostEntity and it doesn’t drop my FPS anymore. But I can’t get it to disappear when the player dies.