calculatorRandom, Math, & More

This page is a grab-bag of “you’ll use these constantly” utilities: RNG, formatting, time, and a few core subsystems that show up in most games.

circle-info

If you’re looking for arrays/slices, see Arrays and Collections.

Random numbers (RNG)

Random uses an explicit u64 seed. Pass the seed by ref so it updates.

rng: u64 = rng_seed_time();
// or deterministic per-entity:
// rng: u64 = rng_seed(entity.id);

// Range values are inclusive
roll := rng_range_int(ref rng, 1, 10);
chance := rng_range_float(ref rng, 0, 1);
circle-exclamation

Math functions

Common math helpers:

angle_sin := sin(x);
angle_cos := cos(x);

result := pow(2.0, 3.0);   // 8.0
root := sqrt(16.0);        // 4.0

value := lerp(0.0, 100.0, 0.5);  // 50.0
clamped := clamp(value, 0.0, 10.0);

absolute := abs(-5);
minimum := min(5, 10);
maximum := max(5, 10);

len := length(v);
len_sq := length_squared(v);
normalized := normalize(v);

String formatting

Use % placeholders with an argument array:

If you need an argument adjacent to a percent sign (or next to another argument), use %0 as an alias for %:

For decimal rounding, use format_float:

Logging

Logging follows the same formatting rules:

Time

SFX

circle-exclamation

Economy quick start (currencies)

Economy currencies are per-player and automatically persisted.

See Economy for the full guide.

Last updated