Difference between revisions of "Dice/RollerConfig/MacroRegistry"

From DiceRoller Documentation
(Created page with "{{APIdoc|RollerConfig.FunctionRegistry Property}} Gets or sets the registry of all valid macro names and their callbacks. {{ns}} == Syntax == <syntaxhighlight lang="C#"> pub...")
 
 
Line 16: Line 16:
 
== Remarks ==
 
== Remarks ==
 
RollerConfig is initialized with an empty MacroRegistry. Attempting to use a macro not in the registry reports an error of [[Dice/DiceErrorCode|DiceErrorCode.InvalidMacro]].
 
RollerConfig is initialized with an empty MacroRegistry. Attempting to use a macro not in the registry reports an error of [[Dice/DiceErrorCode|DiceErrorCode.InvalidMacro]].
 +
 +
== Examples ==
 +
The following example registers a number of macro callbacks.
 +
<syntaxhighlight lang="C#">
 +
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;
 +
        }
 +
    }
 +
}
 +
</syntaxhighlight>

Latest revision as of 21:35, 4 August 2017

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;
        }
    }
}