Blitz3D+ Command Reference

Delete object

Parameters

object - a variable holding the object to destroy, or Each typename to destroy every object of a type

Description

Destroys a custom type object and removes it from its type's list.

Delete b removes that one bullet. Delete Each bullet clears the lot in a single statement, which is exactly what you want when a level ends or the player restarts.

Deleting from inside a For ... = Each loop is safe and is the normal way to do it - the loop has already noted where it is going next, so removing the current object does not break the iteration. That gives you the standard update-and-cull pattern: walk every object, move it, and delete it in the same pass if it has expired or gone off screen.

The one thing to be careful of is other variables. Deleting an object does not clear any other variable that happens to point at it, so a saved reference - the target a homing missile is chasing, say - is left pointing at something that no longer exists. Set those to Null yourself when you delete the thing they refer to, and check for Null before using them.

See also: New, Type, Each, Null, First, Insert.

Example

; Delete Example
; --------------

; A squad of enemies, kept in a Type collection
Type Enemy
    Field name$
    Field hp
End Type

Spawn("Scout",20)
Spawn("Grunt",50)
Spawn("Brute",80)

Print "Before the battle:"
ShowSquad()

; The Grunt goes down - Delete removes that object from the collection
For e.Enemy=Each Enemy
    If e\name="Grunt" Then Delete e
Next

Print ""
Print "After deleting the Grunt:"
ShowSquad()

Print ""
Print "Press any key to close the example"
WaitKey

End

; Create one enemy and set its fields
Function Spawn(n$,hp)
    e.Enemy=New Enemy
    e\name=n$
    e\hp=hp
End Function

; Print every enemy in collection order
Function ShowSquad()
    For e.Enemy=Each Enemy
        Print "    "+e\name+" ("+e\hp+" hp)"
    Next
End Function

Index