Custom Components
Introduction
Custom Components might define visual appearance, physical properties, input handling, or custom game logic.Creating a Custom Component
To create a custom Component, follow these steps:
- In the File browser, click the plus button (+).
- Click "New file" → "New Component file", and give it a name (File extension optional).
- A new Component file will be generated, TypeScript by default, in your project.
- The new Component file will include the boilerplate code required to register the custom Component.
Registering a Custom Component
The following code is an example of how a newly created Custom Component will appear in the Code Editor:
Example
Avoid using ‘debug-’ as a prefix for component names. Component names starting with 'debug-' are reserved for internal debugging purposes and will not function correctly if used in your code.
// This is a Component file. You can use this file to define a custom Component for your project.
// This Component will appear as a custom Component in the editor.
import * as ecs from '@8thwall/ecs' // This is how you access the ecs library.
ecs.registerComponent({
name: 'custom-component',
schema: {
// Add data that can be configured on the Component.
},
schemaDefaults: {
// Add defaults for the schema fields.
},
data: {
// Add data that cannot be configured outside of the Component.
},
add: (world, component) => {
// Runs when the Component is added to the world.
},
tick: (world, component) => {
// Runs every frame.
},
remove: (world, component) => {
// Runs when the Component is removed from the world.
},
})
Adding Custom Components to an Entity
The following example shows how to add a custom Component to an existing entity.
Example
Make sure you export the Component before attempting to import or use it anywhere.
import * as ecs from '@8thwall/ecs'
const CustomComponent = ecs.registerComponent({
name: 'custom-component',
schema: {
foo: ecs.string
},
schemaDefaults: {
},
data: {
},
add: (world, component) => {
},
tick: (world, component) => {
},
remove: (world, component) => {
},
})
export {CustomComponent}
import CustomComponent from './custom-component' // The name of the file without the file type.
const demo = world.createEntity()
CustomComponent.set(world, demo, {
foo: 'bar'
})
Functions
Component functions allow you to perform different actions on a component and its data in relation to an entity.
Get
Returns a read-only reference.
Example
ecs.CylinderGeometry.get(world, component.eid)
Set
Ensures the component exists on the entity, then assigns the (optional) data to the component.
Example
ecs.CylinderGeometry.set(world, component.eid, {
radius: 1,
height: 1
})
Mutate
Perform an update to the component within a callback function. Return true
to indicate no changes made.
Example
ecs.CylinderGeometry.mutate(world, component.eid, (cursor) => {
cursor.radius += 1;
cursor.height *= 2;
return false;
})
Remove
Removes the component from the entity.
Example
ecs.CylinderGeometry.remove(world, component.eid)