Cogs.Core
FieldSetter.cpp
1#include "FieldSetter.h"
2
3#include "Foundation/Logging/Logger.h"
4
5namespace
6{
7 const Cogs::Logging::Log logger = Cogs::Logging::getLogger("FieldSetter");
8}
9
10const Cogs::Reflection::Field * getComponentField(
11 BridgeContext* ctx,
12 EntityId entityId,
13 ComponentId componentId,
14 FieldId fieldId,
16{
17 auto context = static_cast<Cogs::Core::Context*>(ctx);
18 auto entity = context->store->getEntityPtr(entityId);
19
20 if (!entity) {
21 LOG_ERROR(logger, "Could not find entity with id %zd.", size_t(entityId));
22 return nullptr;
23 }
24
25 if (fieldId == Cogs::Reflection::NoField) {
26 LOG_ERROR(logger, "Invalid field id %d.", fieldId);
27 return nullptr;
28 }
29
30 auto & componentType = Cogs::Reflection::TypeDatabase::getType(componentId);
31
32 auto field = componentType.getField(fieldId);
33
34 component = entity->getComponent<Cogs::ComponentModel::Component>(componentType);
35
36 if (!component) {
37 LOG_ERROR(logger, "Component with type id %d (%s) in entity %zd not found.", componentId, componentType.getName().c_str(), size_t(entityId));
38 return nullptr;
39 }
40
41 return field;
42}
Base class for Component instances.
Definition: Component.h:143
ComponentType * getComponent() const
Definition: Component.h:159
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
class EntityStore * store
Entity store.
Definition: Context.h:231
ComponentModel::Entity * getEntityPtr(const EntityId entityId)
Get a raw pointer to the entity with the given id.
Log implementation class.
Definition: LogManager.h:139
Field definition describing a single data member of a data structure.
Definition: Field.h:68
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
constexpr FieldId NoField
No field id.
Definition: Name.h:60