Like2D Logo Like2D
← Back to Docs

Advanced

Filesystem, encryption, video playback, and miscellaneous utilities.

Filesystem API NEW

Functions for reading, writing, and managing files. All write operations use a separate userdata/ folder to keep game data separate from engine files.

readFile(path)

Reads a text file and returns its content as a string.

Returns: content (string/nil), error (string/nil)

local content, err = readFile("settings.txt")
if content then
    print("File content: " .. content)
else
    print("Error: " .. err)
end

writeFile(path, content)

Writes text content to a file in the userdata folder.

Returns: success (boolean), error (string/nil)

local ok, err = writeFile("config.txt", "resolution=1920x1080")

createDirectory(path)

Creates a new directory inside the userdata folder.

createDirectory("saves")
createDirectory("player/data")

listDirectory(path)

Returns a table of filenames inside the given directory.

fileExists(path)

Checks whether a file exists.

if fileExists("saves/slot1.dat") then
    print("Save file found!")
end

deleteFile(path)

Deletes a file from the userdata folder.

getGamePath()

Returns the absolute path to the game executable directory.

getUserdataPath()

Returns the absolute path to the userdata directory.

Encryption API NEW

Universal file encryption system that works with ALL file types (images, zip, text, etc.). Uses XOR encryption with a unique L2DE magic header.

encryptFile(path, inPlace)

Encrypts any file. If inPlace is true, overwrites the original. If false, creates a .enc copy.

-- Encrypt in-place (original file becomes encrypted)
encryptFile("assets/player.png", true)

-- Encrypt as .enc file (creates player.png.enc, keeps original)
encryptFile("assets/player.png", false)

decryptFile(path)

Decrypts an encrypted file and overwrites it with the original content.

readEncryptedFile(path)

Reads and decrypts a file, returning the raw content as a string.

Returns: content (string/nil), error (string/nil)

writeEncryptedFile(path, data)

Encrypts the given string data and writes it to a file with the L2DE magic header.

-- Save encrypted data
writeEncryptedFile("save.dat", json.encode({score = 100, level = 5}))

-- Load it back
local data = readEncryptedFile("save.dat")
if data then
    local save = json.decode(data)
    print("Score: " .. save.score)
end

Loading Encrypted Images

The loadImage() function automatically detects encrypted files. Just encrypt your image and load it directly:

-- Step 1: Encrypt an image
encryptFile("assets/boss.png", false)  -- creates boss.png.enc

-- Step 2: Load the encrypted image (auto-decrypts!)
loadImage("assets/boss.png.enc")
renderImage("assets/boss.png.enc", 0, 0, 128, 128)

Video Playback

loadVideo(key, filename)

Loads video file.

updateVideo(key, dt)

Updates video frame. Call in onUpdate().

renderVideo(key, x, y, w, h)

Renders current video frame.

playVideo(key, loop)

Starts playback.

pauseVideo(key) / stopVideo(key)

Pauses/stops video playback.

seekVideo(key, seconds)

Jumps to time position.

isVideoPlaying(key) / isVideoEnded(key)

Returns playback state.

getVideoDuration(key) / getVideoTime(key)

Returns duration/current position in seconds.

unloadVideo(key)

Unloads video from memory.

Miscellaneous

quit()

Cleanly closes the game.