Enums and references in code
Enums become C# enums, and reference fields become properties that hand you the other record directly. Here's how to use both, and what happens when something they depend on changes.
Enums
Each enum becomes a C# enum with one member for each value. It sits inside the class of the object type that owns it, and its name is the enum's name with Enum added. The member name is the value's C# name, and its number is the number shown in Stoatly.
if (weapon.Element == Weapon.ElementEnum.Flame)
{
// ...
}A field of the enum type is nullable, so an empty field is null. A required enum field is never null. A list of enum values is a List of the enum.
- A deprecated value is still in the enum, marked
[Obsolete], so old records still read and your code gets a warning where it uses the value. - A deleted value is gone. Only values that have never shipped can be deleted.
- The C# name of a shipped value doesn't change when you rename its label, so your code keeps working. Changing it on purpose is a breaking change.
References
A reference field is a property of the target's class. The record is loaded when you read the property.
var weapon = hero.StartingWeapon; // a Weapon, or null
var damage = hero.StartingWeapon?.Damage;A reference field is null when it's empty or when the record it points at no longer exists. A target can be any record of the target type or of its child types, so a field that targets Item can hold a Weapon. Check with a pattern if you need the specific class:
if (hero.Trinket is Weapon w) { /* ... */ }Lists of references
A list of references is an IEnumerable of the target class. Entries that no longer resolve are skipped.
foreach (var loot in boss.Drops)
{
// loot is a Weapon, Armor or other Item
}Quantised lists
A quantised list is a List<QuantisedEntry<T>>. Each entry has a Quantity, the Ref slug and the loaded record as Value.
foreach (var entry in recipe.Ingredients)
{
var item = entry.Value; // the record, or null
var count = entry.Quantity; // for example 3
}When the target type goes away
If a reference field's target object type is deleted, the property is generated as the target's slug, a string?, or a List<string> for lists, so the code still compiles. You'll need to update the code that used the record.