file

The file library provides functions for interacting with the local filesystem. This is crucial for loading external resources like images, configuration files, and more.

Important Notes:

  • Security and Sandbox: For security, all file operations are sandboxed and restricted to a specific sub-folder within your main application directory, typically named files/. You cannot use this API to read files from arbitrary locations on the user's computer (e.g., C:\Windows\system32.dll).

  • Path Separators: Always use a forward slash (/) for directory separators in your paths, even on Windows. The C++ backend will handle the conversion correctly. Example: "configs/my_settings.json".

  • Naming Conventions: All functions support PascalCase, camelCase, and snake_case.


Draw an example logo over enemies using their bounding box. This uses the read function, utility LoadImage function, and draw.Image.

local LogoESP = {
    textureId = nil,
    isInitialized = false
}


function LogoESP:Initialize()
    local success, result = pcall(function()

        if not (file and file.read) then
            print("[LogoESP] Error: 'file.read' API is not available. Cannot load image.")
            return nil
        end

        local imageData = file.read("test_logo.png")
        if not imageData then
            return nil
        end
        
        return utility.loadImage(imageData)
    end)

    if success and result then
        self.textureId = result
        self.isInitialized = true
    else
        print("[LogoESP] Initialization failed.")
        if not success then
            print("  -> Error details:", result)
        end
    end
end


function LogoESP:OnPaint()
    if not self.isInitialized or not self.textureId then
        return
    end

    local only_enemies = false
    local players = entity.getPlayers(only_enemies)


    for i, player in ipairs(players) do
        local bbox = player.BoundingBox
        

        if bbox then
            draw.image(self.textureId, bbox.x, bbox.y, bbox.w, bbox.h, 255, 255, 255, 255)
        end
    end
end

LogoESP:Initialize()
cheat.register("onPaint", function()
    LogoESP:OnPaint()
end)

File Operations

read

Reads the entire content of a specified file and returns it as a string. This function can read both plain text and binary files (like images).

  • Aliases: file.Read, file.read

  • Signature: file.read(filepath)

  • Parameters:

    • filepath: string - The path to the file, relative to the sandboxed files/ directory.

  • Returns: string or nil - The file's content as a string. If the file does not exist or cannot be opened, it returns nil.

  • Usage Examples:

    1. Reading a text file:

    local welcome_message = file.read("welcome.txt")
    if welcome_message then
        print(welcome_message)
    end

  • 2. Loading an image for the draw API:

    local logo_data = file.read("images/my_logo.png")
    if logo_data then
        local texture_id = utility.loadImage(logo_data)
        
        cheat.register("onPaint", function()
            draw.image(texture_id, 10, 10, 128, 128, 255, 255, 255, 255)
        end)
    end

Last updated