Skip to Content
Stoatly Docs
Unity

Load data in your game

The generated code gives your game typed access to its records. This goes beyond the basics in Generated code: how the loader starts, how records are cached and how to avoid stalls. You don't need to write any setup code.

The loader starts itself

The generated loader installs itself before the first scene loads. You don't create it or call anything first. Until it has installed, the data layer has no data source, which is also why the generated data code can run in tests without Unity.

Resources

With the Resources loader, reading is immediate. A single record is read from its file, and LoadAll reads every file in the type's folder. Everything in the data folder is built into the player, so large data sets grow your build. The data folder must be inside a Resources folder.

Addressables

With the Addressables loader, records are loaded asynchronously. Because Addressables can't list a folder, Stoatly writes a small records.json for each object type that lists its records. The loader reads that to find them.

Warm the cache before you need the data, for example on a loading screen:

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

If your code asks for a record that isn't loaded yet, the call still works. It waits for the load to finish, which blocks the frame, and logs a warning that names the type and tells you to call PreloadAsync(). The warning is shown once for each type.

Records are cached

Each record is loaded once. Every later request, however you reach it, returns the same object. That includes Load, LoadAll, a reference property and LoadAllIncludingChildren. Two consequences follow:

  • Properties are read only, because changing a shared object would change it for every reader.
  • A request for a record that doesn't exist returns null, and Stoatly remembers that, so asking again is cheap.

Clearing the cache

Call StoatlyObject.ClearAllCaches() to drop every cached record, or StoatlyObject.ClearCache("weapon") to drop one type. Do this if you swap the underlying data while the game runs, for example in an editor tool. The loader also clears the cache as it installs.

The registry

StoatlyRegistry knows every object type in the project.

MemberWhat it does
TypeSlugsThe slugs of every object type
Create(typeSlug)Makes an empty record of the right class for a slug
PreloadAllAsync()Preloads every object type

A record is always loaded as its own class. Loading everything under Item gives you Weapon and Armor instances, with their own fields, not trimmed copies of Item.

Enums and references in code have their own page: Use enums and references in code.

On this page