Components

If entities are the bread of ECS then components are the butter. Components provide state to entities that persists between game steps.

You can read about how components are added to entities in the Entities chapter.

Tag Components

Because systems resolve entities based on their composition, the simple addition of a component to an entity has meaning in Javelin. It stands to reason then that systems could execute per-entity logic solely based on the presence of a component.

Tags are the simplest kind of component. They are stateless, and consequentially are performantly added and removed from entities.

Tags are created with the tag function:

let PurpleTeam = j.tag()
let YellowTeam = j.tag()

Tags also happen to take up minimal space in network messages because they have no corresponding value to serialize.

Value Components

Value components define entity state that should be represented with a value, like a string, array, or object. They are created with the value function:

let Mass = j.value()

value accepts a generic type parameter that defines the value the component represents. Value components that aren’t provided a value type are represented as unknown.

let Mass = j.value<number>()

Schema

Value components may optionally be defined with a schema. Schemas are component blueprints that make a component’s values eligible for auto-initialization, serialization, pooling, and validation.

A value component can be defined with a schema by providing the schema as the first parameter to value:

let Mass = j.value("f32")

Schemas can take the form of scalars or records.

let Quaternion = j.value({
  x: "f32",
  y: "f32",
  z: "f32",
  w: "f32",
})

Deeply-nested schema are planned, but not supported at the current point in Javelin’s development.

Below is a table of all schema-supported formats.

idformatsupported values
number(alias of f64)
u88-bit unsigned integer0 to 255
u1616-bit unsigned integer0 to 65,535
u3232-bit unsigned integer0 to 4,294,967,295
i88-bit signed integer-128 to 127
i1616-bit signed integer-32,768 to 32,767
i3232-bit signed integer-2,147,483,648 to 2,147,483,647
f3232-bit float
f6464-bit float