There are many different ways to create and/or access files. The file paths and the ways your access these files varies depending on the type of file and the location of the file.
Defold provides several different functions to work with files:
io.*
functions to read and write files. These functions give you very fine-grained control over the entire I/O process.os.rename()
and os.remove()
to rename and remove files.sys.save()
and sys.load()
to read and write Lua tables. Additional sys.*
functions exist to help with platform independent file path resolution.File and folder locations can be divided into three categories:
When saving and loading application specific files such as high scores, user settings and game state it is recommended to do so in a location provided by the operating system and intended specifically for this purpose. You can use sys.get_save_file()
to get the OS specific absolute path to a file. Once you have the absolute path you can use the sys.*
, io.*
and os.*
functions (see above).
Check the example showing how to use sys.save() and sys.load().
You can bundle files with your application in two ways:
CUSTOM RESOURCES - As part of the game archive using the Custom Resources field in game.project. You can read these files using sys.load_resource()
. Note that these aren’t actual files on the file system. Files included this way becomes part of the binary game archive and the only way to access them is through sys.load_resource()
.
BUNDLE RESOURES - As additional files and folders located as a part of your application bundle using the Bundle Resources field in game.project. You can use sys.get_application_path()
to get the path to where the application is stored. Use this application base path to create the final absolute path to the files you need access to. Once you have the absolute path of these files you can use the io.*
and os.*
functions to access the files (see above).
For security reasons browsers (and by extension any JavaScript running in a browser) is prevented from accessing system files. File operations in HTML5 builds in Defold still work, but only on a “virtual file system” using the IndexedDB API in the browser. What this means is that there is no way to access bundle resources using io.*
or os.*
functions. You can however access bundle resources using http.request()
.
Characteristic | Custom Resources | Bundle Resources |
---|---|---|
Loading speed | Faster - files loaded from binary archive | Slower - files loaded from filesystem |
Load partial files | No - only entire files | Yes - read arbitrary bytes from file |
Modify files after bundling | No - files stored inside a binary archive | Yes - files stored on the local file system |
HTML5 support | Yes | Yes - but access through http and not file I/O |
Access to system files may be restricted by the operating system for security reasons. You can use the extension-directiories
native extension to get the absolute path to some common system directories (ie documents, resource, temp). Once you have the absolute path of these files you can use the io.*
and os.*
functions to access the files (see above).
For security reasons browsers (and by extension any JavaScript running in a browser) is prevented from accessing system files. File operations in HTML5 builds in Defold still work, but only on a “virtual file system” using the IndexedDB API in the browser. What this means is that there is no way to access system files in HTML5 builds.
The Asset Portal contains several assets to simplify file and folder access. Some examples:
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB