Skip to content

Commit 5369f36

Browse files
committed
📝 updat trait docs
1 parent db1043d commit 5369f36

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,30 +579,35 @@ const { entityId, generation, worldId } = unpackEntity(entity)
579579
580580
### Trait
581581
582-
A trait is a specific block of data. They are added to entities to build up its overall data signature. If you are familiar with ECS, it is our version of a component. It is called a trait instead to not get confused with React or web components.
582+
Traits are self-contained slices of data you attach to an entity to define its state. They serve the same purpose as components in a traditional ECS. We call them traits to avoid confusion with React or web components.
583583
584584
A trait can be created with a schema that describes the kind of data it will hold.
585585
586586
```js
587587
const Position = trait({ x: 0, y: 0, z: 0 })
588588
```
589589
590-
In cases where the data needs to be initialized for each instance of the trait created, a callback can be passed in to be used a as a lazy initializer.
590+
A schema supports primitive values with **no** nested objects or arrays. In cases where the data needs to initialized for each instance of the trait, or complex structures are required, a callback initializer can be used.
591591
592592
```js
593-
//The items array will be shared between every instance of this trait
593+
//Arrays and objects are not allowed in trait schemas
594594
const Inventory = trait({
595595
items: [],
596+
vec3: { x: 0, y: 0, z: 0}
596597
max: 10,
597598
})
598599

599-
//With a lazy initializer, each instance will now get its own array
600+
//Use a callback initializer for arrays and objects
600601
const Inventory = trait({
601602
items: () => [],
603+
vec3: () => ({ x: 0, y: 0, z: 0})
602604
max: 10,
603605
})
604606
```
605607
608+
> ℹ️ **Why not support nested schemas?**<br>
609+
> It looks obvious to support nested stores, but doing so makes algorithms that work with the data exponentially more complex. If all data can be assumed scalar then any operation is guaranteed to be the simplest and fastest algorithm possible. This is called the First Normal Form in relational database theory. [You can read more here](https://www.dataorienteddesign.com/dodbook/node3.html#SECTION00340000000000000000).
610+
606611
Sometimes a trait only has one field that points to an object instance. In cases like this, it is useful to skip the schema and use a callback directly in the trait.
607612
608613
```js

0 commit comments

Comments
 (0)