string¶
Used to interact with text files.
Reading and saving text files¶
function setup()
local contentsOrNil = string.read(asset.."Hello.txt")
print("Contents: " .. tostring(contentsOrNil))
if test == "World" then
string.save(asset.."Hello.txt", "Hello")
else
string.save(asset.."Hello.txt", "World")
end
end
- string.read(assetKey)¶
Reads the contents of a file as text.
- Parameters:
assetKey (
assetKey) – The asset key of the file.- Returns:
The contents of the file.
- Return type:
- string.save(assetKey, text[, callback])¶
Saves text to a file.
If a callback is supplied, saving will happen in the background. Multiple calls to save the same file are grouped and only the latest save will be performed.
- Parameters:
assetKey (
assetKey) – The asset key of the file.text (
string) – The text to save.callback (
function) – Optional completion callbackfunction(ok, err).
string.save(asset.documents .. "Config.json", json.encode(config), function(ok, err) if not ok then print("Save failed:", err) end end)