RenderOverride not works automatically.

Hello everyone.

There is a problem with RenderOverride in NPC* entity.

I do Dr.Kleiner with santa hat.
The problem is that the hat, which is drawn in the function RenderOverride does not work after spawn entity, but only after I update (resave) lua file. And alse doesn’t work after reconnect.
How can I fix it?

shared.lua



AddCSLuaFile()

ENT.Base = "base_ai"
ENT.Type = "ai"
ENT.AutomaticFrameAdvance = true

function ENT:SetAutomaticFrameAdvance( bUsingAnim )
	self.AutomaticFrameAdvance = bUsingAnim
end


if CLIENT then
	ENT.CM = ENT.CM or ClientsideModel("models/gmod_tower/santahat.mdl", RENDERGROUP_OPAQUE)
	ENT.CM:SetNoDraw(true)
end

function ENT:Initialize( )

	self:SetModel("models/Kleiner.mdl")
	self:SetHullType( HULL_HUMAN )
	self:SetHullSizeNormal( )
	self:SetNPCState( NPC_STATE_SCRIPT )
	self:SetSolid(  SOLID_BBOX )
	self:CapabilitiesAdd( CAP_ANIMATEDFACE || CAP_TURN_HEAD )
	self:SetUseType( SIMPLE_USE )
	self:DropToFloor()
	self:SetMaxYawSpeed( 90 )
	self.Entity.last=0

end

	
function ENT:RenderOverride()
	local model = self.CM
	pos, ang = self:GetBonePosition(self:LookupBone("ValveBiped.Bip01_Head1"))
	model:SetModelScale(1.2, 0)
	pos = pos + (ang:Forward() * 4.3) 
	ang:RotateAroundAxis(ang:Forward(), 180)
	ang:RotateAroundAxis(ang:Up(), -40)
	ang:RotateAroundAxis(ang:Right(), -90)

	model:SetPos(pos)
	model:SetAngles(model:LocalToWorldAngles(Angle(0,1,0)))
	model:SetRenderOrigin(pos)
	model:SetRenderAngles(ang)
	model:SetupBones()
	model:DrawModel()
	model:SetRenderOrigin()
	model:SetRenderAngles()
	self:DrawModel()

end


P.S. Sorry for my English

Just use ENT:DrawTranslucent()

you need to create ENT.CM in initialize

To elaborate more on this, you should put this in your entities Initialize because this will make one hat per entity spawned. If you don’t do this, you will only make a new hat every time the file is updated.

If that doesn’t make any sense, think of it like a global variable. If you set it once, you’re only creating it once.

Yes, i know it, Actually Kleiner will one at map

In practice, the location that you declared the variable will work fine, but in theory the variable should be local to the entity itself, as there will be one hat per kleiner which just by coincidence happens to be one per map. If you ever want to add another kleiner, it won’t work.

And brandon, it won’t make a new ClientsideModel every time the file is reloaded/updated due to the inline conditional.

still a problem, Initialize and DrawTranslucent doesn’t work clientside … idk why

[editline]31st December 2013[/editline]

I change type from ‘ai’ to ‘anim’ and clientside starting work… but i need AI

Try ENT:Draw() instead then.