resources
modkit.resources reads files of your own resource. On the server it can also write them. The two sides work differently, so the functions are listed per side.
modkit.resources.file (server)
modkit.resources.file(path)
Server
Reads a file from the folder of the calling resource on the server's disk.
| Parameter | Type | Description |
|---|---|---|
| path | string | path relative to the resource folder, for example "data.json" or "config/shop.json" |
Returns: the file content as a string. nil when the file does not exist, is empty, or when the path is absolute or contains ...
local text = modkit.resources.file("data.json")
local data = text and modkit.json.decode(text) or {}
modkit.resources.save
modkit.resources.save(path, text)
Server
Writes a file into the folder of the calling resource. The content goes to <path>.tmp first and then replaces the target, so a crash during the write keeps the old file.
| Parameter | Type | Description |
|---|---|---|
| path | string | path relative to the resource folder. The folder part has to exist. |
| text | string | content. Binary data is fine. |
Returns: true when the file was written. false for an absolute path, a path with .., a missing folder or a disk error.
The write blocks the game thread. Keep files small and do not save on every event. See the persistence guide.
local ok = modkit.resources.save("data.json", modkit.json.encode({ visits = 12 }))
if not ok then print("save failed") end
modkit.resources.file (client)
modkit.resources.file(path)
Client
Returns a file that the server delivered for the calling resource. The client keeps these files in memory, there is no disk access.
| Parameter | Type | Description |
|---|---|---|
| path | string | path as listed by the manifest, with forward slashes, for example "textures/pattern.png" |
Returns: the file content as a string, or nil when the file was not delivered.
Only files listed under client, files or nui in the manifest exist on the client.
local text = modkit.resources.file("config/hud.json")
local cfg = text and modkit.json.decode(text) or {}