Entity

namespace marengine

Typedefs

typedef std::vector<Entity> FEntityArray
class Entity
#include “Core/ecs/Entity/Entity.h”

The entity is a general purpose object. It only consists of a unique ID (uint32_t, which can found at entt::entity) and m_sceneRegistry. Every entity is a container, where components can be attached. Entities are the base of all objects, that can be found in the scene.

Public Functions

Entity() = delete

Entity default constructor is disabled, because we don’t want to store nullptr at m_registry!

Entity(entt::registry *pSceneRegistry)

This is default constructor for Entity class, because we need to initialize m_sceneRegistry member! If m_sceneRegistry member will stay as nullptr value, Entity instance will immediately crash. During this constructor call entity is created.

Warning

Constructor does not check, if pSceneRegistry is valid. It creates Entity in place, ensure that given pointer is correct.

Parameters
  • pSceneRegistry: valid entt::registry pointer to which entity will belong to.

Entity(entt::entity enttEntity, entt::registry *pSceneRegistry)

Constructor for using already created entity and its sceneRegistry instance. Used mostly in entt::registry::view lambda.

Warning

Constructor does not check, if pSceneRegistry is valid. It creates Entity in place, ensure that given pointer is correct.

Parameters
  • entt_entity: valid entt::entity, which can be reassigned to new entity object

  • sceneRegistry: valid entt::registry, to which entt_entity belongs to.

Entity(const Entity &other) = default

Default copy constructor.

void destroyYourself() const

Method that has ability to destroy current entity. Remember to delete destroyed entity array that it is stored in, it can cause a lot of damage.

bool isValid() const

Method checks, if entity is valid one and returns result.

Return

returns true if entity is valid (it exists and is fine).

const Entity &assignChild(const Entity &child) const

Assigns child to current entity. Places child to array at CChildren.

Warning

Make sure that child is a valid entity!

Parameters
  • child: valid entity, which will be assigned as child

void removeChild(size_t index) const

Removes child from CChildren by its index in array.

Warning

child is not destroyed, only removed from array!

Parameters
  • index: index at which child will be removed

void removeChild(const Entity &child) const

Removes child from CChildren, only if child is assigned to entity.

Warning

child is not destroyed, only removed from array!

Parameters
  • child: child, which we want to be removed from array

bool hasChildren() const

Method checks, if current entity contains children and returns result.

Return

Returns true, if current entity contains any children.

const FEntityArray &getChildren() const

Returns children assigned to an entity in array.

Return

Returns all children of current entity.

const Entity &getChild(size_t index) const

Returns child by its index in array.

Warning

Method does not check if it is valid entity! If index is too large, it may cause overflow.

Return

child instance at given index

Parameters
  • index: index of child

template<typename TComponent>
bool hasComponent() const

Checks, if current entity has TComponent assigned and returns result.

Return

returns true, if entity contains component

Template Parameters
  • TComponent: structure type of component

template<typename TComponent, typename ...Args>
TComponent &addComponent(Args&&... args) const

Method emplaces TComponent object at current entity instance and returns reference to newly created object. If needed you can pass arguments, but then please make sure that TComponent contains constructor that takes those arguments.

Return

Returns newly created component.

Template Parameters
  • TComponent: structure type of component

  • Args: variadic template, which gives ability to call specific constructor during creation

Parameters
  • args: variadic parameter, pass all args that TComponent needs during creation

template<typename TComponent>
TComponent &getComponent() const

Method returns TComponent, that current entity should contain.

Warning

If current entity does not contain TComponent debug_break is called.

Return

TComponent’s instance assigned to current entity

Template Parameters
  • TComponent: structure type of component

template<typename TComponent>
TComponent &replaceComponent(const Entity &other) const

Method replaces current entity’s component with other entity component’s values. Make sure that, before this method current and other entities have TComponent.

Return

Returns newly created object at current entity (copied from given entity)

Template Parameters
  • TComponent: structure type of component

Parameters
  • other: entity, from which we want component to be copied to current entity

template<typename TComponent>
TComponent &replaceComponent(const TComponent &other) const

Method replaces current entity’s component with passed TComponent argument. Make sure that, before this method current entity has TComponent.

Return

Returns newly created object at current entity (copied given component)

Template Parameters
  • TComponent: structure type of component

Parameters
  • other: component, which will be copied to current entity

template<typename TComponent, typename ...Args>
TComponent &get_addComponent(Args&&... args) const

Method should be used, if you don’t know whether entity has TComponent or do not. It adds it or just returns it. Remember that, you should know, when entity can contain TComponent.

Return

Returns newly created component or already existing

Template Parameters
  • TComponent: structure type of component

  • Args: variadic template, which gives ability to call specific constructor during creation

Parameters
  • args: variadic parameter, pass all args that TComponent needs during creation

template<typename TComponent>
void removeComponent() const

Method removes TComponent from current entity.

Warning

Method does not check if entity contains component, it just removes it!

Template Parameters
  • TComponent: structure type of component

Public Static Functions

void fillEntityWithBasicComponents(const Entity &entity)

Static method for filling entity with basic components. By default it needs:

  • CTag (we want to have human readable tag for every entity)

  • CTransform (we want every entity to have its own position, rotation, scale)

  • LightBatchInfoComponent (engine-only component, it remembers light batches that other components are stored in, optimization)

  • MeshBatchInfoComponent (engine-only component, it remembers mesh batches that other components are stored in, optimization)

  • CChildren (used to store some children, that will be relative to base entity - parent)

    Parameters
    • entity: entity, which will be filled with basic components

Private Members

entt::registry *m_pSceneRegistry = {nullptr}
entt::entity m_entityHandle = {entt::null}

Friends

friend class Scene
struct CChildren

Public Members

FEntityArray children