Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Components

Eric McDaniel edited this page Sep 10, 2021 · 14 revisions

A component is data associated with an entity, differentiated by its schema, a uniquely identifiable template. Or, more specifically, that schema's integer id.

Schema

A schema is defined using Harmony's createSchema function:

const Health = createSchema(world, formats.float64)

createSchema returns an integer, the id of the newly created schema.

Harmony exports a formats object that contains multiple numeric types, e.g. uint8, int16, float64, that are used to define the shape of schemas, the process of which is discussed below.

Native Components

The term "native" here is misleading; as we will see, the other kind of component that Harmony can store is powered by a native, although esoteric, language feature.

Native components store plain built-in JavaScript values. This currently includes numbers and objects, and will include arrays, maps, and sets in the near future.

We have already defined a number component in the Health component example above. Any of Harmony's numeric formats (uint, int, float) do not matter here: the component is simply stored in an array of built-in, IEEE 754 numbers.

Native object components are created by providing an object to makeSchema, where each key is a fixed property name, and its value is either a format or a nested object conforming to the same rules:

const Attributes = createSchema({
  strength: formats.uint8,
  dexterity: formats.uint8,
  // ...
  modifiers: {
    strength: formats.int8,
    // ...
  },
})

A component can be added to or removed from an entity using set and unset, respectively. Components are then modified using queries.

Binary Components

Clone this wiki locally