Hook entity spawn and remove

How to check when an entity gets spawned or removed by map or players?

spawncheck.lua located in “…\autorun”
[lua]
function GM:OnEntityCreated( npc )
Msg("created " … tostring(ent) … “!”)
end

function GM:EntityRemoved(ent)
Msg("removed " … tostring(ent) … “!”)
end
[/lua]
Doesn’t do anything.

Are you sure the file is being included correctly? Both of those functions work fine for me.

I guess it doesn’t work because its not a gamemode. It’s just a default addon.

It should work fine although it’s never a good idea to try and overwrite a function via an addon. Try using hooks instead

Got it working, thank you!
[lua]
function EntityCreated(ent)
Msg("created " … tostring(ent) … “!”)
end
hook.Add(“OnEntityCreated”, “EntCreatd”, EntityCreated)

function EntityRemoved(ent)
Msg("removed " … tostring(ent) … “!”)
end
hook.Add(“EntityRemoved”, “EntRemod”, EntityRemoved)
[/lua]

Got other problem.
[lua]
enemynpcinfo = {}

function EntityCreated(ent)
if(ValidEntity(ent) && ent:IsNPC()) then
if(IsEnemy(ent)) then
Msg( "enemy: " … tostring(ent) … "
")
table.insert(enemynpcinfo, ent)
end
end
end
hook.Add(“OnEntityCreated”, “EntCreat”, EntityCreated)

function IsEnemy( npc )
if(npc:GetModel() == “models/scientist.mdl”) then return true end
if(npc:GetModel() == “models/kleiner”) then return true end
if(npc:GetClass() == “npc_breen”) then return false end
if(npc:GetClass() == “npc_mytestnpc”) then return false end
return true
end
[/lua]
Why does it return everything that is NOT an enemy? Makes no sense at all…

It should return everything with model Kleiner or scientist and that it’s not Breen or mytestnpc.

EDIT: Message seems to be right, but PrintTable(enemynpcinfo) outputs:



1	=	[NPC 176/npc_mytestnpc]
2	=	[NULL NPC]
3	=	[NULL NPC]
4	=	[NULL NPC]
5	=	[NULL NPC]