Is there a way to prevent it from adding components one by one? template <typename...…
- User
Is there a way to prevent it from adding components one by one? template <typename... Components, typename... Args> void ECS::AddComponents(const EntityId entityId, Args&&... args) { AddComponentsHelper<0, Components...>(entityId, std::forward(args)...); }
template <std::size_t I, typename T, typename... Ts, typename... Args> std::enable_if_t<I != sizeof...(Ts), void> ECS::AddComponentsHelper(const EntityId entityId, Args&&... args) { AddComponent(entityId, std::get(std::forward_as_tuple(args...))); AddComponentsHelper<I + 1, Ts...>(entityId, std::forward(args)...); }
template <std::size_t I, typename T, typename... Ts, typename... Args> std::enable_if_t<I == sizeof...(Ts), void> ECS::AddComponentsHelper(const EntityId entityId, Args&&... args) { AddComponent(entityId, std::get(std::forward_as_tuple(args...))); }
- Assistant
You can use modern C++ features like fold expressions to simplify this code. Here's an example using fold expressions that eliminates the need for the
AddComponentsHelperfunctions:cpptemplate <typename... Components, typename... Args> void ECS::AddComponents(const EntityId entityId, Args&&... args) { (AddComponent<Components>(entityId, std::forward<Args>(args)), ...); }This code will call
AddComponentfor each type inComponents..., forwarding the corresponding argument fromargs.... The fold expression ensures that all these calls are made in a single line, so components are added one by one but only with a single invocation of theAddComponentsfunction. - User
Is there a way to have this ((componentMasks[entityId].AddComponent(Component::GetTypeID())), ...); finish updating componentMasks before moving on to Adding the components?
template <typename... Components, typename... Args> void ECS::AddComponents(const EntityId entityId, Args&&... args) { ((componentMasks[entityId].AddComponent(Component::GetTypeID())), ...); (AddComponent(entityId, std::forward(args)), ...); }
- Assistant
Yes, if you specifically want to update
componentMasksbefore adding the components, you can introduce a lambda function and use it in the fold expression. Make sure the lambda is invoked beforeAddComponentin the fold expression.cpptemplate <typename... Components, typename... Args> void ECS::AddComponents(const EntityId entityId, Args&&... args) { auto update_mask = [this, &entityId](auto component_type) { componentMasks[entityId].AddComponent(component_type); }; (update_mask(Component<Components>::GetTypeID()), ...); (AddComponent<Components>(entityId, std::forward<Args>(args)), ...); }In this code, the lambda function
update_maskupdates thecomponentMasksfor eachGetTypeID(), and the fold expression ensures that all these calls are made before anyAddComponentfunction is called. - User
Whats the difference between template<class... Cs, typename... Args> void Entity::AddComponents(Args&&... args) { ComponentMask compMask; ((compMask.AddComponent(Component::GetTypeID())), ...); (ecs_.AddComponents(id_, std::forward(args)), ...); } and template<class... Cs, typename... Args> void Entity::AddComponents(Args&&... args) { ComponentMask compMask; ((compMask.AddComponent(Component::GetTypeID())), ...); (ecs_.AddComponents<Cs...>(id_, std::forward(args)), ...); }
- Assistant
In both versions,
template<class... Cs, typename... Args>is defining a variadic template that takes two parameter packs -class... Csandtypename... Args. They are used to handle any number of types,Csfor handling the component types, andArgsfor forwarding the component arguments.The difference between the two snippets is in the usage of
ecs_.AddComponents.- In the first version, the
Cpack expansion is done directly while callingAddComponents:
cpp(ecs_.AddComponents<Cs>(id_, std::forward<Args>(args)), ...);Here,
Csis expanded likeComponent1,Component2, ...,ComponentN. This code doesn't compile and results in a compilation error because the angle brackets after make the compiler think there is only one type. - In the first version, the
- User
How to prevent ambiguous calling? template <typename... Components, typename... Args> void ECS::AddComponents(const EntityId entityId, Args&&... args) { auto update_mask = [this, &entityId](auto component_type) { componentMasks[entityId].AddComponent(component_type); };
code(update_mask(Component<Components>::GetTypeID()), ...); (AddComponents<Components>(entityId, std::forward<Args>(args)), ...); Archetype* newArchetype = GetArchetype(componentMasks[entityId]); Record& record = entityArchetypeMap[entityId]; UpdateRecord(entityId, newArchetype, record);}
template <typename C, typename... Args> void ECS::AddComponents(const EntityId entityId, Args&&... args) { auto newComponentID = GetTypeID(); Archetype* newArchetype = GetArchetype(componentMasks[entityId]);
code - Assistant
- User
How to have this two functions work? template <typename... Components, typename... Args> std::enable_if<sizeof...(Components) != 1> ECS::AddComponents(const EntityId entityId, Args&&... args) { auto update_mask = [this, &entityId](auto component_type) { componentMasks[entityId].AddComponent(component_type); };
code(update_mask(Component<Components>::GetTypeID()), ...); (AddComponents<Components>(entityId, std::forward<Args>(args)...), ...); Archetype* newArchetype = GetArchetype(componentMasks[entityId]); Record& record = entityArchetypeMap[entityId]; UpdateRecord(entityId, newArchetype, record);} template<class... Cs, typename... Args> void Entity::AddComponents(Args&&... args) { ecs_.AddComponents(id_, std::forward(args)); }
- Assistant
To make the ECS::AddComponents function work with std::enable_if for the case where the number of components is different than 1, you need to apply the std::enable_if as the return type, but first, we need to fix the fold expression for adding components.
Also, note that the initial implementation of AddComponents should be a bit different to decide which underlying function to call based on the number of components. If only one component is being added, it will call AddSingleComponent; for multiple components, it will call AddComponentsInternal.