Is there a way to check if an Entity still exists?

rgbDreamerrgbDreamer Join Date: 2011-10-02 Member: 125190Members
<div class="IPBDescription">Attempt to access an object that no longer exists (was type Marine)</div>I've been getting a "Attempt to access an object that no longer exists (was type SquadUplink)" errors. Is there a way to check my reference to see if it still exists? The reference isn't nil and throws an error if I try to index it.

Also, is there a callback reliably called when an Entity stops "existing"? I've tried :OnDestroy(), but I might be doing something else wrong.

Thanks!

Comments

  • playerplayer Join Date: 2010-09-12 Member: 73982Members
    You are right. An entity's OnDestroy-method is called when said entity is about to be destroyed, so override it to know when this happens. Alternatively, you could keep track of entities by their Id instead (a simple number upward from 1 that is always unique and will not be used again after the entity is destroyed). You may then use Shared.GetEntity( integer EntityId ) to fetch a reference to the actual entity, and if it doesn't exist (anymore) it will probably return nil to tell you this (haven't tried it, speculating).
  • fsfodfsfod uk Join Date: 2004-04-09 Member: 27810Members, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Subnautica Playtester, NS2 Community Developer, Pistachionauts
    If you doing this in a function you added to the marine class you could use OnEntityChange that gets called when any entity is destroyed or transfered to a new entity

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--> function Marine:OnEntityChange(oldEntityId, newEntityId)

       Player.OnEntityChange(self, oldEntityId, newEntityId)

       if(oldEntityId == self.UplinkId and not newEntityId) then
         //handle uplink being destroyed
       end

    end<!--c2--></div><!--ec2-->
  • HackepeterHackepeter Join Date: 2003-06-08 Member: 17107Members, Constellation
    edited November 2011
    <!--quoteo(post=1883476:date=Nov 3 2011, 06:06 PM:name=player)--><div class='quotetop'>QUOTE (player @ Nov 3 2011, 06:06 PM) <a href="index.php?act=findpost&pid=1883476"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->You may then use Shared.GetEntity( integer EntityId ) to fetch a reference to the actual entity, and if it doesn't exist (anymore) it will probably return nil to tell you this (haven't tried it, speculating).<!--QuoteEnd--></div><!--QuoteEEnd-->

    Yeah exactly, it returns a nil reference if it can't find a matching entity for that id. I also use that method, imho it's safer then working with a direct reference to a entity across update ticks.
Sign In or Register to comment.