vgui.DModelPanel constantly being made?

Basically I had setup a DModelPanel, which resluted fine visually until I realised it was constantly drawing this DModelPanel causing my game to lag?
I had tried putting loop = false at the top of the whole lua file then after the DModelPanel loop = true then having this at the bottom:
hook.Add(“HUDPaint”, “DrawHUD”, function()
if loop == true then portrait() end
end)
This stopped the whole HUD being produced with no error in console so I don’t know what to do?

Can we see the portrait function?

hook.Add( “HUDPaint”, “lolportrait”, function()
local portrait = vgui.Create( “DModelPanel” )
loop = true
portrait:SetPos( 10, ScrH() - 80 )
portrait:SetSize( 70, 80 )
portrait:SetModel( LocalPlayer():GetModel() )
portrait.Think = function()
portrait:SetModel( LocalPlayer():GetModel() )
end
portrait.LayoutEntity = function()
return false
end
portrait:SetFOV( 40 )
portrait:SetCamPos( Vector( 25, -15, 62 ) )
portrait:SetLookAt( Vector( 0, 0, 62 ) )
portrait.Entity:SetEyeTarget( Vector( 200, 200, 100 ) )
end )

don’t use hudpaint

What should I use then?

Define the local portrait outside of the HUDPaint and instead at the start of that function put.


if IsValid(portrait) then return end

Okay I tried that but the hud is stilll not being made at all.

Can i see the newest code?
ALL of it?
Beacouse it seems you are only giving us a part of it.

A panel constantly drawing is fine. What causes your game to lag is that you’re constantly creating the panel. You want to create it once, but draw it for as long as whenever needed.
If you want to draw any kind of panel in a HUDPaint hook, fine, but you should paint it manually; here’s how:

  1. Create the panel OUTSIDE of the HUDPaint hook, and call

Panel:SetPaintedManually. This will make the panel invisible, requiring you to draw it manually
2. In the HUDPaint hook, check if the panel is valid. If it is, call

Panel:PaintManual on it to draw the panel whenever needed (there’s a more elaborate example in that page)

Okay after using this at first it worked fine but then I rejoined as I was moving the ModelPanel and there was multiple in the way and it produced this error:

[ERROR] addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:205: Tried to use a NULL entity!

  1. GetModel - [C]:-1
  2. unknown - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:205
    3. doInclude - [C]:-1
    4. loadModules - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:102
    5. Call - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:147
    6. unknown - gamemodes/darkrp/gamemode/cl_init.lua:53
    UPDATE: I recopied it over with no changes and seemed to fix this error? Seems like it launches the code once the player is sending client info making it believe the GetModel is a null entity. And after using the PaintManual code it still produces the DModelPanel to a large scale.
    Here is my code for the DModelPanel

local portrait = vgui.Create( "DModelPanel" )
	portrait:SetPos( 10, ScrH() - 150 )
	portrait:SetSize( 70, 80 )
	portrait:SetPaintedManually( true )
	portrait:SetModel( LocalPlayer():GetModel() )
	portrait.Think = function()
		portrait:SetModel( LocalPlayer():GetModel() )
	end
	portrait.LayoutEntity = function()
		return false
	end
	portrait:SetFOV( 40 )
	portrait:SetCamPos( Vector( 25, -15, 62 ) )
	portrait:SetLookAt( Vector( 0, 0, 62 ) )
	portrait.Entity:SetEyeTarget( Vector( 200, 200, 100 ) )

	hook.Add( "HUDPaint", portrait, function()
		if IsValid( portrait ) then
		portrait:PaintManual()
		end
	end)

Please put the code inside


 tags

[editline]3rd August 2016[/editline]

Also create the panel inside the Initialize hook, beacouse LocalPlayer is not valid when that runs, and beacouse you are creating as well a VGUI element too early.

Done I think I found my source the icons I have on it are also creating a lot but the fix for the get model null entity is what I am after fixing now

With the initialize hook the “NULL entity!” error should dissapear

Which initialize hook?

GM:Initialize
Are you even reading what im saying?
Take the second example in the code of that page in the wiki.

Okay, also next problem I have ran into is DImage’s I have setup are producing a lot also? Would I use the ManualPaint() to fix it?

Whats the DImage? A material/image you want to draw?
If its a material/image instead of making it with a DImage draw it with the surface library.

It is an Image and got an example of the surface code or right article on gmod wiki?

Material

surface.SetMaterial

surface.DrawTexturedRect

Create the material outside of the HUDPaint Hook

Ahh okay also I tried implementing the GM:Initialize() and I doubt this is right but can you tell me what I have done wrong?


function GM:Initialize()
	local portrait = vgui.Create( "DModelPanel" )
	portrait:SetPos( 10, ScrH() - 150 )
	portrait:SetSize( 70, 80 )
--	portrait:SetPaintedManually( true )
	portrait:SetModel( LocalPlayer():GetModel() )
	portrait.Think = function()
		portrait:SetModel( LocalPlayer():GetModel() )
	end
	portrait.LayoutEntity = function()
		return false
	end
	portrait:SetFOV( 40 )
	portrait:SetCamPos( Vector( 25, -15, 62 ) )
	portrait:SetLookAt( Vector( 0, 0, 62 ) )
	portrait.Entity:SetEyeTarget( Vector( 200, 200, 100 ) )
end
hook.Add( "Initialize", "someswag", init )