Blitz3D+ Command Reference

After object

Parameters

object - a variable holding an object of a custom type

Description

Returns the next object along in the type's list, or Null at the end.

After takes an object, not a type name - that is the difference from First and Last, and the mistake everybody makes once.

Paired with First it lets you walk a list by hand:

b.bullet = First bullet
While b <> Null
    ; ... do something with b ...
    b = After b
Wend

For a straightforward sweep, For ... = Each is shorter and does the same job. Reach for After when you need something Each cannot give you: stopping partway, comparing each object with its neighbour, or skipping ahead.

After the last object it returns Null, which is what ends the loop. If you are deleting as you go, read After into a variable before the delete.

See also: Before, First, Last, Each, Null, Insert.

Example

; After 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)

; Walk the squad from front to back using After
Print "Roll call, front to back:"

e.Enemy=First Enemy
While e<>Null
    Print "    "+e\name+" ("+e\hp+" hp)"
    ; After moves to the next object - it returns Null past the last one
    e=After e
Wend

Print "No one is after the last enemy - the walk ends."

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

Index