The Entity class represents an entity in the Editor.

Hierarchy

  • Events
    • Entity

Internal

  • Inserts entity as a child at specified index.

    Parameters

    • entity: Entity

      The entity

    • index: number = undefined

      The index. If undefined the child will be added in the end.

    Returns boolean

    Whether the child was added

Other

  • Adds a component to this Entity

    Parameters

    • component: string

      The component name

    • data: {} = {}

      Default component data. Defaults values will be used for any missing fields. For details on component properties see Entity.

    Returns void

    editor.entities.root.addComponent('model', {
    type: 'box'
    });
  • Adds a script to the script component of this entity. If a script component does not exist, this method will add the script component as well.

    Parameters

    • scriptName: string

      The name of the script.

    • options: { attributes?: object; history?: boolean; index?: number } = {}
      • Optionalattributes?: object

        The values of attributes. Each key is the name of the attributes and each value is the value for that attribute. Leave undefined to let the Editor set default values depending on the attribute types.

      • Optionalhistory?: boolean

        Whether to add a history action. Defaults to true.

      • Optionalindex?: number

        The desired index in the entity's scripts order to add this script.

    Returns Promise<void>

    A promise

  • Deletes entity (and its children)

    Parameters

    • options: { history?: boolean } = {}
      • Optionalhistory?: boolean

        Whether to record a history action. Defaults to true.

    Returns Promise<void>

    A promise

    editor.entities.root.findByName('door').delete();
    
  • Executes function for this entity and its children in depth first order.

    Parameters

    • fn: (entity: Entity) => void

      A function that takes an entity as an argument

    Returns void

    // get a list of all entities in the graph in depth first order
    const entities = [];
    editor.entities.root.depthFirst(entity => entities.push(entity));
  • Duplicates entity under the same parent

    Parameters

    • options: { history?: boolean; rename?: boolean; select?: boolean } = {}
      • Optionalhistory?: boolean

        Whether to record a history action. Defaults to true.

      • Optionalrename?: boolean

        Whether to rename the duplicated entity. Defaults to false.

      • Optionalselect?: boolean

        Whether to select the new entity. Defaults to false.

    Returns Promise<any>

    The new entity

  • Returns the entity and children that satisfy the function

    Parameters

    • fn: (entity: Entity) => boolean

      A function that takes an Entity and returns whether it should be included in the result

    Returns Entity[]

    The result

    const doors = editor.entities.root.filter(entity => entity.get('name').startsWith('door'));
    
  • Finds first entity by name using depth-first search

    Parameters

    • name: string

      The name

    Returns Entity

    The entity

    const door = editor.entities.root.findByName('Door');
    
  • Gets value at path. See Entity for a list of properties.

    Parameters

    • path: string

      The path

    Returns any

    The value

    console.log(entity.get('position'));
    
  • Checks if path exists. See Entity for a list of properties.

    Parameters

    • path: string

      The path

    Returns boolean

    True if path exists

    console.log(entity.has('components.model'));
    
  • Inserts value in array at path, at specified index. See Entity for a list of properties.

    Parameters

    • path: string

      The path

    • value: any

      The value

    • Optionalindex: number

      The index (if undefined the value will be inserted in the end)

    Returns boolean

    Whether the value was inserted

    entity.insert('tags', 'a_tag');
    
  • Returns a JSON representation of entity data. The children array of the entity gets recursively converted to an array of entity data instead of containing children resource ids.

    Returns any

    • The data
    const data = entity.jsonHierarchy();
    console.log(data.children[0].name);
  • Finds all entities with specified tags

    Parameters

    • ...tags: any[]

      The tags. If multiple tags are specified then entities that contain ANY of the specified tags will be included. If an argument is an array of tags then entities that contain ALL of the tags in the array will be included.

    Returns Entity[]

    The entities

    // entities that have the following tag
    const entities = editor.entities.root.listByTag('tag');
    // entities that have any of the following tags
    const entities = editor.entities.root.listByTag('tag', 'tag2');
    // entities that have all of the following tags
    const entities = editor.entities.root.listByTag(['tag', 'tag2']);
  • Removes a component from this Entity

    Parameters

    • component: string

      The component name

    Returns void

    editor.entities.root.removeComponent('model');
    
  • Removes a script from the entity's script component.

    Parameters

    • scriptName: string

      The name of the script.

    • options: { history?: boolean } = {}
      • Optionalhistory?: boolean

        Whether to record a history action. Defaults to true.

    Returns void

  • Remove value from array at path. See Entity for a list of properties.

    Parameters

    • path: string

      The path

    • value: any

      The value

    Returns boolean

    Whether the value was removed

    entity.removeValue('tags', 'a_tag');
    
  • Reparents entity under new parent

    Parameters

    • parent: Entity

      The new parent

    • index: number = null

      The desired index. If undefined the entity will be added at the end of the parent's children.

    • options: { history?: boolean; preserveTransform?: boolean } = {}
      • Optionalhistory?: boolean

        Whether to record a history action. Defaults to true.

      • OptionalpreserveTransform?: boolean

        Whether to preserve the original transform after reparenting

    Returns void

    const redHouse = editor.entities.root.findByName('red house');
    const greenHouse = editor.entities.root.findByName('green house');
    const door = redHouse.findByName('door');
    door.reparent(greenHouse);
  • Sets value at path. See Entity for a list of properties.

    Parameters

    • path: string

      The path

    • value: any

      The value

    Returns boolean

    Whether the value was set

    entity.set('position', [1, 0, 0]);
    
  • Unsets value at path. See Entity for a list of properties.

    Parameters

    • path: string

      The path

    Returns boolean

    Whether the value was unset

    entity.unset('components.model');