Type Iteration with Loops

BambooBasic automatically maintains a list of all instances for each Type. You can iterate over all instances using special loop syntax.



For...EachIn Loop

The For...EachIn loop iterates through all instances of a Type:

Type TEnemy
    Field name:String
    Field health:Int
EndType

Function Main()
    ; Create some enemies
    Local goblin:TEnemy = Create TEnemy
    goblin\name = "Goblin"
    goblin\health = 30

    Local orc:TEnemy = Create TEnemy
    orc\name = "Orc"
    orc\health = 80

    Local dragon:TEnemy = Create TEnemy
    dragon\name = "Dragon"
    dragon\health = 500

    Print "=== All Enemies ==="

    ; Iterate through all TEnemy instances
    Local enemy:TEnemy
    For enemy EachIn TEnemy
        Print enemy\name & ": " & ToString(enemy\health) & " HP"
    Next

    Return False
EndFunction

Output:

=== All Enemies ===
Goblin: 30 HP
Orc: 80 HP
Dragon: 500 HP


For Each...In Loop (Alternative Syntax)

You can also use For Each...In syntax, which works identically:

Function Main()
    ; Create instances...
    Local goblin:TEnemy = Create TEnemy
    goblin\name = "Goblin"

    Local orc:TEnemy = Create TEnemy
    orc\name = "Orc"

    ; Alternative iteration syntax
    Local enemy:TEnemy
    For Each enemy In TEnemy
        Print enemy\name
    Next

    Return False
EndFunction

Note: Both For enemy EachIn and For Each enemy In work the same way. Use whichever you prefer!



Calling Methods During Iteration
Type TPlayer
    Field name:String
    Field score:Int

    Method AddScore(points:Int)
        this\score = this\score + points
    EndMethod

    Method ShowInfo()
        Print this\name & ": " & ToString(this\score) & " points"
    EndMethod
EndType

Function Main()
    ; Create players
    Local p1:TPlayer = Create TPlayer
    p1\name = "Alice"
    p1\score = 100

    Local p2:TPlayer = Create TPlayer
    p2\name = "Bob"
    p2\score = 250

    Local p3:TPlayer = Create TPlayer
    p3\name = "Charlie"
    p3\score = 175

    ; Give everyone bonus points
    Print "=== Adding Bonus Points ==="
    Local player:TPlayer
    For player EachIn TPlayer
        player\AddScore(50)
    Next

    ; Show results
    Print "=== Updated Scores ==="
    For player EachIn TPlayer
        player\ShowInfo()
    Next

    Return False
EndFunction

Output:

=== Adding Bonus Points ===
=== Updated Scores ===
Alice: 150 points
Bob: 300 points
Charlie: 225 points


Counting Instances
Function CountEnemies:Int()
    Local count:Int = 0
    Local enemy:TEnemy

    For enemy EachIn TEnemy
        count = count + 1
    Next

    Return count
EndFunction

Function Main()
    ; Create enemies
    Local e1:TEnemy = Create TEnemy
    Local e2:TEnemy = Create TEnemy
    Local e3:TEnemy = Create TEnemy

    Print "Total enemies: " & ToString(CountEnemies())  ; Output: 3

    ; Remove one
    Remove e2

    Print "After removal: " & ToString(CountEnemies())  ; Output: 2

    Return False
EndFunction


Removing During Iteration

Warning: Be careful when removing instances during iteration. The safe way is to mark them for removal:

Type TBullet
    Field x:Int
    Field y:Int
    Field isActive:Int

    Method Update()
        If this\isActive
            this\x = this\x + 5

            ; Mark as inactive if off-screen
            If this\x > 800
                this\isActive = False
            EndIf
        EndIf
    EndMethod
EndType

Function UpdateBullets()
    Local bullet:TBullet

    ; Update all bullets
    For bullet EachIn TBullet
        bullet\Update()
    Next

    ; Remove inactive bullets (separate pass)
    For bullet EachIn TBullet
        If bullet\isActive = False
            Remove bullet
        EndIf
    Next
EndFunction

