Checking if a NextBot is dead

Okay, since NextBots aren’t affected by GM:OnNPCKilled(), what’s a good way to check if a nextbot is dead? This is all I can come up with(not tested yet, I wrote the code in-thread and haven’t put it in init.lua :P)


function GM:Think()
for k, v in pairs(ents.GetAll()) do
if v:GetClass() == "simple_nextbot" and v:Health() <= 0 then
StartVictoryBehavior() --this is a function I have defined
end
end
end


Nextbots and NPCs cease to exist after they are killed, so you should be looking for something like #ents.FindByClass(“simple_nextbot”) < 1.

You can make Nextbots fire OnNPCKiller by doing this:



function ENT:OnKilled( dmginfo )
	hook.Call( "OnNPCKilled", GAMEMODE, self, dmginfo:GetAttacker(), dmginfo:GetInflictor() )
end

Thanks, RobotBoy!

[editline]16th May 2015[/editline]

Wait, are you saying run this code in the nextbot’s code or run this in the gamemode’s code? I’m guessing in the nextbot’s code due to ENT:?

[editline]16th May 2015[/editline]

nvm

[editline]16th May 2015[/editline]

Actually, RobotBoy, I know a better way to do it.



function Killed( dmginfo )
	hook.Call( "OnNPCKilled", GAMEMODE, self, dmginfo:GetAttacker(), dmginfo:GetInflictor() )
end
hook.Add( "OnKilled", "Bleh", Killed )


is the better way to do this. Why? Well, if we override the hook, then it is impossible for the nextbot to die. If we make this function get called upon the hook OnKilled being called, then the nextbot will be able to die AND it will call OnNPCKilled.