Skip to Content
Stoatly Docs
Unity

Formulas in code

The three kinds of formula reach your game in three different ways. Here's each one, so the logic you tested in Stoatly is the logic your game runs.

Formula fields

A formula field is a read only property holding the value Stoatly calculated. Its name is the field's key in PascalCase with Field on the end, so a field with the key dps becomes DpsField.

float? dps = sword.DpsField;

The value is a nullable float. Stoatly calculates it, and writes it into the record's file, so your game doesn't repeat the calculation. If you need the result for values the game decides at runtime, use a runtime formula.

Runtime formulas

A runtime formula, one with parameters, becomes a static method on the object type's class. It takes the record first, then the parameters in the order you set.

// A formula with the key damage_taken and one parameter, armour.
float? taken = Weapon.DamageTaken(sword, armour: 12f);
  • The method name is the formula's key in PascalCase.
  • Parameter names are in camelCase, and their types follow the type you chose: float for Number, int for Integer, bool for Boolean and string for String.
  • The result is nullable, with the type Stoatly worked out.

The method runs in your game, so it uses the values on the record you pass in at that moment.

Random formulas

A formula that uses random values is always a static method, even when it has no parameters. It takes the record first, then any parameters, and it rolls a new result every time you call it.

// A formula with the key hit_damage and no parameters.
float? hit = Weapon.HitDamage(sword);
float? again = Weapon.HitDamage(sword);

Global formulas with random values work the same way, on Globals.

A formula field backed by a random formula is a StoatlyRange? property with Min and Max. It holds the lowest and highest result the formula can give, so it's the same on every read.

StoatlyRange? damage = sword.HitDamageRangeField;
Debug.Log($"{damage?.Min} to {damage?.Max}");

The Field ending keeps a formula field apart from a formula of the same key, so a formula field and a random formula can share a key. They only clash when the property name matches the method name, for example a formula field damage and a random formula damage_field. Stoatly says "A formula field and a formula that uses random values can't share the name" followed by the name. Rename one of them.

Seed or replace the random source

Random formulas draw from StoatlyRandom, which Stoatly generates only when the project has one. Seed it to get the same rolls on every run, which helps in tests.

StoatlyRandom.Seed(1234);

To control the rolls yourself, replace the source with your own implementation of IStoatlyRandomSource. Range returns a whole number from the minimum to the maximum, both included.

class FixedSource : IStoatlyRandomSource
{
    public int Range(int minInclusive, int maxInclusive) => maxInclusive;
}

StoatlyRandom.Source = new FixedSource();

GDScript support for random formulas is coming.

Global formulas

Global formulas become static methods on one class named Globals. They take no record, only their parameters.

// A global formula with the key xp_for_level and one parameter, level.
var xp = Globals.XpForLevel(level: 10);

Globals only exists when the project has global formulas.

Keep the behaviour the same

The code that runs in your game is generated from the same expression you tested in the editor, so the answers should match what you saw in Run on Record and Test values. If they ever differ, check the type of the result and whether the inputs are the same.

Changing a formula

A published runtime formula is part of your code's surface. Renaming it, or changing its parameters, breaks the calls that use it, and Stoatly warns you before you save. A formula that backs a formula field also breaks the property's type when it gains or loses random values, because the property changes between float? and StoatlyRange?. Read Keep your code working before you rename one.

On this page