Skip to content

Instantly share code, notes, and snippets.

@danielytics
Created April 18, 2021 23:53
Show Gist options
  • Save danielytics/ead773d5f3161c3eee2b23b7932f6ee9 to your computer and use it in GitHub Desktop.
Save danielytics/ead773d5f3161c3eee2b23b7932f6ee9 to your computer and use it in GitHub Desktop.
EnTT copy entity from one registry to another, from discord, saving for later
entt::entity EntityLayout::Build(entt::registry& reg, entt::hashed_string entName)
{
auto ConfigIt = g_entityConfigs.find(entName);
if (ConfigIt == g_entityConfigs.end())
{
return entt::null;
}
const EntityConfig& config = (*ConfigIt).second;
if (config.protoEnt_ == entt::null)
{
return entt::null;
}
entt::entity newEnt = reg.create();
AddComponentWithFlags<ClassComponent>(newEnt, reg, Trait_Transient, entName.data());
// Copy the prototype entity into the real registry
stamp(g_protoReg, config.protoEnt_, reg, newEnt);
auto it = g_mappedCallbacks.find(&reg);
if (it != g_mappedCallbacks.end())
{
const OnEntityConstructedCallback& onConstructCb = (*it).second.onConstruct_;
if (onConstructCb)
{
onConstructCb(reg, newEnt, entName.data());
}
}
return newEnt;
}
inline void stamp(const entt::registry& from, entt::entity src, entt::registry& to, entt::entity dst)
{
from.visit(src,
[&from, src, &to, dst](const entt::type_info type_id) {
auto it = GetStampFunctions().find(type_id.hash());
if (it != GetStampFunctions().end())
{
it->second(from, src, to, dst);
}
else
{
printf("Trying to stamp but no func. stamp_functions.size(): %zd", GetStampFunctions().size());
}
}
);
}
template<typename Type>
inline void stamp(const entt::registry& from, const entt::entity src, entt::registry& to, const entt::entity dst) {
if constexpr (!std::is_empty_v<Type>)
{
to.emplace_or_replace<Type>(dst, from.get<Type>(src));
}
else
{
to.emplace_or_replace<Type>(dst);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment