Skip to content

CommandRegistry

Kanisuko edited this page May 23, 2026 · 3 revisions

CommandRegistry

Namespace:​ ScavLib.command

Safely register custom commands into the game's native developer console. Commands are injected at the correct time automatically via a Harmony Postfix on ConsoleScript.Start() — you do not need to manage injection timing.


Defining a Command

Inherit BaseCommand and implement the required members:

using ScavLib.command;

public class HealCommand : BaseCommand
{
    public override string Name => "heal";
    public override string Description => "Fully heal the player.";

    public override void Execute(string[] args)
    {
        ScavLib.util.PlayerUtil.HealAll();
        ConsoleScript.instance.ExecuteCommand("log [HealCommand] Player healed.");
    }
}

Then register it in your plugin's Awake():

private void Awake()
{
    CommandRegistry.Register(new HealCommand());
}

BaseCommand Members

Member Type Required Description
Name string Yes Command name as typed in the console. No spaces.
Description string Yes Short description shown in console help.
ArgDescription (string, string)[] No Per-argument descriptions. Each entry is (shortDesc, longDesc).
ArgAutofill Dictionary<int, List<string>> No Auto-complete candidates. Key is argument index (0 = first arg after command name).
Execute(string[] args) void Yes Command logic. args[0] is the command name itself.

Adding Argument Descriptions and Auto-fill

public class SpawnCommand : BaseCommand
{
    public override string Name => "mymod_spawn";
    public override string Description => "Spawn an item at the player's position.";

    public override (string, string)[] ArgDescription => new (string, string)[]
    {
        ("string id", "The resource ID of the item to spawn.")
    };

    public override Dictionary<int, List<string>> ArgAutofill =>
        new Dictionary<int, List<string>>
        {
            {
                0, new List<string>
                {
                    "bandage", "rifle", "pistol", "medkit"
                }
            }
        };

    public override void Execute(string[] args)
    {
        if (args.Length < 2)
        {
            ConsoleScript.instance.ExecuteCommand("log Usage: mymod_spawn <id>");
            return;
        }

        string id = args[1];
        var go = ScavLib.util.GameUtil.SpawnAtPlayer(id);

        string result = go != null ? $"Spawned '{id}'." : $"Unknown item '{id}'.";
        ConsoleScript.instance.ExecuteCommand("log " + result);
    }
}

Naming Convention

Prefix your command name with a short mod identifier to avoid collisions with built-in commands and other mods:

mymod_spawn     ✔
spawn           ✗  (may conflict with a built-in command)

Duplicate Registration

If a command with the same name is already registered, CommandRegistry will log a warning and skip the duplicate. Check the BepInEx log if your command does not appear in the console.

Clone this wiki locally