Skip to main content

Add Dynamic Configuration

Overview

The Monolith configuration system provides a centralized way to manage gamemode settings with automatic client synchronization, file persistence, and admin permission controls. It supports nested configuration tables and automatic type checking.

Core Functions

Monolith.Config.Set(key, value)

Sets a configuration value. Can set entire tables or individual values.

Parameters:

  • key (string): Configuration key using dot notation
  • value (any): The value to set (must match default type)

Examples:

-- Set entire configuration table
Monolith.Config.Set("economy", {
initialCash = 5000,
paycheckInterval = 1200,
prices = {
fuel = 85,
electricity = 25
}
})

-- Set individual values
Monolith.Config.Set("economy.initialCash", 7500)
Monolith.Config.Set("economy.prices.fuel", 90)

Monolith.Config.Get(key, default)

Retrieves a configuration value with optional default fallback.

Parameters:

  • key (string): Configuration key using dot notation
  • default (any, optional): Default value if key doesn't exist

Returns: The configuration value or default

Examples:

-- Basic usage
local initialCash = Monolith.Config.Get("economy.initialCash")
local fuelPrice = Monolith.Config.Get("economy.prices.fuel")

-- With default value
local maxPlayers = Monolith.Config.Get("server.maxPlayers", 32)
local enableFeature = Monolith.Config.Get("features.newSystem", false)

Monolith.Config.GetAssert(key, default)

Same as Get() but throws an error if the value is nil.

Parameters:

  • key (string): Configuration key
  • default (any, optional): Default value

Returns: The configuration value (never nil)

Examples:

-- Will error if the key doesn't exist and no default is provided
local fuelDecay = Monolith.Config.GetAssert("economy.fuelDecay")

-- Safe with default
local repairReward = Monolith.Config.GetAssert("economy.prices.vehRepairReward", 150)

Monolith.Config.AddSetting(key, name, description, category, permission)

Registers a configuration option for the admin panel.

Parameters:

  • key (string): Configuration key
  • name (string): Display name in admin panel
  • description (string): Description for admins
  • category (string): Category grouping
  • permission (string, optional): Required permission to modify

Supported Types: boolean, number

Examples:

-- Basic setting
Monolith.Config.AddSetting("economy.initialCash", "Initial Cash", "Cash players start with", "Economy")

-- With permission requirement
Monolith.Config.AddSetting("economy.deathFee", "Death Fee", "Percentage lost on death", "Economy", "economy.modify")

-- Categorized settings
Monolith.Config.AddSetting("economy.prices.fuel", "Fuel Price", "Cost per fuel unit", "Economy/Prices")

Configuration File Structure

Basic Setup

Create configuration files in /gamemode/config/ directory:

-- gamemode/config/gameplay/economy.lua
Monolith.Config.Set("economy", {
-- Basic settings
initialCash = 5000,
paycheckInterval = 20 * 60, -- 20 minutes
deathFee = 0.2, -- 20% loss

-- Nested tables
prices = {
fuel = 85,
electricity = 25,
water = 10,
vehicle = 500
},

-- Server-only settings (not synced to clients)
server = {
databaseHost = "localhost",
apiKey = "secret_key"
}
})

-- Register admin settings
Monolith.Config.AddSetting("economy.initialCash", "Initial Cash", "Starting money for new players", "Economy")
Monolith.Config.AddSetting("economy.deathFee", "Death Fee", "Percentage of money lost on death", "Economy")
Monolith.Config.AddSetting("economy.prices.fuel", "Fuel Price", "Cost per fuel unit", "Economy/Prices")

Server-Only Configuration

Any subtable named server will not be synchronized to clients:

Monolith.Config.Set("myModule", {
publicSetting = "visible to clients",

server = {
secretKey = "only on server",
databasePassword = "hidden from clients"
}
})

Usage in Code

Basic Usage

-- Get configuration values
local initialCash = Monolith.Config.Get("economy.initialCash", 5000)
local fuelPrice = Monolith.Config.Get("economy.prices.fuel")

-- Use in your code
function PLAYER:GiveStartingMoney()
local amount = Monolith.Config.Get("economy.initialCash", 5000)
self:SetMoney(amount)
end

function CalculateFuelCost(gallons)
local pricePerGallon = Monolith.Config.Get("economy.prices.fuel", 85)
return gallons * pricePerGallon
end

Configuration Subscriptions

React to configuration changes in real-time:

-- Subscribe to configuration changes
Monolith.Config.Subscribe("MyModuleConfig", function(changedKey)
-- Reload configuration-dependent variables
myModuleEnabled = Monolith.Config.Get("myModule.enabled", true)
myModuleRate = Monolith.Config.Get("myModule.updateRate", 5)

print("Configuration updated:", changedKey)
end, { "myModule.enabled", "myModule.updateRate" })

Advanced Subscription Example

-- Vehicle system configuration subscription
local bIsDriveByAllowed = Monolith.Config.Get("general.vehicle.canDriveBy", false)
local iSpawnPrice = Monolith.Config.Get("economy.prices.vehicle", 500)

Monolith.Config.Subscribe("VehicleConfig", function(changedKey)
bIsDriveByAllowed = Monolith.Config.Get("general.vehicle.canDriveBy", false)
iSpawnPrice = Monolith.Config.Get("economy.prices.vehicle", 500)

-- Update all active vehicles if needed
if changedKey == "general.vehicle.canDriveBy" then
UpdateAllVehicleDriveBySettings()
end
end, { "general.vehicle.canDriveBy", "economy.prices.vehicle" })

