Function OpenFile

OpenFile(extension)
Toy Basic does not provide direct access to file system for security reasons, though it allows you to save and open files with user interaction using system File Open and File Save dialogs. You can use function OpenFile to load content of a file with desired extension as a string. You will not know the actual file name, it will be provided by user. The content will be returned by the function. In case of a failure or if dialog is cancelled by user, the return value is null. In the example below you can see a small graphic editor with possibility to save and open images as PNG files:
function OnMouseUp(x, y)
	DrawCircle(x, y, 5)
end function

function OnKeyboard(c)
	if c="s"
		SaveFile(Screenshot(0, 0, GetWidth(), GetHeight()), "png")
	elseif c="o"
		img = OpenFile("png")
		if not IsNull(img)
			ClearScreen()
			DrawImage(0, 0, img)
		end if
	elseif c="c"
		ClearScreen()
	end if
end function

See also: