# Turn data into gameplay

Lesson 6 · Build

Move generated content through typed validation and semantic checks before the game can load it.

## Outcomes

- Define a stable data contract between generation and gameplay.
- Separate schema validity from game-rule validity.
- Prove that validated relay data changes a playable scene.

Prerequisites: Lesson 5 and its task-specific context packets.

Deliverable: A versioned relay-data contract, fixture set, and in-game integration proof.


## Generated text is not game data

A game needs predictable values, not a persuasive paragraph. Put a contract between any AI-assisted generator and the engine. The generator proposes data; a validator accepts or rejects it; only accepted data enters the playable build.

Schema-constrained generation can improve output shape, but shape is not truth. The [Structured Outputs guide](https://developers.openai.com/api/docs/guides/structured-outputs) notes that applications still need to handle refusals and incomplete or semantically wrong responses. Your integration must assume that valid-looking data can still be unplayable.

## Define the relay contract

Pseudocode for a small, engine-neutral record:

```text
RelayRecord v1 {
  id: one of [moss, ridge, floodplain]
  displayName: non-empty string, max 32 characters
  requiredCells: integer from 1 to 2
  position: { x: number, y: number }
  feedbackKey: known localization key
  repaired: false at new-game load
}
```

Keep authored state separate from save state. The content file can say what a relay costs and where it starts. The save file records whether this particular run repaired it. If generation overwrites progress, the contract mixed two kinds of truth.

Include a contract version in every file so migrations fail visibly instead of loading old data by accident.

## Validate shape, then meaning

Use layers:

1. **Parse:** is the file valid JSON or the chosen format?
2. **Schema:** are fields present and correctly typed?
3. **Semantic rules:** are IDs unique, positions inside the map, costs achievable, and feedback keys known?
4. **Integration:** does the engine instantiate the relay and update the actual win state?

A record can pass the first two and still place a tower inside water. A set can pass each record individually and still demand more cells than exist. Semantic checks should inspect the collection and the game rules.

## Signal Garden integration

Create three fixture files: one valid, one structurally invalid, and one structurally valid but impossible. Load only the valid fixture into a test scene. Collect a cell, repair Moss, and confirm four effects: the cell count falls, the repaired set gains `moss`, the visual changes, and the objective count becomes 1/3.

Persist only explicit state. Engine documentation on [saving games](https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html) illustrates why runtime objects usually need a deliberate serializable representation. The principle applies across engines: save stable identifiers and values, not opaque scene objects.

## Failure modes and tradeoffs

**Schema worship** assumes typed output is correct output. **Silent coercion** converts the string two or a null value into a default and hides a generation fault. **Manual glue** requires a developer to rewrite every accepted file before the game can read it. **Live generation at load** makes the base game depend on latency and network access.

Strict rejection can slow experimentation, while permissive repair can conceal defects. During development, report exact errors and allow a deliberate regeneration step. In a shipped offline build, package prevalidated data and use known fallbacks. Never let a runtime model invent a rule that determines whether the player can finish.

## Build task: prove the full path

Write the contract and the three fixtures. Implement or simulate the four validation layers. Then capture evidence from the game or a minimal test harness showing that the accepted record changes gameplay state.

```text
Generate three Signal Garden relay records that follow RELAY CONTRACT v1.
Return data only in the requested structure. Do not invent fields.
Before returning, check: unique IDs, each requiredCells is 1 or 2,
all feedbackKey values come from the supplied list, and total required cells
does not exceed the supplied world inventory. If constraints conflict, stop
and report the conflict instead of producing records.
```

## Completion evidence

The structural fixture must fail at the schema layer. The impossible fixture must pass schema and fail semantics. The valid fixture must load without manual editing and drive a complete repair interaction. Record the contract version beside the fixture results. You are not done when a file exists; you are done when validated data causes the intended, observable state transition.



## Knowledge check

A relay record matches the schema but requires four cells when the game contains only three. What failed?

1. Syntax parsing.
2. Semantic validation.
3. File naming only.

Answer: 2. The shape is valid, but the values violate the playable game's resource rules.

## Further reading

- [Structured model outputs](https://developers.openai.com/api/docs/guides/structured-outputs) — OpenAI
- [Saving games](https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html) — Godot documentation

Sources checked 1 December 2026. Signal Garden is an original teaching scenario.
