File

Type to handle file reading and writing operations.

File type. This is used to permanently store data into the user device's file system and to read from it. This can be used to store game save data or player configuration files, for example. Here's a sample on how to write and read from a file:

More...
@GodotBaseClass
struct File {}

Members

Aliases

BaseClasses
alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses)
Undocumented in source.

Enums

CompressionMode
enum CompressionMode
Constants
enum Constants
ModeFlags
enum ModeFlags

Functions

close
void close()

Closes the currently opened file.

eofReached
bool eofReached()

Returns true if the file cursor has reached the end of the file.

fileExists
bool fileExists(StringArg0 path)

Returns true if the file exists in the given path.

get16
long get16()

Returns the next 16 bits from the file as an integer.

get32
long get32()

Returns the next 32 bits from the file as an integer.

get64
long get64()

Returns the next 64 bits from the file as an integer.

get8
long get8()

Returns the next 8 bits from the file as an integer.

getAsText
String getAsText()

Returns the whole file as a String.

getBuffer
PoolByteArray getBuffer(long len)

Returns next len bytes of the file as a PoolByteArray.

getCsvLine
PoolStringArray getCsvLine(StringArg0 delim)

Returns the next value of the file in CSV (Comma Separated Values) format. You can pass a different delimiter to use other than the default "," (comma).

getDouble
double getDouble()

Returns the next 64 bits from the file as a floating point number.

getEndianSwap
bool getEndianSwap()
getError
GodotError getError()

Returns the last error that happened when trying to perform operations. Compare with the ERR_FILE_* constants from @GlobalScope.

getFloat
double getFloat()

Returns the next 32 bits from the file as a floating point number.

getLen
long getLen()

Returns the size of the file in bytes.

getLine
String getLine()

Returns the next line of the file as a String.

getMd5
String getMd5(StringArg0 path)

Returns an MD5 String representing the file at the given path or an empty String on failure.

getModifiedTime
long getModifiedTime(StringArg0 file)

Returns the last time the file was modified in unix timestamp format or returns a String "ERROR IN file". This unix timestamp can be converted to datetime by using OS.getDatetimeFromUnixTime.

getPascalString
String getPascalString()

Returns a String saved in Pascal format from the file.

getPath
String getPath()

Returns the path as a String for the current open file.

getPathAbsolute
String getPathAbsolute()

Returns the absolute path as a String for the current open file.

getPosition
long getPosition()

Returns the file cursor's position.

getReal
double getReal()

Returns the next bits from the file as a floating point number.

getSha256
String getSha256(StringArg0 path)

Returns a SHA-256 String representing the file at the given path or an empty String on failure.

getVar
Variant getVar()

Returns the next Variant value from the file.

isOpen
bool isOpen()

Returns true if the file is currently opened.

opAssign
File opAssign(T n)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(File other)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(typeof(null) n)
Undocumented in source. Be warned that the author may not have intended to support it.
open
GodotError open(StringArg0 path, long flags)

Opens the file for writing or reading, depending on the flags.

openCompressed
GodotError openCompressed(StringArg0 path, long mode_flags, long compression_mode)

Opens a compressed file for reading or writing. Use COMPRESSION_* constants to set compression_mode.

openEncrypted
GodotError openEncrypted(StringArg0 path, long mode_flags, PoolByteArray key)

Opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.

openEncryptedWithPass
GodotError openEncryptedWithPass(StringArg0 path, long mode_flags, StringArg2 pass)

Opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.

seek
void seek(long position)

Change the file reading/writing cursor to the specified position (in bytes from the beginning of the file).

seekEnd
void seekEnd(long position)

Changes the file reading/writing cursor to the specified position (in bytes from the end of the file). Note that this is an offset, so you should use negative numbers or the cursor will be at the end of the file.

setEndianSwap
void setEndianSwap(bool enable)
store16
void store16(long value)

Stores an integer as 16 bits in the file.

store32
void store32(long value)

Stores an integer as 32 bits in the file.

store64
void store64(long value)

Stores an integer as 64 bits in the file.

store8
void store8(long value)

Stores an integer as 8 bits in the file.

storeBuffer
void storeBuffer(PoolByteArray buffer)

Stores the given array of bytes in the file.

storeDouble
void storeDouble(double value)

Stores a floating point number as 64 bits in the file.

storeFloat
void storeFloat(double value)

Stores a floating point number as 32 bits in the file.

storeLine
void storeLine(StringArg0 line)

Stores the given String as a line in the file.

storePascalString
void storePascalString(StringArg0 string)

Stores the given String as a line in the file in Pascal format (i.e. also store the length of the string).

storeReal
void storeReal(double value)

Stores a floating point number in the file.

storeString
void storeString(StringArg0 string)

Stores the given String in the file.

storeVar
void storeVar(VariantArg0 value)

Stores any Variant value in the file.

Mixins

__anonymous
mixin baseCasts
Undocumented in source.

Properties

endianSwap
bool endianSwap [@property getter]
bool endianSwap [@property setter]

If true the file's endianness is swapped. Use this if you're dealing with files written in big endian machines. Note that this is about the file format, not CPU type. This is always reset to false whenever you open the file.

Static functions

_new
File _new()
Undocumented in source. Be warned that the author may not have intended to support it.

Static variables

_classBindingInitialized
bool _classBindingInitialized;
Undocumented in source.

Structs

_classBinding
struct _classBinding
Undocumented in source.

Unions

__anonymous
union __anonymous
Undocumented in source.

Variables

_GODOT_internal_name
enum string _GODOT_internal_name;
Undocumented in source.

Mixed In Members

From mixin baseCasts

as
To as()
Undocumented in source. Be warned that the author may not have intended to support it.
as
To as()
Undocumented in source. Be warned that the author may not have intended to support it.
as
ToRef as()
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
template opCast(To)
Undocumented in source.
opCast
template opCast(To)
Undocumented in source.
opCast
template opCast(ToRef)
Undocumented in source.
opCast
void* opCast()
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
godot_object opCast()
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
bool opCast()
Undocumented in source. Be warned that the author may not have intended to support it.

Detailed Description

func save(content): var file = File.new() file.open("user://save_game.dat", File.WRITE) file.store_string(content) file.close()

func load(): var file = File.new() file.open("user://save_game.dat", File.READ) var content = file.get_as_text() file.close() return content

Meta