new Entity()
An entity is a game object. All the Objects in the game are entities. Entities can have components attached to them. Entities can have children entities, which can be used to create a hierarchy of entities.
- Source
// Create a new entity
let entity = new Entity("Box");
Members
children :Array.<Entity>
Children of this entity.
- Array.
<Entity>
- Source
(protected, readonly) components :Array.<Component>
Component attached to this entity.
- Array.
<Component>
- Source
(protected) name :string
Name of the entity. Names are not processed by the engine. They are used for debugging purposes.
- string
- Source
parent :Entity
Parent Entity of this entity. If this entity is the root entity, this will be null.
- Source
Methods
AddChild(child)
Adds a child entity to this entity. This checks if the child is already added to the entity. If it is, it will not be added again.
Name | Type | Description |
---|---|---|
child | Entity | The child entity to be added. |
- Source
// Create a new entity
let entity = new Entity("Box");
// Create a new child entity
let child = new Entity("Child");
// Add the child entity to the parent entity
entity.AddChild(child);
AddChildren(…children)
Adds multiple child entities to this entity.
Name | Type | Attributes | Description |
---|---|---|---|
children | Array. | <repeatable> | The child entities to be added. |
- Source
// Create a new entity
let entity = new Entity("Box");
let childEntity1 = new Entity("Child1");
let childEntity2 = new Entity("Child2");
let childEntity3 = new Entity("Child3");
// Add the child entities to the parent entity
entity.AddChildren(childEntity1, childEntity2, childEntity3);
AddComponent(component)
Adds a component to the entity. This checks if the component is already attached to the entity. If it is, it will not be added again.
Name | Type | Description |
---|---|---|
component | Component | The component to be added. |
- Source
// Add a transform component to the entity
entity.AddComponent(new Transform());
GetChildren() → {Array.<Entity>}
Get all children of this entity.
- Source
The children of this entity.
- Type:
- Array.
<Entity>
GetChildWithComponent(type) → {Entity}
Gets the child entity with the specified component type.
Name | Type | Description |
---|---|---|
type | Component | The type of the component to be returned. |
- Source
The child entity with the specified component type.
- Type:
- Entity
// Get the first child entity with a transform component
let child = entity.GetChildWithComponent(Transform);
GetComponent(type) → {Component}
Returns the component of the given type attached to this entity.
Name | Type | Description |
---|---|---|
type | Component | The type of the component to be returned. |
- Source
The component of the given type attached to this entity.
- Type:
- Component
// Get the transform component of the entity
let transform = entity.GetComponent(Transform);
GetComponentsInChildren(type) → {Array.<Component>}
Get a component of the given type attached to the children. Returns all the components of the given type attached to all the children.
Name | Type | Description |
---|---|---|
type | Component | The type of the component to be returned. |
- Source
The components of the given type attached to the children.
- Type:
- Array.
<Component>
// Get the transform components of all the children of the entity
let transforms = entity.GetComponentsInChildren(Transform);
GetParent() → {Entity}
Returns the parent of this entity.
- Source
The parent of this entity.
- Type:
- Entity
GetTree() → {Object}
Generate a tree with the children of this entity. Components are not included in the tree.
- Source
The tree with the children of this entity.
- Type:
- Object
// Generate a tree with the children of the entity
let tree = entity.GetTree();
HasComponent(type) → {boolean}
Returns true if the entity has a component of the given type.
Name | Type | Description |
---|---|---|
type | Component | The type of the component to be checked. |
- Source
True if the entity has a component of the given type.
- Type:
- boolean
// Check if the entity has a transform component
let hasTransform = entity.HasComponent(Transform);
RemoveComponent(type)
Removes the component of the given type from the entity.
Name | Type | Description |
---|---|---|
type | Component | The type of the component to be removed. |
- Source
// Remove the transform component from the entity
entity.RemoveComponent(Transform);