Type Instances

Every Type you define acts as a blueprint. To use it, you must create instances (objects) of that Type. BambooBasic automatically manages a list of all instances for each Type.



Creating Instances

Use the Create keyword to create a new instance of a Type:

Type TEnemy
    Field name:String
    Field health:Int
EndType

Function Main()
    ; Create an instance
    Local goblin:TEnemy = Create TEnemy

    ; Set its fields
    goblin\name = "Goblin Warrior"
    goblin\health = 50

    Print goblin\name & " created with " & ToString(goblin\health) & " HP"

    Return False
EndFunction

What happens:



The Instance List

BambooBasic automatically maintains a linked list of all instances for each Type. When you create an instance, it's added to this list. This enables powerful iteration features.

Function Main()
    ; Creating instances automatically adds them to the Type's list
    Local enemy1:TEnemy = Create TEnemy
    enemy1\name = "Goblin"

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

    Local enemy3:TEnemy = Create TEnemy
    enemy3\name = "Troll"

    ; All three instances are now in TEnemy's internal list
    ; You can iterate over them (see Type Iteration documentation)

    Return False
EndFunction


Removing Instances

Use the Remove keyword to delete an instance and free its memory:

Function Main()
    Local temp:TEnemy = Create TEnemy
    temp\name = "Temporary Enemy"
    temp\health = 100

    Print "Created: " & temp\name

    ; Delete the instance
    Remove temp

    ; temp is now invalid - DO NOT use it!

    Return False
EndFunction

What happens:

Important: After removing an instance, the variable points to freed memory. Don't access it!



Null Instances

Type variables can be Null, meaning they don't point to any instance:

Function Main()
    ; Create uninitialized variable
    Local player:TEnemy = Null

    ; Check before using
    If player = Null
        Print "No player instance exists"
        player = Create TEnemy  ; Now create it
    EndIf

    ; Safe to use now
    player\name = "Hero"
    Print player\name

    Return False
EndFunction

Always check for Null when receiving Type variables from functions or methods that might not return a valid instance.



Instance Lifecycle Example
Type TProjectile
    Field x:Int
    Field y:Int
    Field velocityX:Int
    Field velocityY:Int
    Field damage:Int
    Field isActive:Int

    Method Initialize(startX:Int, startY:Int, vx:Int, vy:Int)
        this\x = startX
        this\y = startY
        this\velocityX = vx
        this\velocityY = vy
        this\damage = 10
        this\isActive = True
    EndMethod

    Method Update()
        If this\isActive
            this\x = this\x + this\velocityX
            this\y = this\y + this\velocityY

            ; Deactivate if off-screen
            If this\x < 0 Or this\x > 800 Or this\y < 0 Or this\y > 600
                this\isActive = False
            EndIf
        EndIf
    EndMethod
EndType

Function FireProjectile:TProjectile(x:Int, y:Int, vx:Int, vy:Int)
    ; Create and initialize a projectile
    Local proj:TProjectile = Create TProjectile
    proj\Initialize(x, y, vx, vy)
    Print "Projectile fired from (" & ToString(x) & ", " & ToString(y) & ")"
    Return proj
EndFunction

Function Main()
    ; Fire some projectiles
    Local bullet1:TProjectile = FireProjectile(100, 100, 5, 0)
    Local bullet2:TProjectile = FireProjectile(100, 100, -3, 4)
    Local bullet3:TProjectile = FireProjectile(100, 100, 0, -5)

    Print "=== Projectiles Created ==="
    Print "Total active: 3"

    ; Simulate a few updates
    For i:Int = 0 To 200
        bullet1\Update()
        bullet2\Update()
        bullet3\Update()
    Next

    Print "=== After 200 Updates ==="
    Print "Bullet 1 active: " & ToString(bullet1\isActive)
    Print "Bullet 2 active: " & ToString(bullet2\isActive)
    Print "Bullet 3 active: " & ToString(bullet3\isActive)

    ; Clean up
    Remove bullet1
    Remove bullet2
    Remove bullet3

    Print "All projectiles removed"

    Return False
EndFunction


Removing All Instances

You can iterate through all instances of a Type and remove them (see Type Iteration documentation for details):

Function CleanupAllEnemies()
    Local enemy:TEnemy

    ; Iterate and remove all
    For enemy EachIn TEnemy
        Print "Removing: " & enemy\name
        Remove enemy
    Next

    Print "All enemies removed"
EndFunction


When to Remove Instances

Remove instances when:

Example: Enemy Death

Type TEnemy
    Field name:String
    Field health:Int

    Method TakeDamage:Int(amount:Int)
        this\health = this\health - amount
        If this\health <= 0
            Print this\name & " has been defeated!"
            Return True  ; Signal that enemy should be removed
        EndIf
        Return False
    EndMethod
EndType

Function DamageEnemy(enemy:TEnemy, amount:Int)
    If enemy <> Null
        If enemy\TakeDamage(amount)
            ; Enemy died, remove it
            Remove enemy
            enemy = Null  ; Mark as removed
        EndIf
    EndIf
EndFunction

Function Main()
    Local orc:TEnemy = Create TEnemy
    orc\name = "Orc"
    orc\health = 50

    Print orc\name & " has " & ToString(orc\health) & " HP"

    ; Attack the orc
    DamageEnemy(orc, 30)
    If orc <> Null
        Print orc\name & " has " & ToString(orc\health) & " HP remaining"
    EndIf

    ; Finish it off
    DamageEnemy(orc, 25)
    If orc = Null
        Print "Orc has been removed"
    EndIf

    Return False
EndFunction


Memory Management

BambooBasic handles memory management automatically:

Good Practice:

Function ProcessLevel()
    ; Create instances for this level
    Local boss:TEnemy = Create TEnemy
    boss\name = "Boss"
    boss\health = 1000

    ; ... game logic ...

    ; Clean up before exiting
    Remove boss

    Print "Level complete, memory cleaned up"
EndFunction


Key Points

See Also

Examples

See the Types Examples folder for complete demonstrations.


BambooBasic © 2026 Michael Denathorn