Unhandled promise rejection: Error: Cannot get component data if not attached.
at Object.get runtime.js
at Object.get runtime.js
at tick spawn.js:66:21
at (anonymous) runtime.js
at (anonymous) runtime.js
at (anonymous) runtime.js
at p runtime.js
at u runtime.js
at Object.loadScene runtime.js
ACTUAL CODE:
import * as ecs from '@8thwall/ecs' // This is how you access the ecs library.
const {mat4, quat, vec3} = ecs.math
ecs.registerComponent({
name: 'spawn',
schema: {
// Add data that can be configured on the component.
objetoASpamear: ecs.eid,
},
schemaDefaults: {
// Add defaults for the schema fields.
},
data: {
interval: ecs.i32,
box: ecs.eid,
// Add data that cannot be configured outside of the component.
},
add: (world, component) => {
// Runs when the component is added to the world.
let {box} = component.data
const interval = world.time.setInterval(() => {
const randX = THREE.MathUtils.randInt(-2, 2)
box = world.createEntity()
ecs.BoxGeometry.set(world, box, {
width: 1,
height: 1,
depth: 1,
})
const cameraPos = ecs.Position.get(world, world.camera.getActiveEid())
ecs.Position.set(world, box, {
x: randX + cameraPos.x,
y: 6,
z: 0,
})
ecs.Material.set(world, box, {
r: 255,
g: 255,
b: 255,
})
ecs.Collider.set(world, box, {
shape: ecs.ColliderShape.Box,
mass: 1,
eventOnly: false,
lockXAxis: false,
lockYAxis: false,
lockZAxis: false,
friction: 0.5,
restitution: 0.5,
linearDamping: 0,
angularDamping: 0,
rollingFriction: 0.1,
spinningFriction: 0.1,
})
}, 1000)
component.data.interval = interval
},
tick: (world, component) => {
// Runs every frame.
const {box} = component.data
if (ecs.Position.get(world, box) <= -8) {
world.deleteEntity(box)
}
},
remove: (world, component) => {
// Runs when the component is removed from the world.
},
})