Mutable Data
Learn how to handle ead-only and mutable data from components
// fetch a read-only (immutable) version
const immutableTransform = Transform.get(myEntity)
// the following does NOT work:
// immutableTransform.position.y = 2
const mutableTransform = Transform.getMutable(myEntity)
// the following line DOES change the entity's position
mutableTransform.position.y = 2// hard-coded maximum height
const MAX_HEIGHT = 10
// Define the system
function HeightLimitSystem(dt: number) {
// iterate over all entities that have a Transform component
for (const [entity] of engine.getEntitiesWith(Transform)) {
// get read-only values
const currentHeight = Transform.get(entity).position.y
// compare values
if (currentHeight > MAX_HEIGHT) {
// fetch mutable version to make a change
const mutableTransform = Transform.getMutable(entity)
// change transform
mutableTransform.position.y = MAX_HEIGHT
}
}
}
// Add system to engine
engine.addSystem(HeightLimitSystem)Last updated