Filesystem
This section contains documentation of functions and properties that allow you to check the existance of / delete a file, create / enumerate directories, and in other ways interact with and/or manipulate the filesystem.
If you wish to specifically read and write files, you should look at the file datastream class.
Classes
path
A path to a file or directory, which can be split into and built from its device, directories, filename and extension, and converted between the styles of different operating systems.
path();
path(bool absolute);
path(const string&in path);
path(const string&in path, spec::path_style style);
path(const spec::path&in other);
path(const spec::path&in parent, const string&in filename);
path(const spec::path&in parent, const spec::path&in relative);
Arguments:
bool absolute (2): whether the empty path is absolute.
const string&in path (3, 4): the path to parse.
spec::path_style style (4): how to parse the path, a value of the path_style enum.
const spec::path&in other (5): a path to copy.
const spec::path&in parent (6, 7): a directory to start from.
const string&in filename (6): a filename to put in parent.
const spec::path&in relative (7): a relative path to resolve against parent.
Remarks:
This class is in the spec namespace, so scripts write it as spec::path unless they have a using namespace spec; statement.
A path is made up of an optional node (for UNC paths on Windows), an optional device (such as a drive letter), a list of directories and an optional filename. A path whose string ends with a separator, such as sounds/, is a directory with no filename, while sounds/door is a file named door in the sounds directory, however it exists on disk. This class only deals with the text of paths, it never looks at the filesystem, so nothing has to exist.
Paths are parsed in the style of the operating system NVGT is running on unless a path_style is given, and to_string() can produce any style. On Windows both forward and back slashes are accepted.
A path converts to a string implicitly, and / joins two paths, so that spec::path("sounds") / "door.ogg" is sounds/door.ogg (or sounds\door.ogg on Windows). If the right side is absolute it is returned unchanged.
p[i] returns directory i, counting from 0, and an index equal to depth returns the filename.
Many methods return the path itself, so calls can be chained, such as p.make_directory().push_directory("music").
Example:
void main() {
spec::path p("C:/Users/Sam/Documents/My Game/sounds/door_open.ogg", spec::PATH_STYLE_WINDOWS);
string info = "device: " + p.device + "\ndepth: " + p.depth + "\nfilename: " + p.filename + "\nbasename: " + p.basename + "\nextension: " + p.extension + "\nparent: " + p.parent.to_string(spec::PATH_STYLE_WINDOWS);
for (int i = 0; i < p.depth; i++) info += "\ndirectory " + i + ": " + p[i];
p.set_extension("wav").pop_directory().push_directory("music");
info += "\nchanged: " + p.to_string(spec::PATH_STYLE_UNIX);
spec::path joined = spec::path("data") / "levels" / "1.json";
info += "\njoined: " + joined;
alert("Path", info);
}
Methods
absolute
Get an absolute version of the path, leaving this one unchanged.
spec::path path::absolute() const;spec::path path::absolute(const spec::path&in base) const;
Arguments:
- const spec::path&in base (2): the absolute path to resolve against. Without it, the current working directory is used.
Returns:
spec::path: the absolute path, or a copy of this path if it is already absolute.
append
Add a relative path to the end of this one.
spec::path& path::append(const spec::path&in path);
Arguments:
- const spec::path&in path: the path to add, whose directories and filename follow this path's directories.
Returns:
spec::path&: this path, so calls can be chained.
Remarks:
The / operator does the same without changing either path.
assign
Replace the path by parsing a string, or copying another path.
spec::path& path::assign(const string&in path);spec::path& path::assign(const string&in path, spec::path_style style);spec::path& path::assign(const spec::path&in other);
Arguments:
- const string&in path (1, 2): the path to parse.
- spec::path_style style (2): how to parse the path, a value of the path_style enum.
- const spec::path&in other (3): the path to copy.
Returns:
spec::path&: this path, so calls can be chained.
Remarks:
An exception is thrown if the string isn't a valid path. parse() returns false instead.
assign_directory
Replace the path with a directory, treating the string as a directory even if it doesn't end with a separator.
spec::path& path::assign_directory(const string&in directory);spec::path& path::assign_directory(const string&in directory, spec::path_style style);
Arguments:
- const string&in directory: the directory to parse.
- spec::path_style style (2): how to parse the directory, a value of the path_style enum.
Returns:
spec::path&: this path, so calls can be chained.
clear
Clear every part of the path.
spec::path& path::clear();
Returns:
spec::path&: this path, so calls can be chained.
make_absolute
Make the path absolute by resolving it against a base path, if it is relative.
spec::path& path::make_absolute();spec::path& path::make_absolute(const spec::path&in base);
Arguments:
- const spec::path&in base (2): the absolute path to resolve against. Without it, the current working directory is used.
Returns:
spec::path&: this path, so calls can be chained.
make_directory
Turn the path into a directory, moving its filename, if any, onto the end of its directories.
spec::path& path::make_directory();
Returns:
spec::path&: this path, so calls can be chained.
make_file
Turn the path into a file, moving its last directory, if any, into its filename.
spec::path& path::make_file();
Returns:
spec::path&: this path, so calls can be chained.
make_parent
Change the path to its parent directory.
spec::path& path::make_parent();
Returns:
spec::path&: this path, so calls can be chained.
Remarks:
A file becomes the directory it is in, and a directory loses its last directory. A relative path with no directories left gets a .. directory.
parse
Replace the path by parsing a string, without throwing an exception if it isn't valid.
bool path::parse(const string&in path);bool path::parse(const string&in path, spec::path_style style);
Arguments:
- const string&in path: the path to parse.
- spec::path_style style (2): how to parse the path, a value of the path_style enum.
Returns:
bool: true if the path was valid, false otherwise, in which case the path is unchanged.
pop_directory
Remove the last directory of the path.
spec::path& path::pop_directory();
Returns:
spec::path&: this path, so calls can be chained.
pop_front_directory
Remove the first directory of the path.
spec::path& path::pop_front_directory();
Returns:
spec::path&: this path, so calls can be chained.
push_directory
Add a directory to the end of the path's directories.
spec::path& path::push_directory(const string&in directory_name);
Arguments:
- const string&in directory_name: the directory to add.
.is ignored and..removes the last directory if there is one.
Returns:
spec::path&: this path, so calls can be chained.
resolve
Resolve a path against this one, which replaces this path if it is absolute, or is appended to it if it is relative.
spec::path& path::resolve(const spec::path&in path);
Arguments:
- const spec::path&in path: the path to resolve.
Returns:
spec::path&: this path, so calls can be chained.
set_basename
Set the filename of the path without changing its extension.
spec::path& path::set_basename(const string&in basename);
Arguments:
- const string&in basename: the new name, without an extension.
Returns:
spec::path&: this path, so calls can be chained.
set_device
Set the device of the path, such as a Windows drive letter.
spec::path& path::set_device(const string&in device);
Arguments:
- const string&in device: the device to set, such as
C. A non-empty device makes the path absolute.
Returns:
spec::path&: this path, so calls can be chained.
Remarks:
The current device can be read with the device property.
set_extension
Set the extension of the path's filename.
spec::path& path::set_extension(const string&in extension);
Arguments:
- const string&in extension: the new extension, without the dot. An empty extension removes it.
Returns:
spec::path&: this path, so calls can be chained.
set_filename
Set the filename of the path, including its extension.
spec::path& path::set_filename(const string&in filename);
Arguments:
- const string&in filename: the filename to set. An empty filename makes the path a directory.
Returns:
spec::path&: this path, so calls can be chained.
set_node
Set the node of the path, the server name in a Windows UNC path such as \\server\share.
spec::path& path::set_node(const string&in node);
Arguments:
- const string&in node: the node to set. A non-empty node makes the path absolute.
Returns:
spec::path&: this path, so calls can be chained.
Remarks:
The current node can be read with the node property.
to_string
Get the path as a string in a particular style.
string path::to_string(spec::path_style style = spec::PATH_STYLE_NATIVE) const;
Arguments:
- spec::path_style style = spec::PATH_STYLE_NATIVE: the style to write the path in, a value of the path_style enum.
Returns:
string: the path in the given style.
Remarks:
Converting a path to a string implicitly, such as by concatenating it, uses the native style.
Properties
basename
The filename of the path without its extension, so door_open for door_open.ogg.
const string path::basename;
Remarks:
Use set_basename() to change it.
depth
The number of directories in the path, not counting the filename.
const int path::depth;
device
The device of the path, such as the drive letter of a Windows path, or an empty string.
const string path::device;
Remarks:
Use set_device() to change it.
extension
The extension of the path's filename without the dot, such as ogg, or an empty string if it has none.
const string path::extension;
Remarks:
Use set_extension() to change it.
filename
The filename of the path, including its extension, or an empty string for a directory.
const string path::filename;
Remarks:
Use set_filename() to change it.
is_absolute
Whether the path is absolute, such as C:\games or /home/sam.
const bool path::is_absolute;
is_directory
Whether the path refers to a directory, meaning it has no filename.
const bool path::is_directory;
Remarks:
This is about the text of the path only, so sounds/ is a directory and sounds is a file named sounds, whatever is on disk.
is_file
Whether the path refers to a file, meaning it has a filename.
const bool path::is_file;
Remarks:
This is about the text of the path only, see is_directory.
is_relative
Whether the path is relative, such as sounds/door.ogg, meaning it starts from some other directory.
const bool path::is_relative;
node
The node of the path, the server name of a Windows UNC path such as \\server\share, or an empty string.
const string path::node;
Remarks:
Use set_node() to change it.
parent
The parent directory of the path, as a new path.
const spec::path path::parent;
Remarks:
See make_parent(), which changes the path itself.
vms_version
The version of a file in an OpenVMS path, which is empty on every other system.
const string path::vms_version;
Enums
path_style
The styles a spec::path can be parsed and written in, in the spec namespace, so written as spec::PATH_STYLE_UNIX and so on.
- PATH_STYLE_UNIX: Unix style paths, as on Linux, macOS, Android and iOS, such as
/home/sam/game. - PATH_STYLE_URI: the same as PATH_STYLE_UNIX, used for the paths of URIs.
- PATH_STYLE_WINDOWS: Windows style paths, such as
C:\Users\Sam\game. Forward slashes are accepted when parsing. - PATH_STYLE_VMS: OpenVMS style paths, such as
node::device:[dir1.dir2]file.ext;1. - PATH_STYLE_NATIVE: the style of the operating system NVGT is running on.
- PATH_STYLE_AUTO: guess the style when parsing, from the characters the path contains. This can't be used for writing a path.
Functions
directory_create
Creates a directory if it doesn't already exist.
bool directory_create(const string&in directory);
Arguments:
- const string&in directory: path to the directory to create (can be nested, relative or absolute).
Returns:
bool: true if the directory was successfully created or already exists, false otherwise.
Example:
void main() {
if (directory_exists("test")) {
alert("Info", "The test directory already exists; nothing to do");
exit();
}
if (directory_create("test")) {
alert("Info", "Directory created. Deleting...");
alert("Info", directory_delete("test") ? "Success": "Fail");
}
else
alert("Error", "Couldn't create the directory.");
}
directory_delete
Deletes a directory.
bool directory_delete(string directory);
Arguments:
- string directory: the directory to delete.
Returns:
bool: true if the directory was successfully deleted, false otherwise.
directory_exists
Query whether or not a directory exists.
bool directory_exists(const string&in directory);
Arguments:
- const string&in directory: the directory whose existence will be checked (can be a relative or absolute path).
Returns:
bool: true if the directory exists, false otherwise.
Example:
void main() {
alert("Directory exists", directory_exists("test") ? "There is a test folder in the current directory" : "There is not a test folder in the current directory");
}
file_copy
Coppies a file from one location to another.
bool file_copy(const string&in source_filename, const string&in destination_filename);
Arguments:
- const string&in source_filename: the path/name of the file to be coppied.
- const string&in destination_filename: the path (including filename) to copy the file to.
Returns:
bool: true if the file was successfully coppied, false otherwise.
file_delete
Deletes a file.
bool file_delete(const string&in filename);
Arguments:
- const string&in filename: the name of the file to delete.
Returns:
bool: true if the file was successfully deleted, false otherwise.
file_exists
Check for the existence of a particular file.
bool file_exists(const string&in file_path);
Arguments:
- const string&in file_path: the path to the file to query.
Returns:
bool: true if the file exists, false otherwise.
file_get_contents
Reads the entire content of a file and returns it as a string.
string file_get_contents(string filename)
Arguments:
- string filename: the name of the file to read.
Returns:
The contents of the file on success, an empty string on failure.
Remarks:
This is a convenience function, though it is currently the only way to read an Android asset. If you wish to access a file with more control, see the file datastream class.
Be careful to avoid reading files that are too large with this function, as the entire file will be in memory in order to return it as a continguous string.
Example:
void main() {
string filename = input_box("Filename", "Enter the name of a file to read.", "");
string contents = file_get_contents(filename);
if (contents == "")
alert("Example", "Either the file was not found, or it contained no text.");
else {
clipboard_set_text(contents);
alert("Example", "The contents of the file is now on your clipboard.");
}
}
file_get_date_created
Get a datetime object representing the creation date of a particular file.
datetime file_get_date_created(const string&in filename);
Arguments:
- const string&in filename; the name of the file to check.
Returns:
datetime: an initialized datetime object, with all its fields set to the creation date of the file.
Remarks:
The date returned is in UTC, not your local timezone.
Example:
void main() {
datetime dt = file_get_date_created("file_get_date_created.nvgt");
alert("file_get_date_created.nvgt was created on", dt.year + "-" + dt.month + "-" + dt.day + ", " + dt.hour +":" + dt.minute + ":" + dt.second);
}
file_get_date_modified
Get a datetime object representing the modification date of a particular file.
datetime file_get_date_modified(const string&in filename);
Arguments:
- const string&in filename: the name of the file to check.
Returns:
datetime: an initialized datetime object, with all its fields set to the modification date of the file.
Remarks:
The date returned is in UTC, not your local timezone.
Example:
void main() {
datetime dt = file_get_date_modified("file_get_date_modified.nvgt");
alert("file_get_date_modified.nvgt was modified on", dt.year + "-" + dt.month + "-" + dt.day + ", " + dt.hour +":" + dt.minute + ":" + dt.second);
}
file_get_size
Get the size of a file (in bytes).
int64 file_get_size(const string&in filename);
Arguments:
- const string&in filename: the name/path of the file to get the size of.
Returns:
int64: the size of the file (in bytes).
Example:
void main() {
alert("file_get.size.nvgt is", file_get_size("file_get_size.nvgt") + " bytes");
}
file_put_contents
Writes a string to a file.
bool file_put_contents(string filename, string content, bool append = false)
Arguments:
string filename: the name of the file to write to.
string content: the content to write.
bool append = false: specifies whether the current contents of the file should be overwritten when writing.
Returns:
bool: true on success, false on failure.
Example:
void main() {
if (!file_put_contents("example.txt", "This is an example"))
alert("Example", "Failed to write the file.");
else
alert("Example", "Successfully wrote the example file.");
}
get_preferences_path
Determines the best location to store user data for your game, creating the directory for you if necessary.
string get_preferences_path(string company_name, string product_name);
Arguments:
string company_name: The name of your organization, can be blank.
string product_name: The name of your game or application.
Remarks:
This is the recommended way to determine where your game should store application data. While other properties exist such as DIRECTORY_APPDATA which can be used to compose your own path if you desire, this function is generally more robust and platform agnostic than trying to prepare your own storage path using directory constants. This also frees you from needing to check for the existance of the directory before writing to it.
The company name and product name should never change for an application once you start using them, lest players lose access to their previous save data due to the directory changing names. The company name can be blank if you wish to only create one level of new directories in the users appdata, though this is generally not recommended to avoid clutter.
Both the company and product name are expected to contain only spaces and alphanumeric characters if you wish to be as platform agnostic as possible.
You should not assume that DIRECTORY_APPDATA + "/" + company_name + "/" + product_name will be the same as the directory returned by this function, or even that both the company and product names will be used all the time. On android, for example, this function always just returns the app's internal storage path.
This function is also registered as the alias DIRECTORY_PREFERENCES, for those who prefer the old style of directory constants naming.
Example:
void main() {
string path = get_preferences_path("", "documentation example");
alert("test", "data will be stored at " + path);
directory_delete(path); // In this example we don't actually want this directory to save.
}
glob
Return a list of files and directories on the filesystem given a path glob.
string[]@ glob(const string&in path_pattern, glob_options options = GLOB_DEFAULT);
Arguments:
const string&in path_pattern: The pattern to match files and directories with (Unix shell like, see remarks).
glob_options options: A bitwise of glob_options enum constants that influence the behavior of this function (see remarks).
Returns:
string[]@: A list of all matching files and directories that match the given path pattern, an empty array on no matches or failure.
Remarks:
This function provides for one of the easiest ways to enumerate the filesystem in NVGT, particularly because the path patterns provided can actually cause semi-recursive directory searches. The search starts at the current working directory unless an absolute path is given.
The glob patterns have simple rules:
path separators must be matched exactly, * will not cause a recursive lookup
* matches any sequence of characters
? matches any single character
[set] matches any characters between the brackets
[!set] matches any characters that are not listed between the brackets
\*, \[, \] etcexactly match a special character usually used as part of the glob expression
There is no guarantee that the items returned will appear in any particular order in the array.
The following glob_options constance are defined:
GLOB_DEFAULT: the default options
GLOB_IGNORE_HIDDEN: do not match when directory entries begin with a .
GLOB_FOLLOW_SYMLINKS: traverse even across symbolic links if the given pattern demands it
GLOB_CASELESS: match case insensitively
Example:
void main() {
// List all .nvgt files within the filesystem documentation directory.
string[]@ items = glob("../*/*.nvgt");
alert("files found", string(items));
}
Global Properties
DIRECTORY_APPDATA
Property that returns the user's roaming application directory, which is usually where game data can be written to.
const string DIRECTORY_APPDATA;
remarks:
A slash character is already appended to the directory returned by this property.
This function may return different values depending on the operating system the application is being run on.
On Windows, usually C:\Users%username%\appdata\roaming/.
on macOS, usually ~/Library/Preferences/.
on Linux, usually ~/.config/.
on Android, the app's internal storage path.
In any case, the directory returned should be writable.
Example:
void main() {
alert("example", "data for the game could be stored at " + DIRECTORY_APPDATA + "my_game/");
}
DIRECTORY_COMMON_APPDATA
Property that returns the system wide application data directory, which is shared by every user on the computer.
const string DIRECTORY_COMMON_APPDATA;
remarks:
A slash character is already appended to the directory returned by this property.
This property may return different values depending on the operating system the application is being run on.
On Windows, usually C:\ProgramData.
On Linux, usually /etc/.
This directory is often not writable without administrator or root privileges, so it is mostly useful for reading configuration that was put there by an installer. Per user data should go in DIRECTORY_APPDATA or DIRECTORY_LOCAL_APPDATA instead.
Example:
void main() {
alert("example", "shared configuration for the game could be read from " + DIRECTORY_COMMON_APPDATA + "my_game/");
}
DIRECTORY_HOME
Property that returns the current user's home directory.
const string DIRECTORY_HOME;
remarks:
A slash character is already appended to the directory returned by this property.
This property may return different values depending on the operating system the application is being run on.
On Windows, usually C:\Users%username%.
On Linux and macOS, usually the directory in the HOME environment variable, such as /home/username/ or /Users/username/.
Games should usually not write files directly into the home directory. Use DIRECTORY_APPDATA or DIRECTORY_LOCAL_APPDATA for game data instead.
Example:
void main() {
alert("example", "Your home directory is " + DIRECTORY_HOME);
}
DIRECTORY_LOCAL_APPDATA
Property that returns the user's local application data directory, which is suitable for data that should stay on the current machine.
const string DIRECTORY_LOCAL_APPDATA;
remarks:
A slash character is already appended to the directory returned by this property.
This property may return different values depending on the operating system the application is being run on.
On Windows, usually C:\Users%username%\appdata\local.
On Linux, usually ~/.local/share/.
On Windows, the difference from DIRECTORY_APPDATA is that the roaming directory may be synchronized between computers on some networks, while the local directory never is. Large files or data that only makes sense on one computer, such as caches, belong in the local directory.
Example:
void main() {
alert("example", "a cache for the game could be stored at " + DIRECTORY_LOCAL_APPDATA + "my_game/cache/");
}
DIRECTORY_TEMP
Property that returns the system's temporary directory, a place where short-term data can be stored.
const string DIRECTORY_TEMP;
remarks:
A slash character is already appended to the directory returned by this property.
This function may return different values depending on the operating system the application is being run on.
On Windows, usually C:\Users%username%\appdata\local\temp/.
on macOS and Linux systems, usually /tmp/.
on Android, the app's cache path.
In any case the directory returned should be writable, though you should expect things you store there to be deleted by external factors at any time.
Example:
void main() {
alert("example", "a temporary file for the game could be stored at " + DIRECTORY_TEMP + "filename.txt");
}