Nikki_5.56 13 Jun, 2013 @ 6:33am
[SOLVED] How to check if a CBaseEntity exists?
I'm working on a Source engine mod and I'm stuck on how to solve this simple problem.

I built the example code from this page into a weapon:
https://developer.valvesoftware.com/wiki/UTIL_EntitiesInSphere

The weapon filters it down to all the NPCs in a given radius, puts them in an array, and then a Think function blows them up one after another over a couple seconds.
The problem is, if another player kills an NPC in that array before the function gets to them, the engine crashes because it's trying to apply damage to something non-existent.

This seems easy, all I need to do here is check if the NPC is still around before trying to damage it.
I tried reading various variables and stuff that are part of the NPC, but they all return garbage once it's dead. ->IsAlive and ->IsNPC both crash the game. Dead things pass a != NULL check.

So, short version:

How do I safely check if an entity exists/is valid?
Last edited by Nikki_5.56; 13 Jun, 2013 @ 5:14pm
< >
Showing 1-2 of 2 comments
Sandern 13 Jun, 2013 @ 9:55am 
Nikki_5.56 13 Jun, 2013 @ 5:13pm 
Thanks, that works perfectly!
< >
Showing 1-2 of 2 comments
Per page: 1530 50