libecs-cpp 2.1.0
Entity Component System library in C++
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
ecs::Entity Class Reference

One thing in a container, to which components are attached. More...

#include <Entity.hpp>

Collaboration diagram for ecs::Entity:
Collaboration graph
[legend]

Public Member Functions

 Entity (ecs::Container *container)
 Creates an entity with a generated handle.
 
 Entity (ecs::Container *container, const std::string &handle)
 Creates an entity with the given handle.
 
nlohmann::json Export () const
 Describes the entity and its components as JSON.
 
std::shared_ptr< ecs::Component > Component (std::unique_ptr< ecs::Component > component)
 Attaches a component to this entity and returns the stored component.
 
template<class T = ecs::Component>
std::shared_ptr< T > ComponentGet (const std::string &type) const
 Looks up this entity's component with the given type name, as class T.
 
bool ComponentHas (const std::string &type) const
 Says whether this entity has a component stored under the given type name.
 
void Destroy ()
 Removes this entity and its components from the container.
 
void ComponentDestroy (const std::string &type)
 Removes this entity's component with the given type name.
 

Public Attributes

ecs::Container * Container
 The container the entity belongs to.
 
const std::string Handle
 The entity's handle, given at construction and never changed.
 

Detailed Description

One thing in a container, to which components are attached.

An entity is a handle and nothing else; its data is in its components. Create one with Container::Entity().

Thread: container thread only. An entity changes its container's tables, so every member must be used from the thread that drives the container (see Container).

Constructor & Destructor Documentation

◆ Entity() [1/2]

ecs::Entity::Entity ( ecs::Container *  container)

Creates an entity with a generated handle.

Throws std::runtime_error if the container is null. Applications normally call Container::Entity() instead, which creates the entity and owns it.

◆ Entity() [2/2]

ecs::Entity::Entity ( ecs::Container *  container,
const std::string &  handle 
)

Creates an entity with the given handle.

Throws std::runtime_error if the container is null. Applications normally call Container::Entity(handle) instead.

Member Function Documentation

◆ Component()

std::shared_ptr< ecs::Component > ecs::Entity::Component ( std::unique_ptr< ecs::Component >  component)

Attaches a component to this entity and returns the stored component.

The call sets the component's EntityHandle and hands it to Container::Component(), which describes ownership, replacement of a component of the same Type, and the rejected inputs. In short: pass std::make_unique<T>(...) or a std::move()d std::unique_ptr; the caller's pointer is empty afterwards; a second component of the same Type replaces the first; and a null pointer, an empty Type or an entity that is no longer in its container throws std::runtime_error.

◆ ComponentDestroy()

void ecs::Entity::ComponentDestroy ( const std::string &  type)

Removes this entity's component with the given type name.

An unknown type name is a silent no-op.

◆ ComponentGet()

template<class T = ecs::Component>
std::shared_ptr< T > ecs::Entity::ComponentGet ( const std::string &  type) const
inline

Looks up this entity's component with the given type name, as class T.

The result is empty when the entity has no component with that type name or the stored one is not a T. With the default T, ecs::Component, any stored component is returned. The call never throws and never changes the container. See Container::ComponentGet() for the details.

◆ ComponentHas()

bool ecs::Entity::ComponentHas ( const std::string &  type) const

Says whether this entity has a component stored under the given type name.

Only the name is compared, so the component's class does not matter. The call never throws and never changes the container.

◆ Destroy()

void ecs::Entity::Destroy ( )

Removes this entity and its components from the container.

The entity object is destroyed by the call, so the pointer must not be used afterwards.

◆ Export()

nlohmann::json ecs::Entity::Export ( ) const

Describes the entity and its components as JSON.

Member Data Documentation

◆ Container

ecs::Container* ecs::Entity::Container

The container the entity belongs to.

◆ Handle

const std::string ecs::Entity::Handle

The entity's handle, given at construction and never changed.


The documentation for this class was generated from the following file: