Type Methods

Methods are functions that belong to a Type. They can access and modify the Type's fields using the this\ keyword.



Defining Methods

Methods are defined inside a Type using the Method keyword:

Type TPlayer
    Field name:String
    Field health:Int
    Field maxHealth:Int

    Method Setup(playerName:String)
        this\name = playerName
        this\health = 100
        this\maxHealth = 100
    EndMethod

    Method TakeDamage(amount:Int)
        this\health = this\health - amount
        If this\health < 0
            this\health = 0
        EndIf
    EndMethod

    Method IsAlive:Int()
        If this\health > 0
            Return True
        Else
            Return False
        EndIf
    EndMethod
EndType


The this\ Keyword

Inside a method, use this\ to access the current instance's fields:

Type TCounter
    Field value:Int

    Method Increment()
        this\value = this\value + 1  ; Access field of THIS instance
    EndMethod

    Method GetValue:Int()
        Return this\value  ; Return field of THIS instance
    EndMethod
EndType

Why this\? It distinguishes between the instance's fields and method parameters/local variables.



Calling Methods

Call methods using the backslash operator, just like accessing fields:

Function Main()
    Local player:TPlayer = Create TPlayer

    ; Call method to initialize
    player\Setup("Hero")

    ; Call method to modify state
    player\TakeDamage(25)

    ; Call method that returns a value
    If player\IsAlive()
        Print player\name & " is still alive!"
        Print "Health: " & ToString(player\health)
    Else
        Print player\name & " has died!"
    EndIf

    Return False
EndFunction


Methods with Parameters
Type TCharacter
    Field x:Int
    Field y:Int

    Method MoveTo(newX:Int, newY:Int)
        this\x = newX
        this\y = newY
    EndMethod

    Method MoveBy(deltaX:Int, deltaY:Int)
        this\x = this\x + deltaX
        this\y = this\y + deltaY
    EndMethod

    Method GetDistance:Double(otherX:Int, otherY:Int)
        Local dx:Int = this\x - otherX
        Local dy:Int = this\y - otherY
        ; Calculate distance using Pythagorean theorem
        Return Sqrt(dx * dx + dy * dy)
    EndMethod
EndType

Function Main()
    Local hero:TCharacter = Create TCharacter

    ; Set initial position
    hero\MoveTo(10, 20)
    Print "Position: (" & ToString(hero\x) & ", " & ToString(hero\y) & ")"

    ; Move relative
    hero\MoveBy(5, -3)
    Print "New Position: (" & ToString(hero\x) & ", " & ToString(hero\y) & ")"

    ; Calculate distance to origin
    Local dist:Double = hero\GetDistance(0, 0)
    Print "Distance from origin: " & ToString(dist)

    Return False
EndFunction


Methods with Return Values

Methods can return values just like functions:

Type TInventory
    Field gold:Int
    Field items:Int

    Method AddGold(amount:Int)
        this\gold = this\gold + amount
    EndMethod

    Method CanAfford:Int(cost:Int)
        If this\gold >= cost
            Return True
        Else
            Return False
        EndIf
    EndMethod

    Method Purchase:Int(itemCost:Int)
        If this\CanAfford(itemCost)
            this\gold = this\gold - itemCost
            this\items = this\items + 1
            Return True  ; Success
        Else
            Return False  ; Not enough gold
        EndIf
    EndMethod

    Method GetTotal:Int()
        Return this\gold + (this\items * 10)
    EndMethod
EndType

Function Main()
    Local inv:TInventory = Create TInventory
    inv\AddGold(100)

    Print "Gold: " & ToString(inv\gold)

    ; Try to purchase
    If inv\Purchase(50)
        Print "Purchase successful!"
        Print "Gold remaining: " & ToString(inv\gold)
        Print "Items: " & ToString(inv\items)
    Else
        Print "Not enough gold!"
    EndIf

    ; Get total value
    Local total:Int = inv\GetTotal()
    Print "Total value: " & ToString(total)

    Return False
EndFunction


Methods Calling Other Methods

Methods can call other methods of the same instance:

Type TGameObject
    Field x:Int
    Field y:Int
    Field isActive:Int

    Method Activate()
        this\isActive = True
        this\ResetPosition()  ; Call another method
    EndMethod

    Method Deactivate()
        this\isActive = False
    EndMethod

    Method ResetPosition()
        this\x = 0
        this\y = 0
    EndMethod

    Method Update()
        If this\isActive
            this\x = this\x + 1
            If this\x > 100
                this\Deactivate()  ; Call another method
            EndIf
        EndIf
    EndMethod
EndType


Complete Example: RPG Character
Type TRPGCharacter
    Field name:String
    Field health:Int
    Field maxHealth:Int
    Field mana:Int
    Field maxMana:Int
    Field level:Int
    Field experience:Int

    Method Initialize(charName:String)
        this\name = charName
        this\level = 1
        this\experience = 0
        this\SetMaxStats()
        this\FullHeal()
    EndMethod

    Method SetMaxStats()
        this\maxHealth = 50 + (this\level * 10)
        this\maxMana = 30 + (this\level * 5)
    EndMethod

    Method FullHeal()
        this\health = this\maxHealth
        this\mana = this\maxMana
    EndMethod

    Method TakeDamage(amount:Int)
        this\health = this\health - amount
        If this\health < 0 Then this\health = 0

        If this\health = 0
            Print this\name & " has been defeated!"
        EndIf
    EndMethod

    Method CastSpell:Int(manaCost:Int)
        If this\mana >= manaCost
            this\mana = this\mana - manaCost
            Return True
        Else
            Print "Not enough mana!"
            Return False
        EndIf
    EndMethod

    Method GainExperience(amount:Int)
        this\experience = this\experience + amount
        Print this\name & " gained " & ToString(amount) & " XP"

        ; Check for level up (100 XP per level)
        If this\experience >= this\level * 100
            this\LevelUp()
        EndIf
    EndMethod

    Method LevelUp()
        this\level = this\level + 1
        Print this\name & " leveled up to level " & ToString(this\level) & "!"
        this\SetMaxStats()
        this\FullHeal()
    EndMethod

    Method ShowStats()
        Print "=== " & this\name & " ==="
        Print "Level: " & ToString(this\level)
        Print "HP: " & ToString(this\health) & "/" & ToString(this\maxHealth)
        Print "MP: " & ToString(this\mana) & "/" & ToString(this\maxMana)
        Print "XP: " & ToString(this\experience) & "/" & ToString(this\level * 100)
    EndMethod
EndType

Function Main()
    Local hero:TRPGCharacter = Create TRPGCharacter
    hero\Initialize("Aragorn")
    hero\ShowStats()

    Print ""
    Print "=== Combat ==="
    hero\TakeDamage(20)
    Print "After damage - HP: " & ToString(hero\health)

    Print ""
    If hero\CastSpell(15)
        Print "Spell cast! Mana: " & ToString(hero\mana)
    EndIf

    Print ""
    Print "=== Gaining Experience ==="
    hero\GainExperience(80)
    hero\GainExperience(30)  ; Should trigger level up

    Print ""
    hero\ShowStats()

    Remove hero
    Return False
EndFunction


Key Points

See Also

Examples

See the Types Examples folder for complete demonstrations.


BambooBasic © 2026 Michael Denathorn