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.
Discussion
EricBlade: I'm having a little problem with this function in a particular object, derived from the Fire? class. I have built a weapon, that when fired, spawns a fire attached to it. When fired again, it calls thefire.DestroyFire() which eventually calls Destroy(). However, after that is all done, the reference to it still exists, and the next time i go to spawn a fire, it decides the original one still exists, so it uses that, instead. Huh? (the problem with this is that the original one is no longer based on the weapon, after the Destroy() call and any attempts to set it to do so fail) After some more testing, I've discovered that bDeleteMe is set to true, but the object is never actually destroyed.