Skip to main content
Version: latest - a1.73.x βš–οΈ

πŸ“ File

A File represents an entry to a system file.

πŸ’‚Authority
This class can be spawned on both 🟧 Client and 🟦 Server side (if you spawn it on client, it won't be synchronized with other players).
πŸ§‘β€πŸ’»API Source
This page is auto-generated! The Functions, Properties and Events described here are defined in our GitHub's API Repository! Feel free to commit suggestions and changes to the source .json API files!

info

It is not possible to open files from outside the server folder. All path must be relative to the Server's executable folder. All files are opened as binary file by default.

πŸŽ’ Examples​

local configuration_file = File("my_awesome_configuration.json")

local configuration_file_json = JSON.parse(configuration_file:Read())

πŸ“‚ File Access & Sandboxing​

Access to files is sandboxed. Only certain directories and files are allowed to be written or read from.

Server Side​

On Server side, it's only allowed to access files inside the server folder itself (where server executable is located). Also, accessing the Config.toml is not allowed.

The default directory is where the server executable is located.

Example accessing a file from a Package: Packages/My-Package/Server/MyFile.json.

Client Side​

On the client side, we have the concept of the .transient/ folder which is a folder automatically generated inside the client's cached Packages/ folder, where files can be created in a more persistent way.

It's not allowed to access files outside the client's cache folder Packages/, and write access is only permitted inside the .transient/ folder itself.

The default directory is in the client's cached Packages/.transient/ folder.

Example accessing a file from a Package (with ready-only access): ../my-package/Client/MyFile.json.

Example accessing a transient file (with write access): MyFile.json.

πŸ›  Constructors​

Default Constructor

No description provided

local my_file = File(file_path, truncate?)
TypeNameDefaultDescription
file_path Required parameter If on Server, this is the Path relative to server executable. Otherwise if on Client, this is the Path relative to Client's 'Packages/.transient/' folder
truncatefalseWhether or not to clear the file upon opening it

πŸ—Ώ Static Functions​

ReturnsNameDescription
Creates a Directory (for every folder passed)
Verifies if a entry exists in the file system
of Gets a list of all directories given a path
of Gets a list of all files in a directory
Gets the full path given a relative path based on the current side (client or server)
Checks if a path is a directory
Checks if a path is a file
Deletes a folder or file
Returns when a file was last modified in Unix time

CreateDirectory

Creates a Directory (for every folder passed)

β€” Returns (if succeeded).

local ret = File.CreateDirectory(path)
TypeParameterDefaultDescription
path Required parameter Path to folder

Exists

Verifies if a entry exists in the file system

β€” Returns (if exists).

local ret = File.Exists(path)
TypeParameterDefaultDescription
path Required parameter Path to file or folder

GetDirectories

Gets a list of all directories given a path, optionally with filters

β€” Returns of (List of directories).

local ret = File.GetDirectories(path_filter?, max_depth?)
TypeParameterDefaultDescription
path_filter?Path filter
max_depth?-1The maximum depth to go further in the folders while searching. Pass -1 for maximum depth

GetFiles

Gets a list of all files in a directory, optionally with filters

β€” Returns of (List of files).

local ret = File.GetFiles(path_filter?, extension_filter?, max_depth?)
TypeParameterDefaultDescription
or path_filter?Path filter
extension_filter?E.g.: .lua
max_depth?-1The maximum depth to go further in the folders while searching. Pass -1 for maximum depth

GetFullPath

Gets the full path given a relative path based on the current side (client or server)

β€” Returns (the full path parsed).

local ret = File.GetFullPath(path)
TypeParameterDefaultDescription
path Required parameter Path to file or directory

IsDirectory

Checks if a path is a directory

β€” Returns (if is a directory).

local ret = File.IsDirectory(path)
TypeParameterDefaultDescription
path Required parameter Path to folder

IsRegularFile

Checks if a path is a file

β€” Returns (if is a regular file).

local ret = File.IsRegularFile(path)
TypeParameterDefaultDescription
path Required parameter Path to file

Remove

Deletes a folder or file

β€” Returns (amount of files deleted).

local ret = File.Remove(path)
TypeParameterDefaultDescription
path Required parameter Path to file or folder

Time

Returns when a file was last modified in Unix time

β€” Returns (the last update time in unix time).

local ret = File.Time(path)
TypeParameterDefaultDescription
path Required parameter Path to file

🦠 Functions​

ReturnsNameDescription
Closes the file and destroys the entity
Flushes content to the file
Checks if the last operation has Failed
Checks if the file status is Bad
Checks if the file status is End of File
Checks if the file status is Good
Reads characters from the File and returns it. Also moves the file pointer to the latest read position. Pass 0 to read the whole file
Reads characters from the File asynchronously.
Reads the whole file as a JSON and returns it.
Reads the whole file as a JSON and returns it asynchronously.
Reads and returns the next file line
Sets the file pointer to a specific position
Returns the size of the file
Skips n (amount) positions from the current file pointer position
Returns the current file pointer position
Writes the Data at the current position of the file

Close

Closes the file and destroys the entity

my_file:Close()

Flush

Flushes content to the file

my_file:Flush()

HasFailed

Checks if the last operation has Failed

β€” Returns (if last operation failed).

local ret = my_file:HasFailed()

IsBad

Checks if the file status is Bad

β€” Returns (if status is Bad).

local ret = my_file:IsBad()

IsEOF

Checks if the file status is End of File

β€” Returns (if is EOF).

local ret = my_file:IsEOF()

IsGood

Checks if the file status is Good

β€” Returns (if status is Good).

local ret = my_file:IsGood()

Read

Reads characters from the File and returns it. Also moves the file pointer to the latest read position. Pass 0 to read the whole file

β€” Returns (file data).

local ret = my_file:Read(length?)
TypeParameterDefaultDescription
length?0Length to be read from file, leave it empty to read the whole file

ReadAsync

Reads characters from the File asynchronously.

my_file:ReadAsync(length, callback)
TypeParameterDefaultDescription
length Required parameter Length to be read from file, leave it empty to read the whole file
callback Required parameter Callback

ReadJSON

Reads the whole file as a JSON and returns it.

β€” Returns (parsed table).

local ret = my_file:ReadJSON()

ReadJSONAsync

Reads the whole file as a JSON and returns it asynchronously.

my_file:ReadJSONAsync(callback)
TypeParameterDefaultDescription
callback Required parameter Callback with the file read

ReadLine

Reads and returns the next file line

β€” Returns (file line data).

local ret = my_file:ReadLine()

Seek

Sets the file pointer to a specific position

my_file:Seek(position)
TypeParameterDefaultDescription
position Required parameter Position to offset the file pointer

Size

Returns the size of the file

β€” Returns (file size).

local ret = my_file:Size()

Skip

Skips n (amount) positions from the current file pointer position

my_file:Skip(amount)
TypeParameterDefaultDescription
amount Required parameter Amount to offset the file pointer

Tell

Returns the current file pointer position

β€” Returns (current file pointer position).

local ret = my_file:Tell()

Write

Writes the Data at the current position of the file

my_file:Write(data)
TypeParameterDefaultDescription
data Required parameter Data to write to the file

πŸš€ Events​

This class doesn't have own events.