Table of Contents

Console Commands and Message Boxes

IConsoleCommandRegistrar adds console commands that run C# handlers, and IMessageBoxPrompter shows a message box and reports which button was pressed.

This plugin adds a command that shows a message box with a button for each argument:

using SkyrimForge.Game;
using SkyrimForge.Menus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SkyrimForge.ExamplePlugins;

/// <summary>Adds the console command SkyrimForgeButtons, which shows a message box with a button per argument and writes the pressed button to the console.</summary>
public sealed class ConsoleCommandPlugin(IConsoleCommandRegistrar consoleCommandRegistrar, IMessageBoxPrompter messageBox, IGameSession session, IConsole console, ILog log) : IPlugin
{
    private const string
        commandName = "SkyrimForgeButtons",
        shortName = "sfbuttons";

    /// <inheritdoc/>
    public void Start()
    {
        consoleCommandRegistrar.Register(commandName, shortName, "Shows a message box with one button per argument, up to five.",
            ["Button1", "Button2", "Button3", "Button4", "Button5"], ShowButtonsAsync);
        log.Write($"Enter \"{commandName} First Second Third\" in the console to show a message box with three buttons.");
    }

    private async Task ShowButtonsAsync(ObjectReference? target, IReadOnlyList<string> arguments)
    {
        IReadOnlyList<string> buttons = arguments.Count > 0 ? arguments : ["OK"];
        string reference = target is { } targetReference ? $"{targetReference.FormId}" : "no reference";
        Report($"{commandName} ran on {reference} with {arguments.Count} argument(s){(arguments.Count > 0 ? $": {string.Join(", ", arguments)}" : "")}.");
        try
        {
            int pressedIndex = await messageBox.ShowAsync($"{commandName} received {arguments.Count} argument(s).", buttons, session.NewGameOrSaveLoading);
            Report($"{commandName}: button {pressedIndex} (\"{buttons.ElementAtOrDefault(pressedIndex)}\") was pressed.");
        }
        catch (OperationCanceledException)
        {
            Report($"{commandName}: no button was pressed before the game closed the message box or a new game or a save started loading.");
        }
        catch (InvalidOperationException ex)
        {
            Report($"{commandName}: the message box was not shown: {ex.Message}");
        }
    }

    private void Report(string message)
    {
        console.Write(message);
        log.Write(message);
    }
}
  • Register takes the command's name, an optional short name, its help text, and one name per argument. Every argument is optional text.
  • The handler receives the arguments entered and the command's target: the reference selected in the console, the one named as in player.sfbuttons, or null. It runs on the game's thread for a command typed in the console. The console does not wait for its task, and an exception the handler throws, or one its task faults with, is written to the plugin's log.
  • The console's command table has a fixed size, so each command takes the place of an entry the game has disabled. A command remains until the game exits.
  • The task ShowAsync returns completes with the index of the button pressed, and is canceled if the game destroys the message box without a button press. Passing NewGameOrSaveLoading as its cancellation token also cancels it as a new game starts or a save starts loading.
  • Canceling the token ends only the wait. The box opens or stays open, and a button pressed on it leaves the task canceled.