reflect

Compile-time type reflection, field accessors, and streaming binary/JSON codecs for Garnet

Garnet Reflect

Zero-dependency compile-time type reflection, zero-allocation field accessors, and streaming binary/JSON codecs shared across Garnet's ECS, scene authoring, save/load, animation, expressions, and multiplayer replication layers.

Usage

Include Garnet::Reflect::Object in any struct or class and annotate fields with @[Garnet::Reflect::Field]:

require "garnet-reflect"

@[Flags]
enum CollisionLayer : UInt32
  World
  Player
  Enemy
end

@[Garnet::Reflect::Type(id: "game.health", version: 1, role: :component, category: "Gameplay")]
struct Health
  include Garnet::Reflect::Object

  @[Garnet::Reflect::Field(min: 0, max: 500, replicate: true, quantize: {0, 500, 12}, animate: true, save: true)]
  getter current : Float32 = 100_f32

  @[Garnet::Reflect::Field(min: 1, max: 500, replicate: true, save: true)]
  getter max : Float32 = 100_f32

  @[Garnet::Reflect::Field(visible_if: "current < max")]
  getter regen_rate : Float32 = 5_f32

  @[Garnet::Reflect::Field]
  getter layers : CollisionLayer = CollisionLayer::Player

  @[Garnet::Reflect::Field(save: true)]
  getter resistances : Hash(String, Float32) = {"fire" => 0.5_f32}

  def initialize(@current = 100_f32, @max = 100_f32, @regen_rate = 5_f32,
                 @layers = CollisionLayer::Player,
                 @resistances = {"fire" => 0.5_f32})
  end
end

Generated capabilities

  • Metadata: Health.reflect_type_info returns a memoized TypeInfo with FieldInfo, FieldKind, Constraint, VisibilityRule, and capability flags.
  • Zero-allocation direct accessors: Health.reflect_resolve_path("current") resolves a compile-time FieldPath(field_index, lane) once; #reflect_read_scalar, #reflect_with_scalar, #reflect_set_scalar, and .reflect_write_scalar_ptr read and mutate values without Hash or JSON::Any allocations.
  • Bitmask binary delta codecs: #reflect_diff_mask(baseline, replicated_only: true), #reflect_write_binary(writer, mask), and .reflect_read_binary(reader, base) pack only changed fields with optional fixed-point float quantization and smallest-three quaternion encoding.
  • Streaming JSON codecs: #reflect_write_json(builder) and .reflect_read_json(parser) stream directly without intermediate JSON::Any DOM allocations.
  • Tagged unions: Garnet::Reflect.tagged_union Shape, BoxShape, SphereShape, tag_field: "kind" generates deterministic discriminator JSON, variant metadata, and 1-byte tagged binary serialization.
  • Open extensibility: Any sibling shard (garnet-math, garnet-color, garnet-animation, garnet-gameplay, garnet-ecs, garnet-graphics, garnet-scene-data) reopens Garnet::Reflect::Codec to register its own types with zero circular dependencies.
Repository

reflect

Owner
Statistic
  • 0
  • 0
  • 0
  • 8
  • 0
  • about 3 hours ago
  • December 5, 2026
License

MIT License

Links
Synced at

Sat, 05 Dec 2026 22:02:45 GMT

Languages