Paths module

Filesystem paths for Lua scripts. A path is a value rather than text, so joining one uses / and its parts are properties. Scripts can read and write under the game's own directories, and nowhere else.

Properties

  • trx.path.trx_dir (trx.path.Path). The %trx_dir% directory, or nil where the game keeps none. (read-only)
  • trx.path.config_dir (trx.path.Path). The %config_dir% directory, or nil where the game keeps none. (read-only)
  • trx.path.cache_dir (trx.path.Path). The %cache_dir% directory, or nil where the game keeps none. (read-only)
  • trx.path.games_dir (trx.path.Path). The %games_dir% directory, or nil where the game keeps none. (read-only)
  • trx.path.screenshots_dir (trx.path.Path). The %screenshots_dir% directory, or nil where the game keeps none. (read-only)
  • trx.path.saves_dir (trx.path.Path). The %saves_dir% directory, or nil where the game keeps none. (read-only)
  • trx.path.legacy_saves_dir (trx.path.Path). The %legacy_saves_dir% directory, or nil where the game keeps none. (read-only)

Structures

  • trx.path.Path

    A filesystem path. Joining one with / appends a child segment, and its parts are available as properties.

    A path only points to a location. It does not say whether a file is present until exists checks it.

    Properties:

    • name: string. The final component of the path, with its extension. (read-only)
    • parent: trx.path.Path. The directory the path sits in. (read-only)
    • stem: string. The final component of the path, without its extension. (read-only)
    • suffix: string. The extension at the end of the final component, leading . and all, or the empty string where there is none. (read-only)

    Operators:

    • path .. path. A path joins text as itself, whichever side of the .. it is on.
    • path / path. Appends a child segment, as config_dir / "mymod" / "state.json". An absolute path on the right replaces the left side.
    • path == path. Two paths are equal when their filesystem text is equal.
    • tostring(path). The path as the text the engine would open.

    Methods:

    • path:exists()
      Whether anything is at the path now. Raises where the path is outside the directories a script may reach.

      Returns: boolean. Whether a file or directory is present.

    • path:is_reachable()
      Whether a script may read or write there. Scripts reach the game's own directories and nothing else, so the rest of the player's disk is closed to them.

      Returns: boolean. Whether reading and writing are allowed.

    • path:read_text()
      Reads the file as text, or returns nil where no file is present. Raises where the path is outside the directories a script may reach.

      Returns: string or nil. The text, or nil for a file that is not there.

    • path:write_text(text)
      Writes text into the file, making the directories it sits in and writing over an existing file. Raises where the path is outside the directories a script may reach.

      Parameters:

      • text (string). What to write.

Functions

  • trx.path.new(text)
    Creates a path from text, which the engine opens as it stands. Every %token% in the text is expanded first, so "%config_dir%/mymod" says the same thing as trx.path.config_dir / "mymod".

    Parameters:

    • text (string). The path as text.

    Returns: trx.path.Path. The path.

    Example:

    local kept = trx.path.new("%config_dir%/mymod/state.json")
    
  • trx.path.kinds()
    Every kind of file trx.path.resolve may be asked for.

    Returns: table. The file kinds, as a list of strings.

    Example:

    for _, kind in ipairs(trx.path.kinds()) do
      trx.log.info(kind)
    end
    
  • trx.path.resolve(kind, name)
    Works out where the engine would find one of its own files, searching in the order it searches: a mod's own copy first, then the game the mod sits on, then the configuration directory. If no file is found, this returns nil.

    This is how a script reads a file the game ships without knowing which of those directories supplies it. trx.path.kinds lists what may be asked for.

    Parameters:

    • kind (string). Which kind of file, such as common_config or level_file.
    • name (string). The file to look for, such as weapons.json5.

    Returns: trx.path.Path or nil. The file path, or nil.

    Example:

    local weapons = trx.path.resolve("common_config", "weapons.json5")
    if weapons ~= nil then
      trx.log.info("weapons come from " .. tostring(weapons))
    end