Skip to content

Accessors

Accessors are the only way Economy reads and writes resource balances. There is no state scan and no nested path syntax.

ts
import { arrayIndex, computed, readonly, recordField, stateKey } from "@idlekitjs/economy";

stateKey

stateKey targets a top-level numeric state field.

ts
interface State {
  coins: number;
}

const coins = stateKey<State>("coins");

It reads state.coins and assigns the updated number back to the same field.

arrayIndex

arrayIndex targets one slot of a number array. Missing slots read as 0. Writes clone the array and call setArray.

ts
import { arrayIndex } from "@idlekitjs/economy/accessors";

interface State {
  producerTotals: number[];
}

const farms = arrayIndex<State>({
  getArray: (state) => state.producerTotals,
  setArray: (state, producerTotals) => {
    state.producerTotals = producerTotals;
  },
  index: 0,
});

Use this for producer columns and other indexed numeric pools.

recordField

recordField targets a numeric field inside a keyed record. Reads use defaultEntry() when the key is absent. Writes clone the record and the entry, then call setRecord.

ts
import { recordField } from "@idlekitjs/economy/accessors";

interface CardEntry {
  level: number;
  shards: number;
}

interface State {
  cards: Record<string, CardEntry>;
}

const managerShards = recordField<State, CardEntry>({
  getRecord: (state) => state.cards,
  setRecord: (state, cards) => {
    state.cards = cards;
  },
  key: "manager",
  field: "shards",
  defaultEntry: () => ({ level: 0, shards: 0 }),
});

computed

computed is a named pass-through for a custom get/add pair.

ts
import { computed } from "@idlekitjs/economy/accessors";

const doubledCoins = computed<State>({
  get: (state) => state.coins * 2,
  add: (state, amount) => {
    state.coins += amount / 2;
  },
});

It adds no behavior. Use it when the state shape does not match one of the standard accessors.

readonly

readonly exposes a value that Economy can observe but must not credit or spend directly.

ts
import { readonly } from "@idlekitjs/economy/accessors";

const cardLevel = readonly<State>((state) => state.cards.manager?.level ?? 0);

Resources built with readonly are usable in requirements and formatting. Transactions report readonly-resource if such a resource appears in cost or reward.