Complete Module Example

Here's a complete example of setting up a new module with configuration:

-- gamemode/config/gameplay/my_module.lua

-- Define default configuration
Monolith.Config.Set("myModule", {
enabled = true,
updateInterval = 30,
maxItems = 100,

rewards = {
baseAmount = 500,
bonusMultiplier = 1.5
},

restrictions = {
minimumLevel = 5,
cooldownTime = 300
},

server = {
databaseTable = "my_module_data",
logLevel = "INFO"
}
})

-- Register admin-configurable settings
Monolith.Config.AddSetting("myModule.enabled", "Enable Module", "Enable or disable the entire module", "My Module")
Monolith.Config.AddSetting("myModule.updateInterval", "Update Interval", "Seconds between updates", "My Module")
Monolith.Config.AddSetting("myModule.maxItems", "Max Items", "Maximum items allowed", "My Module")

Monolith.Config.AddSetting("myModule.rewards.baseAmount", "Base Reward", "Base reward amount", "My Module/Rewards")
Monolith.Config.AddSetting("myModule.rewards.bonusMultiplier", "Bonus Multiplier", "Bonus reward multiplier", "My Module/Rewards")

Monolith.Config.AddSetting("myModule.restrictions.minimumLevel", "Minimum Level", "Required level to use", "My Module/Restrictions")
Monolith.Config.AddSetting("myModule.restrictions.cooldownTime", "Cooldown Time", "Cooldown in seconds", "My Module/Restrictions")
-- gamemode/modules/my_module/sv_my_module.lua

-- Cache configuration values
local bModuleEnabled = Monolith.Config.Get("myModule.enabled", true)
local iUpdateInterval = Monolith.Config.Get("myModule.updateInterval", 30)
local iBaseReward = Monolith.Config.Get("myModule.rewards.baseAmount", 500)

-- Subscribe to configuration changes
Monolith.Config.Subscribe("MyModuleConfig", function(changedKey)
bModuleEnabled = Monolith.Config.Get("myModule.enabled", true)
iUpdateInterval = Monolith.Config.Get("myModule.updateInterval", 30)
iBaseReward = Monolith.Config.Get("myModule.rewards.baseAmount", 500)

-- Restart timer with new interval
if changedKey == "myModule.updateInterval" then
timer.Remove("MyModuleUpdate")
if bModuleEnabled then
timer.Create("MyModuleUpdate", iUpdateInterval, 0, MyModuleUpdate)
end
end
end, { "myModule.enabled", "myModule.updateInterval", "myModule.rewards.baseAmount" })

-- Use configuration in functions
function CalculateReward(player)
if not bModuleEnabled then return 0 end

local baseAmount = Monolith.Config.Get("myModule.rewards.baseAmount", 500)
local multiplier = Monolith.Config.Get("myModule.rewards.bonusMultiplier", 1.5)

return baseAmount * (player:GetLevel() >= 10 and multiplier or 1)
end

function CanPlayerUse(player)
if not bModuleEnabled then return false end

local minLevel = Monolith.Config.Get("myModule.restrictions.minimumLevel", 5)
return player:GetLevel() >= minLevel
end

Best Practices

1. Organize Configuration Files

-- Group related settings
gamemode/config/
├── gameplay/
│ ├── economy.lua
│ ├── vehicles.lua
│ └── jobs.lua
├── technical/
│ ├── database.lua
│ └── networking.lua
└── features/
├── crafting.lua
└── housing.lua

2. Use Descriptive Keys

-- Good
Monolith.Config.Set("economy.vehicle.spawnPrice", 500)
Monolith.Config.Set("jobs.police.salary", 200)

-- Avoid
Monolith.Config.Set("price1", 500)
Monolith.Config.Set("val2", 200)

3. Provide Sensible Defaults

-- Always provide defaults when getting values
local spawnPrice = Monolith.Config.Get("economy.prices.vehicle", 500)
local enabled = Monolith.Config.Get("features.newSystem", false)

4. Use Server Tables for Sensitive Data

Monolith.Config.Set("database", {
host = "localhost", -- Synced to clients
port = 3306, -- Synced to clients

server = {
username = "root", -- Server only
password = "secret123" -- Server only
}
})

5. Register Settings for Admin Control

-- Make important settings admin-configurable
Monolith.Config.AddSetting("economy.initialCash", "Starting Money", "Money new players receive", "Economy")
Monolith.Config.AddSetting("features.pvpEnabled", "Enable PvP", "Allow player vs player combat", "Features")

Permissions

You can restrict setting modification to specific permissions:

-- Only users with "economy.modify" permission can change this
Monolith.Config.AddSetting("economy.initialCash", "Initial Cash", "Starting money", "Economy", "economy.modify")

-- Check permissions in code
if Monolith.Config.HasPermission(player, "economy.initialCash") then
-- Player can modify this setting
end

Configuration Loading Order

  1. Defaults Set: During module loading, default configurations are established
  2. Saved Values Loaded: Persistent values are loaded from data/monolith/config.txt
  3. Type Checking: Saved values are validated against default types
  4. Client Sync: Non-server values are synchronized to all clients
  5. Subscriptions Run: All configuration subscriptions are executed

This system ensures configuration consistency and provides a robust foundation for gamemode settings management.