Component.define makes a live Component Schema, with fields and Awake (#75) #80

Closed
opened 2026-09-06 01:24:22 +00:00 by crowmaki · 0 comments
Owner

Parent

#75

What to build

A game developer writes local Mover = Component.define("game.mover", { speed = 1.5, enabled = true }) in a Script and gets a real Component Schema in Core. The default value's Luau type picks the Kind: a number is F64, a boolean is Bool; any other value type fails the define with an error naming the field. (The GameObject class as a default marks a Reference field, delivered in a later ticket.)

A Component instance reaching Luau is a userdata carrying the Component Handle with a shared metatable per class. Reading self.speed resolves the field index once per class and reads Core storage through component_data and the Schema field offset; writing goes the same way. No Component state lives in the VM. The userdata is cached per Handle in a weak table so identity comparison works within a frame.

Defaults are applied in the on_add trampoline, then Awake() runs, before the adding call returns. A Component defined with the same name in two different Scripts fails the second load with a log message.

Acceptance criteria

  • After load, engine_component_type finds the Schema by its defined name with fields matching the define, in the declared Kinds
  • Adding that Component from Odin with component_add reads back the defaults through component_data
  • Awake runs before component_add returns; a field it writes is visible from Odin immediately after the call
  • A field written from Odin is read back by the Script with the new value
  • A define with a string, table or nil default fails and logs an error naming the offending field; the Script load returns false
  • A second Script defining an already-defined Component name fails its load with a log line; the first Script's Schema and any instances are untouched
  • Reading or writing a field on a Component that has been destroyed raises a Luau error naming the operation
  • A Luau error inside Awake is logged with a traceback and component_add still returns

Blocked by

## Parent #75 ## What to build A game developer writes `local Mover = Component.define("game.mover", { speed = 1.5, enabled = true })` in a Script and gets a real Component Schema in Core. The default value's Luau type picks the Kind: a number is `F64`, a boolean is `Bool`; any other value type fails the define with an error naming the field. (The `GameObject` class as a default marks a Reference field, delivered in a later ticket.) A Component instance reaching Luau is a userdata carrying the Component Handle with a shared metatable per class. Reading `self.speed` resolves the field index once per class and reads Core storage through `component_data` and the Schema field offset; writing goes the same way. No Component state lives in the VM. The userdata is cached per Handle in a weak table so identity comparison works within a frame. Defaults are applied in the `on_add` trampoline, then `Awake()` runs, before the adding call returns. A Component defined with the same name in two different Scripts fails the second load with a log message. ## Acceptance criteria - [ ] After `load`, `engine_component_type` finds the Schema by its defined name with fields matching the define, in the declared Kinds - [ ] Adding that Component from Odin with `component_add` reads back the defaults through `component_data` - [ ] `Awake` runs before `component_add` returns; a field it writes is visible from Odin immediately after the call - [ ] A field written from Odin is read back by the Script with the new value - [ ] A define with a string, table or nil default fails and logs an error naming the offending field; the Script load returns false - [ ] A second Script defining an already-defined Component name fails its load with a log line; the first Script's Schema and any instances are untouched - [ ] Reading or writing a field on a Component that has been destroyed raises a Luau error naming the operation - [ ] A Luau error inside `Awake` is logged with a traceback and `component_add` still returns ## Blocked by - #76 - #79
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
crowmaki/cinnabar#80
No description provided.