|
object - the object to move Before|After - which side of the other object to place it other - the object to position it relative to |
|
Moves an object to a new position in its type's list. Objects made with New always go on the end of the list. Insert is how you put one somewhere else: Insert a Before b puts a immediately ahead of b, and Insert a After b puts it immediately behind. Both objects must be of the same type. Insert moves an existing object rather than copying it, so the list never grows - the object simply leaves its old position and reappears at the new one. List order matters more often than it first appears, because For ... = Each follows it. Anything you draw from a type list is drawn in list order, so Insert gives you depth sorting for sprites. It also gives you priority queues - insert a new job ahead of the ones it should pre-empt - and keeps a scoreboard or an inventory sorted as entries are added, without an array to shuffle. Combine it with First and Last to reach the ends: Insert a Before First thing makes a the new head of the list. See also: New, Before, After, First, Last, Type. |
; Insert 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 "Squad in recruitment order:"
ShowSquad()
; A commander arrives - New puts her at the END of the collection
c.Enemy=New Enemy
c\name="Commander"
c\hp=120
; Insert moves her object to the front of the collection instead
Insert c Before First Enemy
Print ""
Print "After Insert c Before First Enemy:"
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