What is reflection metadata in a game engine, and how can one description of a component's fields drive the editor, save files and script bindings?
answer
- data about the type
- name, type, offset, flags
- generated, not hand-kept
- one table, many consumers
basics
~20 sReflection metadata is a run-time description of each type and its fields' names, types, locations and flags. Editors draw property panels from it, serializers save fields by name, and script bindings expose fields, with no per-type code.
solid answer
~50 sThe systems languages engines are usually written in have historically had no built-in runtime reflection, so engines build their own: a **registry** that describes each type, with its name, base type and a list of **fields** (name, type descriptor, offset or getter and setter) plus **flags** such as `editable`, `saved`, `transient` or `script_visible` and hints such as a value range. It is produced by registration macros or, more robustly, a **code generator** that parses annotated source as a build step. Generic code then walks it: the editor draws a widget per field type, the save system writes each `saved` field by name and reads it back, the script binder exposes `script_visible` fields, and undo can diff two snapshots field by field. Add a field once with the right flags and every consumer picks it up.
go deeper
Explain reflection as the engine describing its own types so tools can list fields, and name the editor property panel as the classic use.
List what a field entry holds, the flags that route fields to editor, saves and scripts, and the three ways the metadata gets generated.
Discuss transient fields, serializing by name instead of offset, rename aliases, and keeping reflection off hot paths with generated code.
Own the cost of a custom code generator in the build against the drift of hand-kept registration, and which systems may depend on the metadata.
## The problem reflection solves A game has hundreds of component types: health, door, quest giver, light, spawner. Each one needs to appear in the **editor** so designers can tune it, be written to and read from **save files**, and often be readable from the **scripting layer**. Writing a property panel, a save function, a load function and a script binding by hand for every type is slow, and the four drift apart: a field added to the component but forgotten in the load function silently resets on every load. **Reflection** means a program can inspect its own types at run time: list a type's fields, read their names and types, and get or set them on an instance. With that, each consumer is written **once**, generically, and works for every type. ## What the metadata holds The systems languages engines are usually written in have historically offered no built-in runtime reflection of this kind, so engines keep their own **type registry**. A typical entry holds: - **type identity**: a stable name or ID, the base type, the size, and a way to construct a default instance; - **fields**: for each one, its name, a **type descriptor** (float, vector, enum, handle to another object, array of something), and how to reach it, either a byte **offset** inside the object or a getter and setter pair; - **flags** on fields: `editable` (shown in the editor), `saved` (written to saves), `transient` (cached or derived data never written, such as a pointer to a mesh resolved at load), `script_visible`, `read_only`; - **hints** for tools: a numeric range, a display name, a tooltip, a category; - sometimes **functions** callable from scripts or the editor. ## How the metadata is produced There are three common approaches: 1. **Hand-written registration**: a function per type lists its fields. Simple, but it drifts from the real struct the moment someone forgets to update it. 2. **Macros** placed next to each field that expand into registration code. Less drift, but the macros clutter source and can be hard to debug. 3. A **code generator** run as a build step that parses annotated source files and emits the registration code. This is the most robust option and is widely used in large engines, at the cost of a custom build tool. ## One table, many consumers | Consumer | What it reads | What it does | |---|---|---| | Editor property panel | Fields flagged `editable`, their types and hints | Draws a slider, colour picker or object picker per field; writes edits back through the offset or setter | | Save and load | Fields flagged `saved` | Writes each as a name or ID plus value; on load, matches by name so reordering fields is harmless | | Script binding | Fields and functions flagged `script_visible` | Exposes them to designers' scripts without a hand-written wrapper per field | | Undo and redo | All editable fields | Snapshots and diffs an object field by field | | Debug inspector | Every field | Shows live values in a running game | The payoff is consistency: a designer adds a `respawn_delay` field to a spawner component, flags it `editable` and `saved`, and it appears in the editor, persists in saves and is readable from scripts with no further code. ## Traps - **Offsets are a run-time detail.** Writing a field's byte offset or the raw struct bytes to disk ties the file to one build's memory layout; serialize by **field name or stable ID** instead. - **Transient data must be flagged.** Saving a cached pointer or a derived bounding box produces garbage on load. - **Run-time cost.** Walking metadata and switching on type descriptors is slower than code generated per type. Engines often use reflection in the editor and for saves, and generate specialised code for hot paths. - **Metadata is a contract.** Renaming a field changes its saved name, so renames need an explicit alias recorded in the metadata, or old saves lose the value.
- Why is a code generator usually preferred over hand-written registration for reflection?Hand-written registration is a second copy of every struct that someone must remember to update, and forgetting is silent: a new field just never appears in the editor or saves. A generator derives metadata from the source itself on every build, so the two cannot drift.
- Why should reflection-driven saves write field names or IDs rather than byte offsets?Offsets change whenever a field is added, removed, reordered or resized, or when a compiler changes padding. A name or stable ID survives those changes, so loading can match each saved value to the right field in the current layout.
A shipping manifest for a container: instead of every port writing its own procedure per container, each container carries a list of what is inside, where and how to handle it, and every port reads the same list.
- Saving a component by copying its raw bytes is fine
- Every field should be saved, including cached pointers
- Reflection data is free to walk on hot gameplay paths
- Hand-maintained registration lists never drift from the code
- Renaming a reflected field has no effect on existing saves