I want to delete spawned objects

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.
  },
})

Hi, welcome to the forums!

tick is running before the first box is created, result in Box not being a valid entity yet you’re trying to get a position. Also you’re comparing the position object, when you probably want to compare just the y value.

Change your tick code to this:

  const {box} = component.data
  if (box) {
    if (ecs.Position.get(world, box).y <= -8) {
      world.deleteEntity(box)
    }
  }

Additionally, make sure you’re getting a handle to THREE from the window, as you’re using it to do randX.

const {THREE} = window as any

Thanks, Now the code runs, but the boxes are not removed. I guess the position conditional is not executed anymore and why it goes false: “if (box)”. I need to remove those cubes so as not to limit FPS and technical capabilities. randX works.

I don’t know if I should save the cubes in an array or if it would be easier for each one that is generated to auto-verify that it passed a certain Y or touched a certain object, I don’t know if I should create a separate code for that. I could also check in tick, but I think that would consume more, because maybe I should check all the cubes or I don’t know how to make the cube objects like a more accessible variable.

I would create a component that gets attached to each box and that component has a state machine that onTick checks the position and removes its entity if it’s too low.

There are quite a few examples of this in our Project Library, if you run into any issues let me know. :slight_smile:

Hi, I’m new to this technology, can you tell me how to access your library?

Look for Projects where the title starts with "Studio: "