Destroying Objects
Destroying Actors
Actors are destroyed by calling their Destroy() function. Destruction is latent, i.e. the actor is destroyed at the end of the current tick, although the Destroyed() event is executed immediately. The actor is moved to the list of destroyed actors then. Note that calling Destroy() will not halt the execution of a function that is already started, or stop other functions from being called on that actor. If you wish to avoid this behavior, the actor variable bDeleteMe is set to true when an actor has been "destroyed" but has not yet been deleted.
Chain Of Events
When the Destroy() function is called, several other UnrealScript events get called as well before code execution continues after the call to Destroy().
- the actor's EndState() event
- the actor's Destroyed() event
- the LostChild() event of the actor's Owner
- all references to this actor are set to None
Warning: This also detaches the actor from its children (i.e. sets their Owner property to None) without calling LostChild(). If the actor gains a new child afterwards, the engine gets confused and crashes the game soon.
Destroying Non-Actor Objects
Objects will be cleaned up at garbage collection time, provided there are no Actors that retain references to them. You can explicitly force garbage collection using a console command, or wait for the normal collection during a level switch. See my notes on the new keyword about making sure your objects are safe for garbage collection.