Avatar Image

Hello, I’m having a bit of a problem with putting an avatar in my HUD. It is causing my frame rate to drop from 300 to 50, I originally had it outside of my HUD function But that dropped my frame rate. So I decided to try put it in a new file but it still caused my frame rate to drop. Could it be my hook?[lua]hook.Add( “HUDPaint”, “Avatar”, Avatar )[/lua] Please help.

You’re using the correct hook, but it’s probably the “Avatar” function you call that causes the drop. You need to provide some more code if you want help with that.

Use something like this to draw your Avatar.


   local function Avatar(p)
      av = vgui.Create("AvatarImage")
      av:SetPos(x, y)
      av:SetSize(w, h)
      av:SetPlayer( p, size )
   end

   if av then
      return
   else
      Avatar( LocalPlayer() )
   end


My best guess is that you’re creating a vgui avatar element every single frame. You only have to create it once, I suggest doing it in the Initialize hook.

So I did that but now it shows the ‘?’ image instead of my avatar. I used [lua]hook.Add( “Initialize”, “Avatar”, Avatar )[/lua]

Show us what the function Avatar is.

[lua]function Avatar()
local Avatar = vgui.Create( “AvatarImage”, Panel )
Avatar:SetSize( 64,64 )
Avatar:SetPos( ScrW() - 68,4 )
Avatar:SetPlayer( LocalPlayer(), 64 )
AvatarShouldDraw = 0
end
hook.Add( “Initialize”, “Avatar”, Avatar )[/lua]

If you use it with initialize, you have to wait until the player is actually authenticated before drawing the avatar.

Add a timer to that and it will work, you can play around with the timer to get the quickest response:


function Avatar()
timer.Simple(2, function()
  local Avatar = vgui.Create( "AvatarImage", Panel )
  Avatar:SetSize( 64,64 )
  Avatar:SetPos( ScrW() - 68,4 )
  Avatar:SetPlayer( LocalPlayer(), 64 )
  AvatarShouldDraw = 0
  end)
end
hook.Add( "Initialize", "Avatar", Avatar )

That worked, thanks.

Ah yes, LocalPlayer() would be nil in the Initialize hook, silly me. There’s probably a hook that is called when the LocalPlayer() is valid, but I can’t navigate the hooks in the wiki so this’ll do:

[lua]
hook.Add(“Think”, “AddAvatarImage”, function()
if ( not IsValid(LocalPlayer()) ) then return end
Avatar()
hook.Remove(“Think”, “AddAvatarImage”)
end)
[/lua]

[editline]22nd October 2013[/editline]

Oh and you may also want to do Avatar:ParentToHUD()

Ok, I added that. Thanks for the help.