Last typename
Parameters
| typename - the name of a type declared with a Type block |
Description
|
Returns the last object in a type's list, or Null if there are none. Because New appends to the end of the list, Last is the object you most recently created. e.enemy = Last enemy right after New enemy gives you the same object you just made, which is occasionally handier than keeping the variable around. Last is the starting point for walking a list backwards: take Last, then step with Before until you hit Null. Back-to-front order matters more often than you would think - drawing sprites so newer ones sit behind older ones, or processing an undo stack. Together with First it also makes a type list usable as a queue or a stack: push with New, pop the oldest with First, or pop the newest with Last. Like First, it returns Null on an empty list, so If Last thing = Null is a valid emptiness test. See also: First, Before, After, Each, Null, Type. |
Example
; Last 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() ; Last returns the object at the back of the collection e.Enemy=Last Enemy Print "" Print "Bringing up the rear: "+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