Custom Types

Types allow you to define your own data structures with fields and methods. Think of them as blueprints for creating objects.



Defining a Type

Types are defined using the Type keyword and must start with the letter T:

Type TPlayer
    Field name:String
    Field score:Int
    Field health:Double
EndType

Naming Convention: Type names must begin with T (e.g., TPlayer, TEnemy, TWeapon)



Fields

Fields are variables that belong to the Type. Each instance of the Type has its own copy of these fields.

Type TCharacter
    Field name:String       ; Character's name
    Field x:Int            ; X position
    Field y:Int            ; Y position
    Field isAlive:Int      ; 1 = alive, 0 = dead
EndType

Field Types: Fields can be any basic type (Int, Double, String) or another custom Type.



Creating Instances

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

Function Main()
    ; Create a new TPlayer instance
    Local player:TPlayer = Create TPlayer

    ; Set field values using backslash (\) operator
    player\name = "Alice"
    player\score = 100
    player\health = 75.5

    ; Read field values
    Print player\name & " has " & ToString(player\score) & " points"

    Return False
EndFunction

The Backslash Operator: Use \ to access fields and methods (e.g., player\name)



Multiple Instances

You can create multiple instances of the same Type, each with independent field values:

Function Main()
    Local player1:TPlayer = Create TPlayer
    player1\name = "Alice"
    player1\score = 100

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

    ; Each instance has its own field values
    Print player1\name & ": " & ToString(player1\score)  ; Alice: 100
    Print player2\name & ": " & ToString(player2\score)  ; Bob: 250

    Return False
EndFunction


Nested Types

Types can contain fields that are themselves other Types:

Type TWeapon
    Field name:String
    Field damage:Int
EndType

Type TPlayer
    Field name:String
    Field weapon:TWeapon
EndType

Function Main()
    ; Create weapon
    Local sword:TWeapon = Create TWeapon
    sword\name = "Steel Sword"
    sword\damage = 50

    ; Create player with weapon
    Local hero:TPlayer = Create TPlayer
    hero\name = "Arthur"
    hero\weapon = sword

    ; Access nested Type
    Print hero\name & " wields " & hero\weapon\name
    Print "Damage: " & ToString(hero\weapon\damage)

    Return False
EndFunction


Removing Instances

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

Function Main()
    Local temp:TPlayer = Create TPlayer
    temp\name = "Temporary"

    ; Use the instance
    Print temp\name

    ; Clean up when done
    Remove temp

    Return False
EndFunction

Important: After removing an instance, don't try to access it - the memory has been freed.



Null Checking

Type variables can be Null (not pointing to any instance). Always check before using:

Function Main()
    Local player:TPlayer = Null  ; Not initialized

    ; Check before using
    If player = Null
        Print "No player created yet!"
    Else
        Print player\name
    EndIf

    Return False
EndFunction


Complete Example
Type TEnemy
    Field name:String
    Field health:Int
    Field damage:Int
EndType

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

    Local orc:TEnemy = Create TEnemy
    orc\name = "Orc"
    orc\health = 100
    orc\damage = 15

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

    ; Display enemy stats
    Print "=== Enemies ==="
    Print goblin\name & " - HP: " & ToString(goblin\health) & " DMG: " & ToString(goblin\damage)
    Print orc\name & " - HP: " & ToString(orc\health) & " DMG: " & ToString(orc\damage)
    Print dragon\name & " - HP: " & ToString(dragon\health) & " DMG: " & ToString(dragon\damage)

    ; Clean up
    Remove goblin
    Remove orc
    Remove dragon

    Return False
EndFunction


Key Points

See Also

Examples

See the Types Examples folder for complete demonstrations.


BambooBasic © 2026 Michael Denathorn