Putting a players steam avatar in a hud?

Hey, im trying to grab the LocalPlayer’s steam avatar when PlayerAuthed gets ran. But it just puts a question mark for their avatar. I think it’s because the player entity doesn’t exist yet when PlayerAuthed gets run, so it doesn’t know their avatar yet. Any ideas? thank you in advance.


function DrawSteamAvatar()
		
		local Avatar = vgui.Create( "AvatarImage", Panel )
	
		Avatar:SetSize( 32, 32 )
		Avatar:SetPos( 11, ScrH() - 134 )
		Avatar:SetPlayer( LocalPlayer(), 32 )

end
hook.Add( "PlayerAuthed", "1943rphud", DrawSteamAvatar() )

Wait until the player is valid in a HUDPaint hook and then only create the avatar image ONCE.

How would i go about that? ( I know Lua but not a lot, sorry ) I know you probably don’t want to, but you explain further on how to make a custom check under that hook, like how to set it up. Also, how would i only create the avatar once?

EDIT:

Here, i tried it out, it’s probably not correct, but i gave it a shot.


function HUDValidCheck()

	if LocalPlayer():IsValid() then
	
		local Avatar = vgui.Create( "AvatarImage", Panel )
	
		Avatar:SetSize( 32, 32 )
		Avatar:SetPos( 11, ScrH() - 134 )
		Avatar:SetPlayer( LocalPlayer(), 32 )
	
	end

end
hook.Add( "HUDPaint", "1943rphud", HUDValidCheck() )



local avatar
function HUDValidCheck()

    if IsValid( LocalPlayer() ) && !IsValid( avatar ) then
    
        avatar= vgui.Create( "AvatarImage" )
    
        avatar:SetSize( 32, 32 )
        avatar:SetPos( 11, ScrH() - 134 )
        avatar:SetPlayer( LocalPlayer(), 32 )
    
    end

end
hook.Add( "HUDPaint", "1943rphud", HUDValidCheck )

Here, this should work, learn from your mistakes.

You might wanna add "elseif( IsValid( avatar)) then " to update the position or size of you need that.

[lua]local Avatar

function HUDValidCheck()
if !LocalPlayer():IsValid() then return end – don’t bother doing ANYTHING unless the player is valid

if !Avatar then
	Avatar = vgui.Create( "AvatarImage", Panel )

	Avatar:SetSize( 32, 32 )
	Avatar:SetPos( 11, ScrH() - 134 )
	Avatar:SetPlayer( LocalPlayer(), 32 )
end

-- use Avatar here, draw the rest of the hud

end
hook.Add( “HUDPaint”, “1943rphud”, HUDValidCheck() )[/lua]

Edit: ninja’d, of course. :rolleyes:

use Robotboy’s solution, it’s better.

Don’t ever use ent:IsValid() in such, or any case really, use IsValid( ent )

Yeah, I forgot, was basically just recycling as much as I could from his code.

feels ashamed

Okay, so I have this running in the HUDPaint hook and it’s not drawing the avatar at all. I tooled around with it for about 10 minutes and couldn’t figure out why.

EDIT:
It’s creating the avatar but it’s creating it every time hudpaint is called. How do i call it just once?

EDIT… Again:
Alright, this time it’s fixed, as far as i know. Thanks for leading me in the right direction guys. :dance:


local Avatar

	function HUDValidCheck()
		if !LocalPlayer():IsValid() then 
		
			return
			
		end
	
		if !Avatar then
		
			Avatar = vgui.Create( "AvatarImage", Panel )
	
			Avatar:SetSize( 32, 32 )
			Avatar:SetPos( 11, ScrH() - 134 )
			Avatar:SetPlayer( LocalPlayer(), 32 )
			
		end

	end

LocalPlayer() is never not an entity so why? I also don’t understand why this needs to go into a HUDPaint hook. Shouldn’t there be a hook that tells when the player is initialized, like InitPostEntity or something? I’m going to do some tests with SysTime() and see.



