Reflection generator PrimalReflectGen #21

Open
UnflavoredMeson/Primal_Plus_Plus
opened 2026-11-24 20:15:54 +00:00 by UnflavoredMeson · 0 comments
Owner

Why

The editor edits fields of components and classes it doesn't know at compile time, and files store them by name. In the fork, scripts were registered three different ways before this was settled. Deciding it now means game code is written one way.

Decided

  • Unreal's model: marker macros that PrimalReflectGen (ReflectionTool/) reads before compiling. PRIMAL_COMPONENT(), PRIMAL_GAME_SCRIPT(), PRIMAL_ACTOR(), PRIMAL_PART(), PRIMAL_BEHAVIOR(), PRIMAL_ENUM() before the type; PRIMAL_GENERATED_BODY() inside; PRIMAL_PROPERTY(.min = 0.f, .tooltip = "...") before a member. The header's last include is "X.generated.h".
  • Components reflect every field; classes only PRIMAL_PROPERTY fields.
  • One registry, keyed by the FNV-1a hash of the name (the class name, or .name = "..."); never by index. A duplicate name is an error in every build.
  • Field types: bool, s32, u32, s64, u64, f32, v2-v4, m4x4, game_entity::Entity, PRIMAL_ENUM() enums, std::array of those; classes also std::vector/utl::vector and std::string. No C arrays. 64-bit integers are never held as double anywhere.
  • Integration: CMake primal_reflect(<target> ...) (cmake/PrimalReflection.cmake), MSBuild Engine/Reflection.targets.
  • C++26 reflection replaces the generator once clang and MSVC support it; the markers map to annotations.
  • C# scripting was prototyped and rejected (CSharpScripting).

Read first

Steps

  • 1. Write the runtime first (Engine/EngineAPI/Reflection.h, Reflection/Reflection.cpp) with hand-written registrations, and tests for lookup by name, fields and duplicate names.
  • 2. Write the generator: parse the markers in a header, emit X.generated.h with the same registrations. Golden-file tests in UnitTests/ for its output.
  • 3. Wire it into CMake and MSBuild; a header edit regenerates.
  • 4. Reflect the transform components; find a component by name (ecs::find_component(name_hash)).

Done when

  • Reflection tests and the generator's golden tests pass; a change to a marked header rebuilds on both build systems; 0 warnings; tidy 0.

Claude can help with

  • The generator's parsing edge cases (nested templates, default arguments in PRIMAL_PROPERTY).
  • MSBuild target ordering, which is easy to get subtly wrong.

Learn

  • Why name hashes, not indices, are what files and other binaries can rely on.
## Why The editor edits fields of components and classes it doesn't know at compile time, and files store them by name. In the fork, scripts were registered three different ways before this was settled. Deciding it now means game code is written one way. ## Decided - Unreal's model: marker macros that `PrimalReflectGen` (`ReflectionTool/`) reads before compiling. `PRIMAL_COMPONENT()`, `PRIMAL_GAME_SCRIPT()`, `PRIMAL_ACTOR()`, `PRIMAL_PART()`, `PRIMAL_BEHAVIOR()`, `PRIMAL_ENUM()` before the type; `PRIMAL_GENERATED_BODY()` inside; `PRIMAL_PROPERTY(.min = 0.f, .tooltip = "...")` before a member. The header's last include is `"X.generated.h"`. - Components reflect every field; classes only `PRIMAL_PROPERTY` fields. - One registry, keyed by the FNV-1a hash of the name (the class name, or `.name = "..."`); never by index. A duplicate name is an error in every build. - Field types: `bool`, `s32`, `u32`, `s64`, `u64`, `f32`, `v2`-`v4`, `m4x4`, `game_entity::Entity`, `PRIMAL_ENUM()` enums, `std::array` of those; classes also `std::vector`/`utl::vector` and `std::string`. No C arrays. 64-bit integers are never held as `double` anywhere. - Integration: CMake `primal_reflect(<target> ...)` (`cmake/PrimalReflection.cmake`), MSBuild `Engine/Reflection.targets`. - C++26 reflection replaces the generator once clang and MSVC support it; the markers map to annotations. - C# scripting was prototyped and rejected ([CSharpScripting](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/CSharpScripting)). ## Read first - [Reflection](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Reflection), sections 1, 3, 6-8. ## Steps - [ ] 1. Write the runtime first (`Engine/EngineAPI/Reflection.h`, `Reflection/Reflection.cpp`) with hand-written registrations, and tests for lookup by name, fields and duplicate names. - [ ] 2. Write the generator: parse the markers in a header, emit `X.generated.h` with the same registrations. Golden-file tests in `UnitTests/` for its output. - [ ] 3. Wire it into CMake and MSBuild; a header edit regenerates. - [ ] 4. Reflect the transform components; find a component by name (`ecs::find_component(name_hash)`). ## Done when - [ ] Reflection tests and the generator's golden tests pass; a change to a marked header rebuilds on both build systems; 0 warnings; tidy 0. ## Claude can help with - The generator's parsing edge cases (nested templates, default arguments in `PRIMAL_PROPERTY`). - MSBuild target ordering, which is easy to get subtly wrong. ## Learn - Why name hashes, not indices, are what files and other binaries can rely on.
Sign in to join this conversation.
No assignees
No project
Blocks
#22 Game scripts and the game library ABI
UnflavoredMeson/Primal_Plus_Plus
#23 Render components
UnflavoredMeson/Primal_Plus_Plus
#42 Project, scene and prefab files as JSON
UnflavoredMeson/Primal_Plus_Plus
#43 Actor runtime: actors, parts, behaviors
UnflavoredMeson/Primal_Plus_Plus
Depends on
#5 Unit tests with doctest
UnflavoredMeson/Primal_Plus_Plus
#11 The engine as one shared library
UnflavoredMeson/Primal_Plus_Plus
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
UnflavoredMeson/Primal_Plus_Plus#21
No description provided.