ENT:Draw() glitching

I’ve got hats working in my gamemode now, but for some reason, the hats dont appear if you re too close to a player.

[LUA]

if SERVER then
AddCSLuaFile(“shared.lua”)
end

ENT.Type = “anim”
ENT.Base = “base_anim”

if CLIENT then
function ENT:Draw()

	if self:GetOwner() != LocalPlayer() && self:GetOwner():Alive() && LocalPlayer():GetObserverMode() != OBS_MODE_IN_EYE then
		local Owner = self:GetOwner()
		local tbl = Owner:GetAttachment(Owner:LookupAttachment("eyes"))
		local Hatt = HatO[Owner:GetNWString("HAT")]
		self:SetRenderOrigin(tbl.Pos + Hatt.POS)
		self:SetAngles(tbl.Ang + Hatt.ANG)
		self:SetModelScale(Hatt.SCA)
		self:DrawModel()

	end
end

end

[/LUA]

HatO references a bunch of tables containing added positions/angles

Current, the code works well, but I need to know why the hats won’t render (on the client of course) when the two players are within a specific range.

If anyone could help, would be much appreciated.

You should learn how to parent first, I wonder that this works at all.

Parenting changed nothing. I still have the issue.

What render group is it using?

You see, it’s things like this I don’t even know.

Amaze me with your knowledge, as the wiki isn’t providing any information about ‘rendergroups’.

** The creation of the entity **

[LUA]
local hat = ply:GetNWString(“HAT”)

        if hat != "none" then
            local myhat = ents.Create("ent_hat")
            myhat:SetModel(HatT[hat])
            myhat:SetOwner(ply)
            myhat:Spawn()
            ply.Hat = myhat
        end

[/lua]

Also, would you define getting “close”, what distances are we talking about?

[editline]14th August 2012[/editline]

In any case, try to add.

ENT.DrawTranslucent = ENT.Draw

After defining the draw function.

I’ll post a youtube vid, 1 sec

Double edit:

I also noticed that when I am behind the player who has the ‘hat’, the entity won’t draw.

Could this be due to the engine trying to not render it because it may be unviewable?

[editline]14th August 2012[/editline]

The disappearing is half gone now, due to putting in
ENT.DrawTranslucent = ENT.Draw

Thanks Wizard.

Now all I need to make it so it doesn’t disappear when standing behind a player.

Super shameless bump, because I desperately need an answer.

What I would do is a umsg.Start and usermessage.Hook to create the hat, set its position and angles to the player’s head, and parent it to the head bone. Then your draw function would be simple and fast like this.

[lua]
if CLIENT then
function ENT:Initialize()
self.Scale = HatO[self:GetOwner():GetNWString(“HAT”)].SCA
end
function ENT:Draw()

    local Owner = self:GetOwner()
    if Owner != LocalPlayer() && Owner && LocalPlayer():GetObserverMode() != OBS_MODE_IN_EYE then
        self:SetModelScale(self.Scale)
        self:DrawModel()
    end
end

end
[/lua]

I’ll let you code the usermessage.Hook. You’ll need Entity.SetParent and Entity.SetParentPhysNum

You probably need to set the render bounds of it.

**[Entity.SetRenderBoundsWS

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.SetRenderBoundsWS)**

This looks promising.

New wiki doesnt have it, old wiki just blurts out nonsense.

Time to try and decipher what it even means lol.

Thanks.

The new wiki is still being set up.

Well, as far as I can see now, this is fixed.

Thanks for that.