Blitz3D+ Command Reference

First typename

Parameters

typename - the name of a type declared with a Type block

Description

Returns the first object in a type's list, or Null if there are none.

b.bullet = First bullet grabs the oldest surviving bullet, since New adds objects to the end of the list.

Its most common use has nothing to do with the object itself: If First bullet = Null is the quickest way to ask "are there any left". That is how you tell when every enemy in a wave is dead, when the last particle has faded, or when an inventory is empty.

It is also the starting point for walking a list by hand. Take First, then step along with After until you hit Null - useful when you need finer control than a For ... = Each loop gives you, such as stopping early or looking at pairs of neighbours.

And it is a handy way to pop items off a queue: take First, deal with it, Delete it, and repeat until Null.

See also: Last, After, Before, Each, Null, Type.

Example

; First Example
; -------------

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

; New appends each enemy to the end of the collection
Spawn("Scout",20)
Spawn("Grunt",50)
Spawn("Brute",80)

Print "The squad, in collection order:"
ShowSquad()

; First returns the object at the front of the collection
e.Enemy=First Enemy

Print ""
Print "First in line: "+e\name+" ("+e\hp+" hp)"

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