Skip to Content
Stoatly Docs
Unity

Generated code

Stoatly turns your schema into C# classes and your records into JSON files, then sends both to your repository in a pull request. Your game reads its data through typed code, so a misspelt field name is a compile error and not a bug at runtime. You don't need a plugin for any of it. The files live in the folders set in the Unity settings.

What is generated

  • One class for each object type. It has a property for each field and a TypeSlug constant. A type that inherits from another derives from the parent's class.
  • One C# enum for each enum. They go in an Enums folder inside the code folder.
  • A loader. It reads the record files at runtime, using either Resources or Addressables.
  • A registry. It maps each object type's slug to its class, so a record always loads as its own type.
  • An assembly definition file, so the generated code compiles as its own assembly.
  • One JSON file for each record, in a folder named for its object type.

Formula fields become properties too, named with Field on the end (a field with the key dps is DpsField), and runtime formulas become methods on the class. Visual fields aren't generated, because they only draw a chart and carry no data. You never edit generated files, because the next pull request replaces them.

Where the data lives

Record files follow the layout data folder / object type slug / record slug.json. By default the data folder is Assets/Resources/Data and the code folder is Assets/Code/Data.

Load data in your game

Every object type's class has the same loading methods.

// One record, by its slug. Returns null if there is no such record.
var sword = Weapon.Load("iron-sword");

// Every record of the type.
foreach (var weapon in Weapon.LoadAll())
{
    // ...
}

// Every record of the type and of the types that inherit from it.
var all = Weapon.LoadAllIncludingChildren();

Records are cached, so loading the same record twice returns the same object, however you reach it.

Loading with Addressables

With the Addressables loader, reading is asynchronous. Warm the cache during a loading screen to avoid blocking reads later.

await Weapon.PreloadAsync();
await Weapon.PreloadIncludingChildrenAsync();
await StoatlyRegistry.PreloadAllAsync();

Keep slugs stable

The slug you pass to Load is the record's slug. Changing a record's slug renames its file and breaks every call that uses the old one.

On this page