Complete Addon Structure

Examples Updated: Dec 10, 2025

Complete Addon Structure

Recommended structure for addons using cLib.

File Structure

myaddon/
├── lua/
│   ├── autorun/
│   │   └── sh_myaddon_loader.lua    # Main loader
│   └── myaddon/
│       ├── sh_config.lua            # Configuration
│       ├── sh_lang.lua              # Language strings
│       ├── sv_database.lua          # Database setup
│       ├── sv_network.lua           # Server networking
│       ├── sv_functions.lua         # Server functions
│       ├── cl_menu.lua              # Main menu
│       ├── cl_network.lua           # Client networking
│       └── cl_hud.lua               # HUD elements
└── materials/
    └── myaddon/
        ├── logo.png
        └── icons/

Loader File

-- autorun/sh_myaddon_loader.lua
MyAddon = MyAddon or {}
MyAddon.Version = "1.0.0"

hook.Add("cLib.OnLoaded", "MyAddon.Initialize", function()
    print("[MyAddon] Loading v" .. MyAddon.Version .. "...")
    
    -- Load all addon files
    cLib.includeDir("myaddon/")
    
    print("[MyAddon] Loaded successfully!")
    
    -- Fire custom hook
    hook.Run("MyAddon.Loaded")
end)

Configuration File

-- myaddon/sh_config.lua
MyAddon.Config = {
    -- General settings
    Enabled = true,
    DebugMode = false,
    
    -- Features
    Features = {
        HUD = true,
        Notifications = true,
        AutoSave = true
    }
}

-- Register colors
cLib.AddColor("MyAddon.Primary", Color(79, 140, 255))
cLib.AddColor("MyAddon.Secondary", Color(60, 60, 65))
cLib.AddColor("MyAddon.Success", Color(80, 200, 120))
cLib.AddColor("MyAddon.Warning", Color(255, 180, 50))
cLib.AddColor("MyAddon.Danger", Color(255, 80, 80))
cLib.AddColor("MyAddon.Background", Color(35, 35, 40))
cLib.AddColor("MyAddon.Panel", Color(45, 45, 50))

-- Register materials
cLib.AddMaterial("MyAddon.Logo", "myaddon/logo.png")

if CLIENT then
    -- Create fonts
    cLib.CreateFont("MyAddon.Title", "Roboto", 0.025, 600)
    cLib.CreateFont("MyAddon.SubTitle", "Roboto", 0.018, 500)
    cLib.CreateFont("MyAddon.Text", "Roboto", 0.014)
    cLib.CreateFont("MyAddon.Small", "Roboto", 0.012)
end

Server MySQL Database file

local dbConfig = {
    db_host = "localhost",      -- MySQL host
    db_port = 3306,             -- MySQL port
    db_user = "gmod_user",      -- Username
    db_password = "password",   -- Password
    db_name = "gmod_server"     -- Database name
}

local db = nil

hook.Add("cLib.OnLoaded", "MyAddon.Database", function()
    db = cLib.MYSQL.f.ConnectDatabase(dbConfig,
        function(database)
            -- Connected successfully
            print("[MyAddon] Database connected!")
            MyAddon.DB = database
            MyAddon.CreateTables()
        end,
        function(database, err)
            -- Disconnected
            print("[MyAddon] Database disconnected:", err)
        end
    )
end)

Language File

-- myaddon/sh_lang.lua
hook.Add("MyAddon.Loaded", "MyAddon.RegisterLang", function()
    cLib.Lang.AddPhrases("en", {
        ["myaddon.title"] = "My Addon",
        ["myaddon.menu.open"] = "Open Menu",
        ["myaddon.settings"] = "Settings",
        ["myaddon.save"] = "Save",
        ["myaddon.saved"] = "Settings saved!",
        ["myaddon.error"] = "An error occurred",
    })
    
    cLib.Lang.AddPhrases("es", {
        ["myaddon.title"] = "Mi Addon",
        ["myaddon.menu.open"] = "Abrir Menú",
        ["myaddon.settings"] = "Configuración",
        ["myaddon.save"] = "Guardar",
        ["myaddon.saved"] = "¡Configuración guardada!",
        ["myaddon.error"] = "Ocurrió un error",
    })
end)