Skip to content

EntityInput

An entity picker. The inspector renders a dropdown populated with all named entities in the scene.

Runtime value: { type: "entity"; value: WorldEntityView | null } — read field.value

EntityInput inspector control

Declaration

ts
import { Behaviour, type EntityInput } from "@relu-interactives/spatial-ecs";

export default class Demo extends Behaviour {
  data = {
    target: { type: "entity", value: null } as EntityInput,
  };
}

Runtime usage

At runtime, .value is resolved to a WorldEntityView — not a raw ID. Always guard for null (no entity selected):

ts
import { type WorldEntityView } from "@relu-interactives/spatial-ecs";

onUpdate() {
  const target = this.data.target.value as WorldEntityView | null;
  if (!target) return; // no entity assigned in the inspector
}

WorldEntityView shape

ts
type WorldEntityView = {
  name: string;                   // entity name from NameComponent
  entityId: number;               // runtime entity ID
  type: string;                   // entity type ("mesh", "camera", "light", …)
  object: THREE.Object3D | null;  // live three.js scene object
  components: Component[];        // all components attached to this entity
};

Reading entity data

ts
onUpdate() {
  const target = this.data.target.value as WorldEntityView | null;
  if (!target) return;

  // Name and ID
  console.log(target.name);     // e.g. "Camera"
  console.log(target.entityId); // runtime numeric ID

  // Access the three.js object directly
  const obj = target.object;
  if (obj) {
    console.log(obj.position); // THREE.Vector3
  }
}

Moving another entity from a script

Use the entity's components array to find and mutate a specific component:

ts
import {
  Behaviour,
  TransformComponent,
  type EntityInput,
  type WorldEntityView,
  type FloatInput,
} from "@relu-interactives/spatial-ecs";

export default class Mover extends Behaviour {
  data = {
    target: { type: "entity", value: null } as EntityInput,
    speed:  5 as FloatInput,
  };

  onUpdate() {
    const target = this.data.target.value as WorldEntityView | null;
    if (!target) return;

    const transform = target.components.find(
      (c) => c instanceof TransformComponent,
    ) as TransformComponent | undefined;

    if (transform) {
      transform.position.x += this.data.speed * this.deltaTime;
    }
  }
}

TIP

Always guard with if (!target) return; — the field is null until the user picks an entity in the inspector, and may be null in freshly opened scenes.