Resources

Resources are data files that accompany a program. When a program is compiled, resources are embedded into the program code and you do not need to distribute them together with the program.

It is very easy to use resources in Toy Basic programs. If you have a file in the same folder as your BAS file, you can assign its contents to a string variable by using special character # and the file name as a string value. For example if you have a file "test.txt" next to your BAS file, you can print its contents like this:

$Text = #"test.txt"
DrawText (10, 10, $Text)
You proably noticed that we used a global variable $Text, not a local one. We did this for a reason. You can use local variables, but keep in mind, that resource files can be pretty big, and it is not recommended to copy them or read a multiple times. The example below will work fine, but it is not optimal. Every time you call function DrawText, the resource contents is copied to a new local variable:
function DrawTextFile(x, y)
	Text = #"test.txt"
	DrawText (10, 10, Text)
end function
DrawTextFile (10, 10)
DrawTextFile (10, 20)
It is much more efficient to assign a resource to a global variable and use it everywhere you want:
$Text = #"test.txt"
function DrawTextFile(x, y)
	DrawText (10, 10, $Text)
end function
DrawTextFile (10, 10)
DrawTextFile (10, 20)

Very often resources are used to draw images:
$Pony = #"pony.png"
DrawImage (10, 10, $Pony)
Or play sounds:
$Tone = #"tone.mp3"
PlaySound (10, 10, $Tone)

You can aslo use asolute paths to resource files:

$Pony = #"c:\My Documents\My Programs\pony.png"

See also: