Skip to content

File Handling

Hyomoto edited this page Oct 15, 2020 · 2 revisions

The File interface is designed to open a file, read its contents into an internal data structure, and then close the file. This allows for files read into the game to be manipulated more easily, and provides a consistent interface no matter what format the file that is being read from may be written in.

Dependencies: Core, Logging(Logger

Contents

Structures

File( read_only? )

  • read_only? - default: false, whether or not this file can be written to
var _file = new File( false );

_file.write( "Hello World!" );

Implements Core:GenericOutput.

The File interface provides a common framework to seek, open, write to, and save files. It serves as a "virtual" file. Other file types implement it for purposes of writing to the disk.

Methods

  • reset() - Returns file progress to the start.
  • size() - Returns the number of lines in the file.
  • exists() - Returns if the target file exists.
  • read() - Returns the next readable entry of the file.
  • peek( a ) - Returns the value at line a of the file, or undefined if it doesn't exist.
  • poke( a, b ) - Writes b to line a of the file, if it exists and file is writable.
  • write( a ) - Writes a to the end of the file if file is not writable.
  • remaining() - Returns how many lines in the file are left to read.
  • eof() - Returns if the end of the file has been reached.
  • save() - Returns true if this file is writable, otherwise logs and error and returns false.
  • destroy() - Destroys the internal data structures to prepare for garbage-collection.
  • close() - Calls save() and destroy().
  • clear() - Erases the contents of this file.
  • toArray() - Returns this file as an array.
  • toString() - Returns the name of the source file and lines read, used for debugging.

Variables

  • writable - boolean: whether or not this file can be written to
  • contents - ds_list: the data structure containing the contents of the file
  • next - the line that will next be read from the file
  • saveIndex - the line that saving should begin at

FileText( filename, read_only?, new? )

  • filename - the name of the file on disk
  • read only? - default: false, whether or not this file is allowed to be written to
  • new? - default: false, if true the file will be blank, otherwise its contents will be read from disk (if it exists)
var _file = new FileText( "hello.txt", false, true );

_file.write( "Hello World!" );

Implements File Handling: File.

Provides a file wrapper for reading from, and writing to, plain text files.

Methods

  • save( append ) - If the file is writable, will save the file to disk. If append is true, it will append to the existing file.

Variables

  • name - the name of the file.

FileBinary( filename, read_only?, new? )

  • filename - the name of the file on disk
  • read only? - default: false, whether or not this file is allowed to be written to
  • new? - default: false, if true the file will be blank, otherwise its contents will be read from disk (if it exists)
var _file = new FileBinary( "hello.bin", false, true );

_file.write( "Hello World!" );
_file.write( 10 );
_file.write( [ 10, "bigby" ] );

Implements File Handling: File.

Provides a file wrapper for reading from, and writing to, binary files. Accepts strings, reals as well as arrays containing strings and reals.

Methods

  • save() - Attempts to write the given data to a binary file, will log any errors encountered while writing the data.

Variables

  • name - the name of the file.
Clone this wiki locally