mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-09-05 14:58:27 +00:00
feat: Add isolated and simplified path to add components (#1204)
* Components: Make ComponentType inline Prevents the next commits ODR violation * Components: Add new components * Entity: Add headers inline script component ComponentType * Components: Flip constructor argument order Entity comes first always * Entity: Add generic AddComponent Allows for much easier adding of components and is error proof by not allowing the user to add more than 1 of a specific component type to an Entity. * Entity: Migrate all component constructors Move all to the new variadic templates AddComponent function to reduce clutter and ways the component map is modified. The new function makes no assumptions. Component is assumed to not exist and is checked for with operator[]. This will construct a null component which will then be newed if the component didnt exist, or it will just get the current component if it does already exist. No new component will be allocated or constructed if the component already exists and the already existing pointer is returned instead. * Entity: Add placement new For the case where the component may already exist, use a placement new to construct the component again, it would be constructed again, but would not need to go through the allocator. * Entity: Add comments on likely new code * Tests: Fix tests * Update Entity.cpp * Update SGCannon.cpp * Entity: call destructor when re-constructing * Update Entity.cpp Update Entity.cpp --------- Co-authored-by: Aaron Kimbrell <aronwk.aaron@gmail.com>
This commit is contained in:
@@ -66,7 +66,7 @@ public:
|
||||
|
||||
eGameMasterLevel GetGMLevel() const { return m_GMLevel; }
|
||||
|
||||
uint8_t GetCollectibleID() const { return uint8_t(m_CollectibleID); }
|
||||
uint8_t GetCollectibleID() const;
|
||||
|
||||
Entity* GetParentEntity() const { return m_ParentEntity; }
|
||||
|
||||
@@ -274,6 +274,9 @@ public:
|
||||
template<typename T>
|
||||
T GetVarAs(const std::u16string& name) const;
|
||||
|
||||
template<typename ComponentType, typename... VaArgs>
|
||||
ComponentType* AddComponent(VaArgs... args);
|
||||
|
||||
/**
|
||||
* Get the LDF data.
|
||||
*/
|
||||
@@ -501,3 +504,36 @@ T Entity::GetNetworkVar(const std::u16string& name) {
|
||||
|
||||
return LDFData<T>::Default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Adds a component of type ComponentType to this entity and forwards the arguments to the constructor.
|
||||
*
|
||||
* @tparam ComponentType The component class type to add. Must derive from Component.
|
||||
* @tparam VaArgs The argument types to forward to the constructor.
|
||||
* @param args The arguments to forward to the constructor. The first argument passed to the ComponentType constructor will be this entity.
|
||||
* @return ComponentType* The added component. Will never return null.
|
||||
*/
|
||||
template<typename ComponentType, typename... VaArgs>
|
||||
inline ComponentType* Entity::AddComponent(VaArgs... args) {
|
||||
static_assert(std::is_base_of_v<Component, ComponentType>, "ComponentType must be a Component");
|
||||
|
||||
// Get the component if it already exists, or default construct a nullptr
|
||||
auto*& componentToReturn = m_Components[ComponentType::ComponentType];
|
||||
|
||||
// If it doesn't exist, create it and forward the arguments to the constructor
|
||||
if (!componentToReturn) {
|
||||
componentToReturn = new ComponentType(this, std::forward<VaArgs>(args)...);
|
||||
} else {
|
||||
// In this case the block is already allocated and ready for use
|
||||
// so we use a placement new to construct the component again as was requested by the caller.
|
||||
// Placement new means we already have memory allocated for the object, so this just calls its constructor again.
|
||||
// This is useful for when we want to create a new object in the same memory location as an old one.
|
||||
componentToReturn->~Component();
|
||||
new(componentToReturn) ComponentType(this, std::forward<VaArgs>(args)...);
|
||||
}
|
||||
|
||||
// Finally return the created or already existing component.
|
||||
// Because of the assert above, this should always be a ComponentType* but I need a way to guarantee the map cannot be modifed outside this function
|
||||
// To allow a static cast here instead of a dynamic one.
|
||||
return dynamic_cast<ComponentType*>(componentToReturn);
|
||||
}
|
||||
|
Reference in New Issue
Block a user