Skip to content

Adding Custom Base Types

Jimmy Cushnie edited this page Nov 12, 2019 · 4 revisions

Base Types are types which have specific logic for turning them into text and back into data again. All other types are composed of base types. SUCC comes with 16 base types, but you can add your own if you need to.

To add your type as a custom base type, you need two things:

  • a SerializeMethod for turning instances of your type into text
  • a ParseMethod for turning text into instances of your type

Note that your SerializeMethod is not allowed to return a string with any newlines or tab characters in it.

To add your type as a base type, you need to call SUCC.BaseTypes.AddBaseType. It is recommended that you do this in your type's static constructor

Example

using SUCC;

public class SomeCustomBaseType
{
    public int ID;

    // this is the SerializeMethod
    public override string ToString()
        => "SCBT-" + ID;

    // this is the ParseMethod
    public static SomeCustomBaseType Parse(string text)
    {
        if (text.Substring(0, 5) != "SCBT-")
            throw new FormatException();

        return new SomeCustomBaseType()
        {
            ID = int.Parse(text.Substring(5))
        };
    }
    
    
    static SomeCustomBaseType()
    {
        BaseTypes.AddBaseType
            (
            serializeMethod: SCBT => SCBT.ToString(),
            parseMethod: SomeCustomBaseType.Parse
            );
    }
}