1#include "DeferredNameResolution.h"
3#include "EntityStore.h"
8#include "Foundation/Logging/Logger.h"
9#include "Foundation/Reflection/TypeDatabase.h"
18DeferredNameResolution::DeferredNameResolution(
Context* context)
21 context->
services->addFrameCallback([&] { update(); });
24void DeferredNameResolution::update()
29 for (
auto it = scheduled.begin(); it != scheduled.end();) {
30 auto comp = it->component.resolve();
32 LOG_DEBUG(logger,
"Component of entity reference '%s' is dead.", it->name.c_str());
33 it = scheduled.erase(it);
34 }
else if (comp->getGeneration() != it->component.generation) {
35 LOG_DEBUG(logger,
"Component handle from entity reference '%s' no longer valid.", it->name.c_str());
36 it = scheduled.erase(it);
41 LOG_DEBUG(logger,
"Found entity '%s', updating reference.", it->name.c_str());
44 if (it->field->getTypeId() == Reflection::TypeDatabase::getType<EntityPtr>().getTypeId()) {
45 *(it->field->getPtr<
EntityPtr>(comp)) = std::move(entityPtr);
46 }
else if (it->field->getTypeId() == Reflection::TypeDatabase::getType<WeakEntityPtr>().getTypeId()) {
47 *(it->field->getPtr<
WeakEntityPtr>(comp)) = std::move(entityPtr);
49 LOG_ERROR(logger,
"Entity reference to '%s' has unhandled type %d.", it->name.c_str(), (
int)it->field->getTypeId());
54 auto * ptr = it->field->getPtr<std::vector<EntityPtr>>(comp);
56 if (
static_cast<int64_t
>(ptr->size()) <= it->index) ptr->resize(it->index + 1);
58 (*ptr)[it->index] = std::move(entityPtr);
60 auto * ptr = it->field->getPtr<std::vector<WeakEntityPtr>>(comp);
62 if (
static_cast<int64_t
>(ptr->size()) <= it->index) ptr->resize(it->index + 1);
64 (*ptr)[it->index] = std::move(entityPtr);
66 LOG_ERROR(logger,
"Entity reference to '%s' has unhandled type %d.", it->name.c_str(), (
int)it->field->getTypeId());
70 it = scheduled.erase(it);
A Context instance contains all the services, systems and runtime components needed to use Cogs.
std::unique_ptr< class Services > services
Services.
class EntityStore * store
Entity store.
EntityPtr findEntity(const StringView &name, const ComponentModel::Entity *root=nullptr, EntityFind findOptions=EntityFind::Default) const
Finds an entity with the given name.
Log implementation class.
static const Type & getType()
Get the Type of the given template argument.
constexpr TypeId getTypeId() const
Get the unique Reflection::TypeId of this instance.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
std::weak_ptr< ComponentModel::Entity > WeakEntityPtr
Weak Smart pointer for Entity access.
constexpr Log getLogger(const char(&name)[LEN]) noexcept