Reference/Native_modules/lu_ini_INI_files
LoadModule("lu_ini") adds functions to read and write INI files: plain text files with [Section] headers and Key = Value lines, which are easy to edit by hand. Good for settings, small lists and per-player values on small servers; use a database (C.4) for thousands of accounts.
; Data/settings.ini
[Server]
Motd = Welcome to my server!
MaxCash = 500000
Hardcore = true
[Spawn]
X = 812.5
Y = -940.2How it works. A file is read once and kept in memory (it is read again when it changes on disk). A write changes the copy in memory and the file is saved a few milliseconds later, in one go, and always when the scripts unload or the server stops - so thousands of calls cost almost nothing. Paths are relative to the server folder; missing folders are not created. A missing file reads as empty, and the first write creates it.
Functions
ReadIniString
ReadIniString(file, section, key)The value as a string, or "" when the file, section or key does not exist.
- file – Path of the INI file, for example
"Data/settings.ini". - section – Section name without brackets (
"Server"). Use""for lines before the first header. - key – Key name. Names are case sensitive.
Returns: string
ReadIniInteger
ReadIniInteger(file, section, key)The value as an integer ("12abc" reads 12, text reads 0), or 0 when it is missing.
Returns: integer
ReadIniFloat
ReadIniFloat(file, section, key)The value as a float, or 0.0 when it is missing or not a number.
Returns: float
ReadIniBool
ReadIniBool(file, section, key)true when the value is 1, true, yes or on (any case), otherwise false (also when missing).
Returns: bool
ReadIniIndex
ReadIniIndex(file, section, index)The NAME of the key at position index (0 based) in the section, or "" past the end. Use it with CountIniSection to walk through a section.
Returns: string
CountIniSection
CountIniSection(file, section)The number of keys in a section (0 when it does not exist).
Returns: integer
WriteIniString
WriteIniString(file, section, key, value)Sets a key (creating the file, section and key when needed). Values with leading / trailing spaces or line breaks are written in double quotes and read back exactly.
Returns: bool (true)
WriteIniInteger / WriteIniFloat / WriteIniBool
WriteIniInteger(file, section, key, value)
WriteIniFloat(file, section, key, value)
WriteIniBool(file, section, key, value)The same for numbers and bools. Bools are written as true / false, floats with up to 6 significant digits (%g).
Returns: bool (true)
RemoveIniValue
RemoveIniValue(file, section, key)Deletes one key.
Returns: bool (true)
DeleteIniSection
DeleteIniSection(file, section)Deletes a section with all its keys.
Returns: bool (true)
ClearIni
ClearIni(file)Empties the whole file.
Returns: bool (true)
FlushIni
FlushIni()Saves every changed file right now. Only needed when another program reads the files while the server runs.
Returns: bool (true)
LoadModule("lu_ini");
const CFG = "Data/settings.ini";
function onScriptLoad()
{
local motd = ReadIniString(CFG, "Server", "Motd");
if (motd == "") WriteIniString(CFG, "Server", "Motd", "Welcome!");
print("max cash: " + ReadIniInteger(CFG, "Server", "MaxCash"));
}
// per-player values: one section per name
function onPlayerPart(player, reason)
{
WriteIniInteger("Data/players.ini", player.Name, "Cash", player.Cash);
WriteIniInteger("Data/players.ini", player.Name, "Score", player.Score);
}
// list every key of a section
function ListSection(file, section)
{
local n = CountIniSection(file, section);
for (local i = 0; i < n; i++)
{
local key = ReadIniIndex(file, section, i);
print(key + " = " + ReadIniString(file, section, key));
}
}File format (the same as the original Liberty Unleashed module, so old files read fine): section and key order, blank lines and comment lines (; or #) are kept when the file is saved; a # after a value starts a comment, write \# for a real one (also \\ and \"); \= in a key and \] in a section name; when a key appears twice the last one wins; sections with the same name are merged. Lines are written with Windows line breaks (CRLF).
"Data/" + player.Name + ".ini"): a name like ../Server would point outside the folder. Use one file with a section per player instead.Source code
The full source of lu_ini is on the page lu_ini source code (download as zip).