ModBase API

ModBase is the abstract base class that all MDB mods must inherit from. It provides lifecycle callbacks and access to core framework services like logging.

Namespace: GameSDK.ModHost


Class Definition

public abstract class ModBase
{
    public ModInfo Info { get; internal set; }
    public ModLogger Logger { get; internal set; }
    
    public virtual void OnLoad() { }
    public virtual void OnUpdate() { }
    public virtual void OnFixedUpdate() { }
    public virtual void OnLateUpdate() { }
    public virtual void OnGUI() { }
}

Properties

Info

public ModInfo Info { get; internal set; }

Metadata about your mod, populated from the [Mod] attribute. Contains:

Example:

public override void OnLoad()
{
    Logger.Info($"Loading {Info.Name} v{Info.Version} by {Info.Author}");
}

Logger

public ModLogger Logger { get; internal set; }

Logger instance for this mod. All messages are prefixed with the mod name and written to MDB/Logs/Mods.log.

See ModLogger API for details.


Lifecycle Methods

All lifecycle methods are virtual - override only the ones you need.

OnLoad()

public virtual void OnLoad()

Called once when the mod is loaded. Use this for initialization:

Timing: Called immediately after the mod DLL is loaded, before the game’s main loop starts.

Example:

public override void OnLoad()
{
    Logger.Info("Mod initialized!");
    ImGuiManager.RegisterCallback(DrawUI, "My Window");
}

OnUpdate()

public virtual void OnUpdate()

Called every frame during Unity’s Update loop.

Frequency: ~60 Hz (varies with game framerate)

Use for:

Example:

private int frameCount = 0;

public override void OnUpdate()
{
    frameCount++;
    if (frameCount % 60 == 0)
        Logger.Info($"60 frames passed, total: {frameCount}");
}

Performance Note: This runs every frame. Keep logic lightweight or use timers to run expensive operations less frequently.

OnFixedUpdate()

public virtual void OnFixedUpdate()

Called at fixed time intervals during Unity’s FixedUpdate loop.

Frequency: Fixed timestep (usually 50 Hz / 0.02s, but game-dependent)

Use for:

Example:

public override void OnFixedUpdate()
{
    // Apply constant velocity to player
    // Runs at fixed intervals regardless of framerate
}

OnLateUpdate()

public virtual void OnLateUpdate()

Called after all Update callbacks during Unity’s LateUpdate loop.

Frequency: ~60 Hz (varies with game framerate)

Use for:

Example:

public override void OnLateUpdate()
{
    // Update camera position after player has moved
}

OnGUI()

public virtual void OnGUI()

Called during Unity’s GUI rendering phase for ImGui rendering.

Frequency: ~60 Hz (varies with game framerate)

Use for:

Note: Most mods should use ImGuiManager.RegisterCallback in OnLoad() instead of overriding OnGUI() directly.

Example:

public override void OnGUI()
{
    if (ImGui.Begin("Direct GUI"))
    {
        ImGui.Text("Rendered in OnGUI");
    }
    ImGui.End();
}

Complete Example

using System;
using GameSDK.ModHost;
using GameSDK.ModHost.ImGui;

namespace ExampleMod
{
    [Mod("Author.Example", "Example Mod", "1.0.0",
         Author = "Your Name",
         Description = "Demonstrates ModBase lifecycle")]
    public class ExampleMod : ModBase
    {
        private int updateCount = 0;
        private int fixedUpdateCount = 0;
        private bool showWindow = true;

        public override void OnLoad()
        {
            // Initialization
            Logger.Info($"{Info.Name} v{Info.Version} loading...");
            Logger.Info($"Mod file: {Info.FilePath}");
            
            // Register UI
            ImGuiManager.RegisterCallback(DrawUI, "Example Window");
            
            Logger.Info("Mod loaded successfully!");
        }

        public override void OnUpdate()
        {
            // Every frame
            updateCount++;
            
            // Example: Log every 300 frames (5 seconds @ 60fps)
            if (updateCount % 300 == 0)
            {
                Logger.Debug($"Updates: {updateCount}, Fixed: {fixedUpdateCount}");
            }
        }

        public override void OnFixedUpdate()
        {
            // Fixed timestep
            fixedUpdateCount++;
        }

        public override void OnLateUpdate()
        {
            // After all updates
            // Could do camera work here
        }

        private void DrawUI()
        {
            if (!showWindow) return;
            
            if (ImGui.Begin("Example Window", ref showWindow))
            {
                ImGui.Text($"Update calls: {updateCount}");
                ImGui.Text($"FixedUpdate calls: {fixedUpdateCount}");
                ImGui.Separator();
                
                if (ImGui.Button("Reset Counters"))
                {
                    updateCount = 0;
                    fixedUpdateCount = 0;
                    Logger.Info("Counters reset!");
                }
            }
            ImGui.End();
        }
    }
}

Best Practices

✅ Do

❌ Don’t


See Also


← Back to API Index ModAttribute →