#pragma once #include <cstdint> #include <unorderedmap> #include <unorderedset> #include…
- User
#pragma once
#include #include <unordered_map> #include <unordered_set> #include #include #include #include
#include "Archetype.h" #include "EventBus.h" #include "System.h" #include "ComponentMask.h"
using ArchetypeId = std::vector; using EntityId = uint32_t;
class SystemBase; class ECS { private:
codeusing SystemsArrayMap = std::unordered_map<std::uint8_t, std::vector<SystemBase*>>; using ComponentTypeIdBaseMap = std::unordered_map<size_t, ComponentBase*>; struct Record { Archetype* archetype; size_t index; }; using EntityArchetypeMap = std::unordered_map<size_t, Record>; using ArchetypesArray = std::vector<Archetype*>; using ComponentMasks = std::unordered_map<size_t, ComponentMask>;public: explicit ECS();
code~ECS(); void Update(const std::uint8_t layer, const std::uint32_t delta); void RegisterEntity(const EntityId entityId); void DestroyEntity(const EntityId entityId); template <class C> void RegisterComponent(); template <class C> const bool IsComponentRegistered() const; template<class C, typename... Args> C* AddComponent(const EntityId entityId, Args&&... args); template<class C> void RemoveComponent(const EntityId entityId); template<class C> C* GetComponent(const EntityId entityId); const bool HasComponent(const EntityId entityId, size_t compTypeId) const; template<class C> size_t GetComponentTypeId(); template<class C> const bool HasComponent(const EntityId entityId); void RegisterSystem(const std::uint8_t& layer, SystemBase* system); void UnregisterSystem(const std::uint8_t& layer, SystemBase* system); template<class... Cs> std::vector<size_t> GetEntitiesWithComponents(); Archetype* GetArchetype(const ComponentMask& componentMask); EntityId GetNewId(); std::shared_ptr<EventBus> GetEventBus() { return eventBus_; } - Assistant
Yes, you can create a helper class that contains all the functionality related to reallocate/transfer/component management and use it inside the ECS class. Here's an example of how you can do it:
- Create a
ComponentManagerclass that will contain all the methods related to component management:
- Create a
- User
There's an Use of a moved from object "(<args_0>)" (lifetime. 1) warning at the line newComponent = new (&newArchetype->componentData[toIndex][currentSize]) C(std::forward(args)...); when I call these two functions template<class C, typename... Args> C ECS::AddComponent(const EntityId entityId, Args&&... args) { // 1. Get the component type ID and check if the component already exists for the entity auto newComponentID = GetTypeID();
- Assistant
The issue you're encountering may be due to the usage of
std::forwardin a non-perfect forwarding context. TheTransferComponentsAndAddmethod uses an argument that has already been forwarded in the method. To fix the issue, you can pass-by-reference the arguments to the method instead of forwarding them. Here is the updated version of your code: - User
- Assistant