Destroy

From ZDoom Wiki
Jump to navigation Jump to search

void Destroy()

Usage

Destroys the calling Object. This is a full removal from the game and as such, the Object will become unusable after doing so. Any important clean up should be handled in the OnDestroy() virtual (e.g. destroying any child Objects, undoing any behaviors, etc.). Class fields that have references to this Object will be automatically nulled, however, there are two important instances where this won't happen:

  • On self. Any function that calls another function that might cause an Object to be destroyed should check bDestroyed after doing so. Code will continue to execute even when self has been destroyed which can result in unexpected behaviors.
  • On local variables. These have no read barrier meaning they can still directly reference destroyed Objects, so like above, their bDestroyed should be checked before continuing any operations on them in an instance where it might get destroyed.

This function can be called on any ZScript class, in any context or scope.

Examples

This function (presumably called from an actor) tries to spawn an Imp 64 units in front of the calling actor. If the Imp doesn't fit, it's destroyed.

Vector2 offset = AngleToVector(self.angle, 64);
Vector3 ppos = self.Vec3Offset(offset.x, offset.y, 0);
Actor thing = Spawn('DoomImp', ppos);
if (!thing.TestMobjLocation() || !level.IsPointInLevel(ppos))
{
	thing.Destroy();
}

(See also AngleToVector, Vec3Offset, TestMobjLocation, IsPointInLevel.)