HUD causes errors to spam after some time.

Hi there.

I am currently working on a HUD for my custom gamemode, but i have run into some trouble.

After some time (this is usually random), my HUD suddenly stops functioning and spams lua errors. This is the error to be Exact:

[ERROR] lua/includes/util/client.lua:135: Tried to use a NULL entity!
  1. GetRenderBounds - [C]:-1
   2. PositionSpawnIcon - lua/includes/util/client.lua:135
    3. DrawCircularCustomHud - gamemodes/fc3dm/gamemode/hud/cl_hud.lua:119
     4. DrawHUD - gamemodes/fc3dm/gamemode/hud/cl_hud.lua:237
      5. unknown - gamemodes/fc3dm/gamemode/hud/cl_hud.lua:245

The hud is this one:

The hud uses some code from older garrysmod 12 huds, which may be why the errors are happening. But i cannot pinpoint the cause of it. Any help?


It seems the error is happening here:
[lua]PortraitModel = ClientsideModel(LocalPlayer():GetModel(), RENDERGROUP_OPAQUE)
PortraitModelView = PositionSpawnIcon( PortraitModel, Vector(0,0,0) )[/lua]

Which means the model might be getting garbage collected or something. You will need to do some validity checks and recreate the model if it gets deleted.

I deleted the whole hook, but the the error happened again. It also spat this at the same time:

CBaseEntityList::AddNonNetworkableEntity: no free slots!


[lua]PortraitModel = ClientsideModel(LocalPlayer():GetModel(), RENDERGROUP_OPAQUE)[/lua]

You are creating a new entity every frame. Those entities never get deleted, which is why you eventually run out of entity slots, hence the “no free slots” error.

PortraitModel should be created only once. Since the local player may not exist or have a valid model when the game starts, you should give it some dummy model to make sure the entity gets created (ClientsideModel with an invalid model does not create an entity). I personally use “models/props_junk/watermelon01.mdl”, but any model will do.

Then in your DrawCircularCustomHud function, just use SetModel to set its model to the local player’s.

Can’t be garbage collected if there’s a reference to it.

Clientside models are always finicky every time I use them.

typed this up real quick. Do you mean something like this?
local function myfunction()
if !showmyderma then
PortraitModel = ClientsideModel(LocalPlayer():GetModel(), RENDERGROUP_OPAQUE)
PortraitModelView = PositionSpawnIcon( PortraitModel, Vector(0,0,0) )
showmyderma = true

PlayerIcon:SetModel(LocalPlayer():GetModel() or "models/props_junk/watermelon01.mdl")

hook.Add(“HUDPaint”, “myfunction”, myfunction)

More like this:

[lua]PortraitModel = ClientsideModel(“models/props_junk/watermelon01.mdl”)

local function myfunction()
PortraitModelView = PositionSpawnIcon( PortraitModel, Vector(0,0,0) )


hook.Add(“HUDPaint”, “myfunction”, myfunction)

RENDERGROUP_OPAQUE isn’t necessary in ClientsideModel, and you need to apply SetNoDraw(true) to make sure it doesn’t show up in the world. By initializing PortraitModel outside of any function, you are making sure that it is created right as the map loads.
Since PositionSpawnIcon probably depends on the model, you need to recalculate PortraitModelView every frame just in case the player’s model changes. You could also detect whenever the model changes and recalculate PortraitModelView only whenever it does, but since I really doubt it’s an expensive computation, it’s probably not worth the trouble.