Function Main()
    ; Create bullets
    Local b1:TBullet = Create TBullet
    b1\x = 700
    b1\isActive = True

    Local b2:TBullet = Create TBullet
    b2\x = 750
    b2\isActive = True

    Local b3:TBullet = Create TBullet
    b3\x = 100
    b3\isActive = True

    Print "Bullets before update: 3"

    UpdateBullets()

    ; Count remaining
    Local count:Int = 0
    Local b:TBullet
    For b EachIn TBullet
        count = count + 1
    Next

    Print "Bullets after update: " & ToString(count)  ; Should be 1

    Return False
EndFunction


Finding Specific Instances
Type TNpc
    Field id:Int
    Field name:String
    Field health:Int
EndType

Function FindNpcByName:TNpc(searchName:String)
    Local npc:TNpc

    For npc EachIn TNpc
        If npc\name = searchName
            Return npc  ; Found it!
        EndIf
    Next

    Return Null  ; Not found
EndFunction

Function FindNpcById:TNpc(searchId:Int)
    Local npc:TNpc

    For npc EachIn TNpc
        If npc\id = searchId
            Return npc
        EndIf
    Next

    Return Null
EndFunction

Function Main()
    ; Create NPCs
    Local npc1:TNpc = Create TNpc
    npc1\id = 1
    npc1\name = "Guard"
    npc1\health = 100

    Local npc2:TNpc = Create TNpc
    npc2\id = 2
    npc2\name = "Merchant"
    npc2\health = 50

    Local npc3:TNpc = Create TNpc
    npc3\id = 3
    npc3\name = "Blacksmith"
    npc3\health = 80

    ; Find by name
    Local found:TNpc = FindNpcByName("Merchant")
    If found <> Null
        Print "Found: " & found\name & " (ID: " & ToString(found\id) & ")"
    Else
        Print "NPC not found"
    EndIf

    ; Find by ID
    Local smithFound:TNpc = FindNpcById(3)
    If smithFound <> Null
        Print "Found: " & smithFound\name
    EndIf

    Return False
EndFunction


Processing Subsets
Type TUnit
    Field team:Int      ; 0 = enemy, 1 = ally
    Field health:Int
    Field name:String
EndType

Function DamageEnemies(amount:Int)
    Local unit:TUnit

    For unit EachIn TUnit
        If unit\team = 0  ; Only enemies
            unit\health = unit\health - amount
            Print unit\name & " takes " & ToString(amount) & " damage!"

            If unit\health <= 0
                Print unit\name & " defeated!"
            EndIf
        EndIf
    Next
EndFunction

Function HealAllies(amount:Int)
    Local unit:TUnit

    For unit EachIn TUnit
        If unit\team = 1  ; Only allies
            unit\health = unit\health + amount
            Print unit\name & " healed for " & ToString(amount) & " HP"
        EndIf
    Next
EndFunction

Function Main()
    ; Create mixed units
    Local enemy1:TUnit = Create TUnit
    enemy1\team = 0
    enemy1\name = "Goblin"
    enemy1\health = 50

    Local ally1:TUnit = Create TUnit
    ally1\team = 1
    ally1\name = "Knight"
    ally1\health = 100

    Local enemy2:TUnit = Create TUnit
    enemy2\team = 0
    enemy2\name = "Orc"
    enemy2\health = 80

    Print "=== Area Attack ==="
    DamageEnemies(30)

    Print ""
    Print "=== Healing Spell ==="
    HealAllies(25)

    Return False
EndFunction


Empty Lists

If no instances exist, the loop simply doesn't execute:

Function Main()
    ; No enemies created yet
    Local enemy:TEnemy

    Print "Checking for enemies..."

    For enemy EachIn TEnemy
        Print "Found: " & enemy\name
    Next

    Print "Search complete"  ; This will print even with no enemies

    Return False
EndFunction

Output:

Checking for enemies...
Search complete


Key Points

See Also

Examples

See the TypeIteration.bam example for complete demonstrations.


BambooBasic © 2026 Michael Denathorn