< Dice‎ | RollerConfig
Revision as of 21:35, 4 August 2017 by Skizzerz (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

RollerConfig.FunctionRegistry Property

From DiceRoller Documentation

Gets or sets the registry of all valid macro names and their callbacks.

  • Namespace: Dice
  • Assembly: DiceRoller (in DiceRoller.dll)

Syntax

public MacroRegistry MacroRegistry { get; set; }

Property Value

Type: Dice.MacroRegistry

The function registry containing registered function callbacks.

Remarks

RollerConfig is initialized with an empty MacroRegistry. Attempting to use a macro not in the registry reports an error of DiceErrorCode.InvalidMacro.

Examples

The following example registers a number of macro callbacks.

using Dice;

class Sample
{
    public static void Main()
    {
        Roller.DefaultConfig.MacroRegistry.RegisterMacro("STR-mod", MacroCallback1);
        Roller.DefaultConfig.MacroRegistry.RegisterMacro("DEX-mod", MacroCallback1);
        Roller.DefaultConfig.MacroRegistry.RegisterMacro("CON-mod", MacroCallback1);
        Roller.DefaultConfig.MacroRegistry.RegisterGlobalMacro(MacroCallback2);
        var result1 = Roller.Roll("1d20+[WIS-mod]"); // rolls 1d20+2
        var result2 = Roller.Roll("1d20+[proficiency]"); // throws DiceException with DiceErrorCode.InvalidMacro
    }

    public static void MacroCallback1(MacroContext context)
    {
        switch (context.Name)
        {
            case "STR-mod":
                context.Value = 0;
                break;
            case "DEX-mod":
                context.Value = 3;
                break;
            case "CON-mod":
                context.Value = -1;
                break;
        }
    }

    public static void MacroCallback2(MacroContext context)
    {
        switch (context.Name)
        {
            case "INT-mod":
                context.Value = -1;
                break;
            case "WIS-mod":
                context.Value = 2;
                break;
            case "CHA-mod":
                context.Value = 0;
                break;
        }
    }
}