SecondBASIC Documentation v3

Home / Examples & Tutorials / $Include - making your own libraries

$Include - making your own libraries

The $Include command includes another SecondBASIC code file into your program. One of the main uses for the $Include command is to create your own custom library that you can include into your programs. This allows one location for the code, and if there's any updates to it, you don't have to update every program that utilizes the library.

SecondBASIC handles included files in the following order:

The main thing to remember when using external files is to keep your variable names, function names, sub-routine names, and line labels unique to the external file(s). For example, if you use the variable names x and y in both your main program and in the external file, you may cause unexpected results from different functions updating the same variables.

Let's get to some examples of using the $Include command.

    ' IncludeFile.sbs
Declare Sub AltPrint(text$ As String, x As Integer, y As Integer)
    ASCIIStart = 0 ' Where the ASCII table is loaded in VRAM
    For i = 0 To Len(text$)
        char$ = Mid$(text$,i,1)
        ascii = Asc(char$)+ASCIIStart
        ascii+=ASCIIStart
        DrawTile ascii,x,y
        x++
    Next
End Sub

The above is the contents of the file "IncludeFile.sbs". To use this in another project, you would just $Include the file and then call the routines as if they were in your main program.

    ' Main.sbs

    $Include "IncludeFile.sbs"
    AltPrint "Hello World!",10, 1

And that's all there is to it. You may want to use paths, especially if the library/external files aren't in the same project folder.