


LuaDB v0.1.7
LuaDB is a SQLite-based persistence module for Lua, intended for Kingdom Come: Deliverance II mod developers. Regular mod users only need to install it when another mod requires it.
Features
- Lua data, including tables, booleans, numbers, and strings, can be stored in an SQLite database.
- Data kept in global storage remains available through every game save and after game restarts.
- Save-associated storage ties data to one particular game save file and handles saving and loading automatically.
- An easy-to-use Lua API is available through the original
LuaDBinterface as well as the object-orientedDB.Createinterface. - Lua objects that can be encoded by
json.luaare supported. - External storage is held in
kcd2db.db, which minimizes the risk of data corruption. - Debugging support includes
LuaDB.Dump(),kcd2db.log, and the SQLite databasekcd2db.db. - Possible uses include mod settings, player progress, inventory and outfit data, plus complex structures such as tables and arrays.
Installation
- Ultimate ASI Loader is required and is included in the download.
- Put this mod’s
.asifile and the ASI loader, the.dllfile, in the game’s root directory. This is the folder containingKingdomCome.exe, located atKingdomComeDeliverance2BinWin64MasterMasterSteamPGO.
Linux users: Proton/Wine users should add the following to their Steam launch options:
WINEDLLOVERRIDES="dinput8=n,b" %command%
Storage modes
- Global Storage keeps data across game saves. Its global APIs end with
Gand save and load automatically. - Save-associated Storage connects data to game save slots and synchronizes saving and loading automatically.
- Whenever the game is saved, local data is automatically saved; when that save is loaded, the data is automatically loaded.
How to use
For a database object belonging to your mod, call DB.Create("ModName"). Stored data is converted to JSON automatically, then converted back into Lua objects when retrieved.
Example:
YourMod = YourMod or (function()
-- Make LuaDB an enhancement for mods, allowing users to opt for LuaDB to achieve persistence.
local db = LuaDB and DB and select(2, pcall(DB.Create,"YourMod"))
return {
version = "__VERSION__",
localData = db and db.L or {},
globalData = db and db.G or {}
}
end)()
function YourMod:Init()
local settingA = YourMod.localData.settingA
local settingB = YourMod.globalData.settingB
-- do something with settings
end
function YourMod:Save()
YourMod.localData.settingA = "valueA"
YourMod.globalData.settingB = { a = 1, b = 2 }
-- save settings
end
function YourMod:DoSomething()
local settingA = YourMod.localData.settingA
local settingB = YourMod.globalData.settingB
-- do something with settings
-- update settings if something changed
YourMod.localData.settingA = settingA
YourMod.globalData.settingB = settingB
end
YourMod:Init()
API
DB API
-- Create a database instance (recommended for most use cases)
local myDB = DB.Create("MyModNamespace") -- Use your mod name or unique identifier
-- Store Lua object (including nested tables)
myDB.Set("player_health", 85.6)
myDB:Set("has_dragon_sword", true) -- myDB allows both .call and :call syntax
-- Example with complex object
local config = {
difficulty = "hard",
weapons = {
sword = true,
bow = false
},
greeting = "hello world"
}
myDB.Set("player_config", config)
-- Retrieve data (automatically converts back to Lua objects)
local health = myDB.Get("player_health")
local config = myDB.Get("player_config")
-- Global APIs (Cross-save)
myDB.SetG("settings", {volume = 0.8, fullscreen = true})
local settings = myDB.GetG("settings") -- return table {volume = 0.8, fullscreen = true}
Methods
DB.Create("Your MOD")– Creates a namespace instance with the same API, automatically adding a prefix to all keys to reduce the likelihood of key conflicts. This method should always be preferred.DB.Get(key)– Get local key value.DB.Set(key, value)– Set local key value.DB.Del(key)– Delete local key value.DB.Exi(key)– Check if local key exists.DB.All()– Get all local key values.DB.GetG(key)– Get global key value.DB.SetG(key, value)– Set global key value.DB.DelG(key)– Delete global key value.DB.ExiG(key)– Check if global key exists.DB.AllG()– Get all global key values.DB.Dump()– Print all data.
Every method accepts either . call syntax or : call syntax, so the preferred form can be chosen according to personal preferences.
Quick access
DB("Your MOD")– Equivalent toDB.Create("Your MOD").DB.key/DB["key"]– Access local key value. If it has the same name as an existing method, it will cause a conflict, resulting in the return of the function instead of the data.DB.L.key/DB.L["key"]– Access local key value without conflict.DB.G.key/DB.G["key"]– Access global key value without conflict.
Original API
-- Store data (automatically bound to current save) LuaDB.Set(key, value) -- Retrieve data local value = LuaDB.Get(key) -- Delete data LuaDB.Del(key) -- Check key existence local exists = LuaDB.Exi(key) -- Store global data LuaDB.SetG(key, value) -- Retrieve global data local value = LuaDB.GetG(key) -- Delete global data LuaDB.DelG(key) -- Check global key existence local exists = LuaDB.ExiG(key)
Debug commands
-- View all stored data in console LuaDB.Dump()
Supported data types
- Key: Within the LuaDB API, a key must be a string. With the DB API, any Lua object encodable by
json.luais acceptable. - Value: LuaDB API values must be boolean, number, or string. DB API values may be any Lua object encodable by
json.lua, including boolean, number, string, and table.
Usage examples
-- Using the new OOP interface (recommended)
local modDB = DB.Create("MyAwesomeMod")
modDB.Set("player_stats", {
health = 100,
stamina = 85.5,
inventory = {"sword", "shield", "potion"}
})
-- Original API examples
LuaDB.Set("player_health", 85.6)
LuaDB.SetG("global_kill_count", 42)
-- View data in console
LuaDB.Dump()
Notes
- Namespace names cannot contain colons.
"namespace:"will be used as a key prefix in the database to isolate data from different mods. - If a key name matches an existing method, direct access invokes the method rather than returning the key value. Use
.Lor.Ginstead. - All values are automatically encoded and decoded as JSON. The game includes built-in
json.lua V0.1.1. - A single object’s JSON string should stay at or below 1 billion bytes, approximately 953MB. Beyond that, SQLite reports
"string or blob too big", and storage is impossible. - With
Dump(), strings containing$0~9are interpreted by the console as color codes. The in-game console cannot show non-ASCII characters, and DEBUG log data is truncated to prevent lag.
Debugging
- The SQLite3 database used in the game root is named
kcd2db.db. - Operation logs are kept in
kcd2db.login the game root. - At startup, diagnostics report module detection and database path information.
- When started with
-console, INFO/WARN/ERROR logs also appear in the console by default. - DEBUG logs stay in
kcd2db.logso the console is not flooded. - The console log level can be set with:
kcd2dbConsoleLog=debug|info|warn|error|off
If the mod does not load or crashes during startup, include kcd2db.log in the issue report.
Compatibility and known issues
This mod makes extensive use of reverse-engineered game internals, so game updates may affect it. If crashes begin after a game update, try removing the mod file or rename its .asi extension to disable it.