CreateTeams	46.026282600821
PreGamemodeLoaded	46.026320635112
CloseDermaMenus	46.026395194397
ForceDermaSkin	46.026516240039
PopulateMenuBar	46.029385414151
PopulateToolMenu	46.029469632939
PostReloadToolsMenu	46.161814477179
OnGamemodeLoaded	46.179864766964
PostGamemodeLoaded	46.17990038638
Initialize	46.179912762618
DrawOverlay	46.333332226515
Tick	46.336190231828
HUDShouldDraw	46.337098225066
OnEntityCreated	51.758895496257
InitPostEntity	53.31377746921
ShouldDrawLocalPlayer	53.320028072929
StartCommand	53.320150627868
CreateMove	53.320168135716
CalcView	53.332121770104
CalcViewModelView	53.332342731224
AdjustMouseSensitivity	53.332383482251
CalcMainActivity	53.332416384931
TranslateActivity	53.332454721082
UpdateAnimation	53.332562786766
NetworkEntityCreated	53.332663909684
Redownloading all lightmaps
PreRender	53.349922120261
RenderScene	53.350167833857
DrawMonitors	53.350193190051
PreDrawSkyBox	53.350230922482
SetupSkyboxFog	53.350246921034
PostDraw2DSkyBox	53.353009840618
PreDrawOpaqueRenderables	53.355592248249
PostDrawOpaqueRenderables	53.355621226757
PreDrawTranslucentRenderables	53.355638130886
PostDrawTranslucentRenderables	53.355662883362
PostDrawSkyBox	53.358400144893
NeedsDepthPass	53.358690835547
SetupWorldFog	53.358708343395
DrawPhysgunBeam	53.359478990582
PreDrawViewModel	53.367053549867
PostDrawViewModel	53.367276624004
PreDrawEffects	53.36742242212
RenderScreenspaceEffects	53.367460456411
PreDrawHalos	53.367490340497
PostDrawEffects	53.367507546486
PreDrawHUD	53.367519620865
PostDrawHUD	53.36757214441
PostRenderVGUI	53.367629497706
PostRender	53.367649722289
Think	53.480239676407
InputMouseApply	53.481239434919
SetupMove	53.507712207196
PlayerTick	53.507758391693
Move	53.507837780729
FinishMove	53.507894228447
PlayerPostThink	53.507919282782
DoAnimationEvent	53.513245894711
HUDPaintBackground	53.515668920551
HUDPaint	53.515813813089
HUDDrawTargetID	53.515860903163
HUDDrawPickupHistory	53.515926406665
DrawDeathNotice	53.515960516783
HUDDrawScoreBoard	53.516042320695
PlayerButtonDown	63.855872675682
PlayerButtonUp	63.890642960638


InitPostEntity works fine. Just tested it. I also tried the Initialize hook, but it didn’t load the avatar correctly.

[lua]
local function HUDValidCheck()

local Avatar = vgui.Create( "AvatarImage", Panel )

Avatar:SetSize( 32, 32 )
Avatar:SetPos( 11, ScrH() - 134 )
Avatar:SetPlayer( LocalPlayer(), 32 )

end
hook.Add( “InitPostEntity”, “1943rphud”, HUDValidCheck )
[/lua]

Those of you rating me, prove me wrong.

[lua]
local ply = LocalPlayer()

hook.Add( “Think”, “LocalPlayerIsntAlwaysValid”, function()
print( IsValid( ply ) )
end )
[/lua]

Why is nobody using panel:ParentToHUD() function?

He never said that it was always valid, he said it’s always AN ENTITY:

It’s true that you’ll always be able to run LocalPlayer():IsValid(). I still believe it’s good practice to use the global IsValid function. While it’s fine not to for entities, for panels (and maybe other objects) certain methods will return nil rather than a NULL userdata. You also can’t rely on mod-makers to abide to using NULL rather than nil.
It’s nice to be able to use the same methods for every situation.