Files, storage and HTTP
In this tutorial you build a kill log. It keeps an all time kill count that survives restarts, writes every kill to a file you can open in a spreadsheet, and keeps a summary as JSON. At the end you fetch data from the web.
storage and files need no permission, because they only reach the script's own folder: scripts\data\<script>\. http needs Allow internet connections.
1. A number that survives restarts
storage is a small key and value store for each script:
local total = storage.get("total", 0)
events.on("local_kill", function(e)
total = total + 1
storage.set("total", total)
end)
events.on("paint", function()
draw.text(20, 20, "all time kills: " .. total, color(255, 255, 255), {shadow = true})
end)storage.get(key, default) gives the stored value, or the default when there is none. storage.set takes anything JSON can hold: numbers, strings, booleans and tables. evasive writes the store to disk every 5 seconds and when the script unloads, so you can call storage.set as often as you like.
storage.remove(key) deletes a key and storage.keys() lists them. A store holds up to 1024 keys and 1 MiB.
2. A line per kill
files reads and writes plain files inside the data folder. Paths are relative to it.
events.on("local_kill", function(e)
local who = e.player and e.player.name or "?"
local line = time.date("%Y-%m-%d %H:%M:%S") .. "," .. game.map() .. "," .. who .. "\n"
local ok, err = files.append("kills.csv", line)
if not ok then log.warn("could not write: " .. tostring(err)) end
end)files.append adds to the end of a file, files.write replaces it. Both give true, or nil and a message. Paths cannot contain : or device names, and store.json is taken by storage. A script's files can use up to 8 MiB.
| Function | What it does |
|---|---|
files.read(path) |
The file's text, or nil and a message. |
files.write(path, data) |
Writes the file. |
files.append(path, data) |
Adds to the end. |
files.exists(path) |
True when the file is there. |
files.list(dir) |
The names in a folder. Folder names end with /. |
files.remove(path) |
Deletes a file. |
3. A JSON summary
json.encode turns a table into text and json.decode turns it back:
local function save_summary(summary)
files.write("summary.json", json.encode(summary, true))
end
local function load_summary()
local text = files.read("summary.json")
if not text then return {maps = {}} end
local data = json.decode(text)
if type(data) ~= "table" then return {maps = {}} end
return data
endThe true in json.encode makes the output readable. json.decode gives nil and a message for broken JSON. A JSON null comes back as json.null, not nil, so a key that is null in the file still exists in the table. An empty Lua table encodes as [].
The finished script
-- @name Kill log
-- @description All time kills, a CSV line per kill and a JSON summary per map.
local show = ui.checkbox("Show counter", true)
ui.button("Reset all time count", function()
storage.set("total", 0)
end)
local function load_summary()
local text = files.read("summary.json")
if not text then return {maps = {}} end
local data = json.decode(text)
if type(data) ~= "table" or type(data.maps) ~= "table" then return {maps = {}} end
return data
end
local summary = load_summary()
events.on("local_kill", function(e)
storage.set("total", storage.get("total", 0) + 1)
local map = game and game.map() or ""
if map == "" then map = "unknown" end
summary.maps[map] = (summary.maps[map] or 0) + 1
local who = e.player and e.player.name or "?"
files.append("kills.csv", time.date("%Y-%m-%d %H:%M:%S") .. "," .. map .. "," .. who .. "\n")
end)
events.on("unload", function()
files.write("summary.json", json.encode(summary, true))
end)
events.on("paint", function()
if not show:get() then return end
draw.text(20, 20, "all time kills: " .. storage.get("total", 0), color(255, 255, 255), {shadow = true})
end)The summary is written once, when the script unloads. A reload counts as an unload too.
Your files end up in Documents\evasive\scripts\data\<script>\, so you can open kills.csv in any spreadsheet.
HTTP
Turn on Allow internet connections first. Without it http is nil.
Requests never block. You pass a function, and evasive calls it on a later frame with the response:
local status = "waiting"
events.on("load", function()
if not http then
status = "turn on Allow internet connections"
return
end
local id, err = http.get("https://example.com/status.json", function(res)
if res.error or res.status ~= 200 then
status = "failed: " .. (res.error or tostring(res.status))
return
end
local data = json.decode(res.body)
status = type(data) == "table" and ("status: " .. tostring(data.status)) or "bad JSON"
end)
if not id then status = "not sent: " .. err end
end)
events.on("paint", function()
draw.text(20, 40, status, color(255, 255, 255), {shadow = true})
end)The response table has status, body, headers and error. Always check error and status before you use the body.
To send data, use http.post(url, body, opts, fn). opts.headers sets request headers:
local function send_score(kills)
if not http then return end
local body = json.encode({kills = kills})
http.post("https://example.com/api/score", body, {headers = {["Content-Type"] = "application/json"}}, function(res)
if res.error then log.warn(res.error) end
end)
endFor other methods, use http.request({url = ..., method = "PUT", body = ..., headers = ...}, fn). It takes GET, POST, PUT, PATCH, DELETE and HEAD.
The rules:
- Only
httpandhttps, and only public addresses. Localhost and your local network are refused. - Up to 4 requests at once per script.
- 10 seconds timeout, up to 5 redirects, responses up to 2 MiB.
Anything a script can read, it can send once this switch is on, player names included. Only turn it on for scripts you trust.
Next
Using it with an AI assistant gets an assistant to write scripts with the real API.