# Data tables

> Keep enemy stats, loot, prices and level curves in typed tables with columns, checked rows, defaults and CSV import, and read them from scripts and Blueprints.

Source: https://feather-engine.com/docs/logic/data-tables/

A data table keeps gameplay numbers and content out of the code. A plain table is any JSON that
scripts read with `data("name")` (see [World logic](/docs/logic/world-logic/#data-tables)). A
**typed table** adds columns: every row has the same fields, each field has a kind, a default and
optional limits, and every change is checked before it is saved. The editor shows a typed table as a
grid, and it imports and exports CSV, so you can balance a game in a spreadsheet.

A typed table is an object of rows by name:

```json
{ "goblin": { "health": 30, "damage": [[1, 4], [10, 40]] }, "orc": { "health": 80, "tier": "elite" } }
```

A row can leave a cell out; reading it returns the column's default. **Curve** columns hold
`[x, y]` keys, so one cell can describe a value that changes with level, distance or time.

## Make a typed table

1. Open **Window → Game logic** and select the **Data tables** tab.
2. Type a name (lowercase letters, digits and `_`) and click **+ Add table**.
3. Click **Make typed table**. The columns are guessed from the rows already in the table, so an
   empty table starts with none. The button is shown when the table's JSON is an object of rows.
4. Click **Columns…**. Type a column id, pick its kind and click **+ Add column**. In the column list,
   set its **Default**, its **Range / options / target** and whether it is **Required**.
5. Type a row name and click **+ Add row**. Edit the cells in the grid; an edit is saved when you
   leave the cell, through the project's undo history.

Cells that use the column default are dimmed. Edit a row's name to rename it, and click the bin to
remove it. **Make plain JSON** drops the columns and keeps the rows as they are. The tab is locked
during Play.

## Example: enemy stats with a damage curve

You will add an `enemies` table to the **Ocean Voyage** example with a health number, a tier and a
damage curve by level, and read it from a world script.

1. Choose **File → Open example → Ocean Voyage**, then **File → Save project** to keep your own copy.
2. Open **Window → Game logic**, select **Data tables**, type `enemies` and click **+ Add table**.
   Click **Make typed table**, then **Columns…**.
3. Add a column `health` of kind **Integer**. Set its **Default** to `10`, and type `1` and `1000`
   into its min and max fields.
4. Add a column `tier` of kind **Enum**. Replace its options with `common, elite`, then set its
   **Default** to `common`.
5. Add a column `damage` of kind **Curve**.
6. Add a row `goblin`. Set **health** to `30` and type `[[1, 4], [10, 40]]` into **damage**.
7. Click **Import CSV…**, paste the text below and click **Import**:

   ```text
   name,health,tier,damage
   orc,80,elite,"[[1,10],[10,90]]"
   bat,,,
   ```

8. Select **World scripts**, click **New**, name it `Enemies` and click **Create**. Replace the
   template with the script below and press **Cmd/Ctrl+Enter**.

```rust
// Reads enemy stats from the typed "enemies" table.
fn start() {
    let goblin = row("enemies", "goblin");
    print("Goblin: " + goblin.health + " health, tier " + goblin.tier);
    print("Bat health: " + cell("enemies", "bat", "health"));
    print("Goblin damage at level 4: " + curve_at("enemies", "goblin", "damage", 4));
    print("Elite enemies: " + rows_where("enemies", "tier", "elite"));
}
```

Do the same from the command line, on a copy of the example. Save the script above as `enemies.rhai`
in the workspace folder.

```bash
cp -R examples/ocean-voyage /tmp/demo
cd /tmp/demo   # put enemies.rhai here
P="--workspace /tmp/demo --project project.feather.json"

feather-cli $P command table.define --save project.feather.json \
  --json '{"name":"enemies","schema":{"description":"Enemy stats","columns":[{"id":"health","kind":"Integer","default":10,"min":1,"max":1000},{"id":"tier","kind":"Enum","options":["common","elite"],"default":"common"},{"id":"damage","kind":"Curve"}]}}'
feather-cli $P command table.row_upsert --save project.feather.json \
  --json '{"name":"enemies","row":"goblin","values":{"health":30,"damage":[[1,4],[10,40]]}}'
feather-cli $P command table.import_csv --save project.feather.json \
  --json '{"name":"enemies","csv":"name,health,tier,damage\norc,80,elite,\"[[1,10],[10,90]]\"\nbat,,,\n"}'

jq -n --rawfile src enemies.rhai '{kind:"world",source:$src}' | \
  feather-cli $P command script.check --json -
jq -n --rawfile src enemies.rhai '{name:"Enemies",enabled:true,source:$src}' | \
  feather-cli $P command world_script.set --json - --save project.feather.json
```

**Check it worked.** Press **Play**. The Console shows `Goblin: 30 health, tier common`,
`Bat health: 10` (the bat row is empty, so it reads the defaults), `Goblin damage at level 4: 16.0`
and `Elite enemies: ["orc"]`. Without the editor, run
`feather-cli $P command game.simulate --json '{"frames":10}'`: the same four lines are in
`data.report.logs`. `table.export_csv` with `{"name":"enemies"}` returns the three rows with every
default written out.

## Column kinds

| Kind | Cell value | Default when not set |
| --- | --- | --- |
| **Number** | A number, within the column's min and max | `0` |
| **Integer** | A whole number, within min and max | `0` |
| **Toggle** | `true` or `false` | `false` |
| **Text** | Text, up to 2,000 characters | `""` |
| **Color** | `[r, g, b]`, each 0–1 | `[1, 1, 1]` |
| **Vec3** | `[x, y, z]` | `[0, 0, 0]` |
| **Enum** | One of the column's options | `""` |
| **Reference** | The ID or name of something in the project; see below | `""` |
| **List** | Up to 256 texts, each up to 200 characters | `[]` |
| **Curve** | Up to 256 keys `[x, y]` with x increasing; each y within min and max | `[]` |

A **Reference** column names what it points at, and each cell must match one that exists:

| Target | Matches |
| --- | --- |
| `item` | An item ID |
| `sound` | A sound or audio cue name |
| `prefab` | A prefab name |
| `effect` | An effect ID, including the built-in water effects |
| `material` | A material name |
| `level` | A level ID |
| `variable` | A game variable ID |
| `row:<table>` | A row name in another data table |

The **Columns…** list offers every target except `row:<table>`, which you set with
`table.column_upsert`. When a row is renamed, cells in other tables that point at it with
`row:<table>` follow the new name.

## Column settings

| Field | Meaning |
| --- | --- |
| `id` | The field name in each row: lowercase letters, digits and `_`, starting with a letter, up to 48 characters |
| `label` | Shown as the column title instead of the ID; up to 80 characters |
| `kind` | One of the kinds above. Changing it in the editor resets the default; the change is refused while rows have cells that do not fit |
| `default` | What a row reads when it leaves the cell out |
| `min`, `max` | Optional limits for Number, Integer and Curve values |
| `unit` | Up to 16 characters |
| `description` | Up to 400 characters; shown when you hover the column title |
| `required` | Every row must set it; empty text or an empty reference counts as not set |
| `options` | Enum choices: 1 to 64, each 1 to 64 characters |
| `reference` | Reference target |

A table can also have a `description` of up to 1,000 characters, shown above the grid.

## Script and Blueprint access

Every world script, object script and Blueprint can read data tables. The row functions work on any
table that is an object of rows; in a typed table, cells a row leaves out return the column default.

| Function | Returns |
| --- | --- |
| `row(table, name)` | The row as a map, with every column filled in |
| `cell(table, row, column)` | One cell |
| `rows(table)` | The row names, sorted |
| `has_row(table, name)` | Whether the row exists |
| `rows_where(table, column, value)` | The names of the rows whose cell equals `value`, sorted |
| `curve_at(table, row, column, x)` | A Curve cell's value at `x`: linear between keys, and the first or last value beyond the ends |
| `data(table)` | The whole table as stored, without defaults |

A missing table, row or column is a runtime error that says what is missing. In Blueprints
the same functions are **Call** nodes in the **Data tables** group; **Row** returns a Map and
**Curve At** a Number.

## CSV

Use **Copy as CSV** to copy a typed table to the clipboard, and **Import CSV…** to paste rows in.

- The first line is the header: a `name` column for the row name, then column IDs. A column the table
  does not have is an error.
- Numbers, whole numbers and text are read as they are. Toggles accept `true`, `yes`, `1`, `false`,
  `no` and `0`. Vec3, Color, List and Curve cells hold JSON, such as `"[[1,10],[10,90]]"`.
- An empty cell is left out of the row, so the column default applies.
- **Import** merges rows by name; **Replace all rows** clears the table first. The import is checked as
  a whole and is one undo step.
- Export writes every column, with defaults filled in.

## Limits

| Limit | Value |
| --- | --- |
| Columns per table | 64 |
| Rows per table | 4,096 |
| Row names | 1 to 64 characters, without spaces at either end |
| CSV import | 1 MiB |
| Data tables per project | 64, up to 1 MiB each |

## Automate it

| Command | Purpose |
| --- | --- |
| `table.list` | Every data table with its row count, and its columns when typed |
| `table.get` | A table's columns and rows; with `row`, that row as stored and `resolved` with defaults filled in |
| `table.define` | Sets a table's columns (`schema`), guesses them from its rows (`infer: true`) or makes it plain JSON (`schema: null`). Creates the table if needed. `migrate: true` drops cells of removed columns instead of refusing |
| `table.column_upsert`, `table.column_remove` | Adds or changes one column (`rename_from` renames it and moves its cells), or removes it and its cells |
| `table.row_upsert`, `table.row_remove` | Adds or changes one row (`values` are merged into it; `rename_from` renames it), or removes it |
| `table.import_csv`, `table.export_csv` | Reads rows from CSV text (`replace` to clear first), or returns the table as CSV |

```bash
feather-cli command table.define \
  --json '{"name":"loot","infer":true}'
```

See [Agents & automation](/docs/agents/overview/) for connecting a live session.
