Skip to content

tw.file

Side: Shared

File loading and reading utilities for Lua modules and resource files.

Functions

tw.file.load(module_path) -> any

Loads and executes a Lua file, returning its result. Supports dot notation for paths and the @ prefix to load from other resources.

ParameterTypeDescription
module_pathstringPath in dot notation (e.g. "modules.utils" or "@other_resource.shared.helpers")

Returns: any — The return value of the loaded Lua file.

lua
-- Load a file from the current resource
local utils = tw.file.load("modules.utils")

-- Load from another resource using @ prefix
local data = tw.file.load("@twinded_libs.locales.en")

tw.file.read(...) -> content, resource_name, file_path

Reads the raw content of a file.

Returns:

ReturnTypeDescription
contentstringFile content
resource_namestringResource the file belongs to
file_pathstringResolved file path
lua
local content, resource, path = tw.file.read("config.settings")
print(content)

tw.file.exists(...) -> boolean

Checks if a file exists.

Returns: booleantrue if the file exists.

lua
if tw.file.exists("locales.fr") then
    local fr = tw.file.load("locales.fr")
end

Examples

Conditional locale loading

lua
local locale = tw.i18n.getLocale()
local path = "locales." .. locale

if tw.file.exists(path) then
    local translations = tw.file.load(path)
    tw.i18n.addEntries(translations)
end

Load a module from another resource

lua
local shared_data = tw.file.load("@twinded_libs.data.weapons")

Notes

  • Paths use dot notation, not slashes. "modules.utils" resolves to modules/utils.lua.
  • The @ prefix references another resource (e.g. "@twinded_libs.shared" loads from the twinded_libs resource).
  • tw.file.load executes the file and returns its result. Use tw.file.read if you only need the raw content.

Premium RedM Scripts — Multi-Framework