< Dice
DiceFunctionAttribute Class
From DiceRoller Documentation
An attribute that can be applied to methods which denotes the method is a valid FunctionCallback. The FunctionRegistry.RegisterType function can be used to register types which contain methods marked with this attribute.
- Namespace: Dice
- Assembly: DiceRoller (in DiceRoller.dll)
Inheritance Hierarchy
System.Object
- System.Attribute
- Dice.DiceFunctionAttribute
Syntax
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class DiceFunctionAttribute : Attribute
Constructors
Name | Description | |
---|---|---|
DiceFunctionAttribute(String) | Constructs a new instance of DiceFunctionAttribute |
Properties
Name | Description | |
---|---|---|
Name | Gets the name of the dice function, which is not necessarily the same as the name of the method the attribute is attached to. | |
Scope | Gets or sets the scope of the dice function. | |
Timing | Gets or sets the timing of the dice function. |
Remarks
This attribute can be applied to a callback to mark it as a dice function. In addition to using the attribute, the type containing the decorated methods must be registered via FunctionRegistry.RegisterType. This attribute should only ever be applied to public methods whose signature matches FunctionCallback.
Examples
The following example demonstrates using DiceFunctionAttribute to register two dice functions, and then registering the type containing those functions.
using System;
using System.Collections.Generic;
using Dice;
class Sample
{
public static void Main()
{
FunctionRegistry.RegisterType(typeof(Sample));
var result = Roller.Roll("b(1d6.a())");
Console.WriteLine(result.Value); // prints 2, regardless of what the 1d6 rolled
}
[DiceFunction("a", Scope = FunctionScope.Roll)]
public static void A(FunctionContext context)
{
context.Value = 1;
context.Values = new List<DieResult>()
{
new DieResult() { DieType = DieType.Literal, NumSides = 0, Value = 1, Flags = 0 }
};
}
[DiceFunction("b")]
public static void B(FunctionContext context)
{
context.Value = context.Arguments[0].Value + 1;
context.Values = context.Arguments[0].Values;
}
}