Include - Including Other Files

The Include keyword allows you to split your code across multiple BambooBasic files and combine them at compile time. This helps organize large projects and enables code reuse.



Basic Syntax

Use Include "filename.bam" to include another BambooBasic file:

Include "filename.bam"

The included file's contents are inserted at the point where the Include statement appears.



Simple Example

Main file: Main.bam

; Include helper functions from another file
Include "Helpers.bam"

Function Main()
    ; Call function defined in Helpers.bam
    GreetUser("Alice")

    Return False
EndFunction

Included file: Helpers.bam

Function GreetUser(name:String)
    Print "Hello, " & name & "!"
EndFunction

Function FarewellUser(name:String)
    Print "Goodbye, " & name & "!"
EndFunction

When you compile Main.bam, the compiler automatically includes Helpers.bam, making all its functions available.



Complete Example

Main file: Include.bam

; INCLUDE Example
Include "Extra.bam"

Function Main()
    ; Declared in "Extra.bam"
    MyFunction()

    Print "Hello"
    Return False
EndFunction

Included file: Extra.bam

Function MyFunction()
    Print "Hello from another file"
EndFunction

Output:

Hello from another file
Hello


Multiple Includes

You can include as many files as needed:

Include "Math.bam"
Include "Graphics.bam"
Include "Input.bam"
Include "Player.bam"

Function Main()
    ; Use functions from all included files
    InitGraphics()
    CreatePlayer()

    Local result:Int = Add(10, 20)
    Print "Result: " & ToString(result)

    Return False
EndFunction


Organizing Your Project

Use Include to organize your project into logical modules:

; Game.bam - Main file
Include "Config.bam"      ; Game configuration
Include "Types.bam"       ; Custom Types
Include "Player.bam"      ; Player management
Include "Enemy.bam"       ; Enemy AI
Include "Collision.bam"   ; Collision detection
Include "Render.bam"      ; Rendering code

Function Main()
    InitGame()
    GameLoop()
    Cleanup()
    Return False
EndFunction

Types.bam:

Type TPlayer
    Field x:Int
    Field y:Int
    Field health:Int
EndType

Type TEnemy
    Field x:Int
    Field y:Int
    Field damage:Int
EndType

Player.bam:

Function CreatePlayer:TPlayer()
    Local p:TPlayer = Create TPlayer
    p\\x = 100
    p\\y = 100
    p\\health = 100
    Return p
EndFunction

Function UpdatePlayer(p:TPlayer)
    ; Player update logic
EndFunction


File Paths

Include paths are relative to the directory containing the main .bam file:

; Same directory
Include "Helpers.bam"

; Subdirectory
Include "utils/Math.bam"

; Parent directory (not recommended)
Include "../Shared.bam"

Best Practice: Keep included files in the same directory or subdirectories for portability.



What Gets Included

When you include a file, everything in that file becomes part of your program:



Nested Includes

Included files can themselves include other files:

; Main.bam
Include "Level1.bam"

; Level1.bam
Include "Common.bam"
Include "Enemies.bam"

; Common.bam
Include "Types.bam"
Include "Utils.bam"

Note: Be careful to avoid circular includes (A includes B, B includes A).



Include vs Import
; Include BambooBasic code
Include "MyCode.bam"

; Import external DLL functions
Import "BBRuntimeLinux.decls"


Common Use Cases

1. Separating Game Components:

Include "Player.bam"
Include "Enemy.bam"
Include "Powerup.bam"

2. Reusable Utilities:

Include "MathHelpers.bam"
Include "StringHelpers.bam"
Include "FileHelpers.bam"

3. Configuration:

Include "Config.bam"  ; Contains game constants and settings

4. Type Definitions:

Include "GameTypes.bam"  ; All Type definitions in one place


Key Points

See Also

Examples

See the Include Example for a complete demonstration.


BambooBasic © 2026 Michael Denathorn