diff --git a/Gems/Multiplayer/Code/Include/Multiplayer/NetworkEntity/INetworkEntityManager.h b/Gems/Multiplayer/Code/Include/Multiplayer/NetworkEntity/INetworkEntityManager.h index 1c8894d6c2..991aef4d38 100644 --- a/Gems/Multiplayer/Code/Include/Multiplayer/NetworkEntity/INetworkEntityManager.h +++ b/Gems/Multiplayer/Code/Include/Multiplayer/NetworkEntity/INetworkEntityManager.h @@ -89,6 +89,11 @@ namespace Multiplayer //! @return the total number of entities tracked by this INetworkEntityManager instance virtual uint32_t GetEntityCount() const = 0; + //! Returns the Net Entity ID for a given AZ Entity ID. + //! @param entityId the AZ Entity ID + //! @return the Net Entity ID + virtual NetEntityId GetNetEntityIdById(const AZ::EntityId& entityId) const = 0; + //! Adds the provided entity to the internal entity map identified by the provided netEntityId. //! @param netEntityId the identifier to use for the added entity //! @param entity the entity to add to the internal entity map diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp index cff05b9151..05373bbeee 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.cpp @@ -73,6 +73,11 @@ namespace Multiplayer return m_networkEntityTracker.Get(netEntityId); } + NetEntityId NetworkEntityManager::GetNetEntityIdById(const AZ::EntityId& entityId) const + { + return m_networkEntityTracker.Get(entityId); + } + uint32_t NetworkEntityManager::GetEntityCount() const { return static_cast(m_networkEntityTracker.size()); diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h index fdd0201b7a..97db532916 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityManager.h @@ -41,6 +41,7 @@ namespace Multiplayer MultiplayerComponentRegistry* GetMultiplayerComponentRegistry() override; HostId GetHostId() const override; ConstNetworkEntityHandle GetEntity(NetEntityId netEntityId) const override; + NetEntityId GetNetEntityIdById(const AZ::EntityId& entityId) const override; EntityList CreateEntitiesImmediate(const AzFramework::Spawnable& spawnable, NetEntityRole netEntityRole); EntityList CreateEntitiesImmediate diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.cpp b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.cpp index a70dd74bf9..e31907461b 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.cpp @@ -18,6 +18,7 @@ namespace Multiplayer ++m_addChangeDirty; AZ_Assert(m_entityMap.end() == m_entityMap.find(netEntityId), "Attempting to add the same entity to the entity map multiple times"); m_entityMap[netEntityId] = entity; + m_netEntityIdMap[entity->GetId()] = netEntityId; } NetworkEntityHandle NetworkEntityTracker::Get(NetEntityId netEntityId) @@ -32,6 +33,16 @@ namespace Multiplayer return ConstNetworkEntityHandle(entity, netEntityId, this); } + NetEntityId NetworkEntityTracker::Get(const AZ::EntityId& entityId) const + { + auto found = m_netEntityIdMap.find(entityId); + if (found != m_netEntityIdMap.end()) + { + return found->second; + } + return Multiplayer::InvalidNetEntityId; + } + bool NetworkEntityTracker::Exists(NetEntityId netEntityId) const { return (m_entityMap.find(netEntityId) != m_entityMap.end()); @@ -50,12 +61,22 @@ namespace Multiplayer void NetworkEntityTracker::erase(NetEntityId netEntityId) { ++m_deleteChangeDirty; - m_entityMap.erase(netEntityId); + + auto found = m_entityMap.find(netEntityId); + if (found != m_entityMap.end()) + { + m_netEntityIdMap.erase(found->second->GetId()); + m_entityMap.erase(found); + } } NetworkEntityTracker::EntityMap::iterator NetworkEntityTracker::erase(EntityMap::iterator iter) { ++m_deleteChangeDirty; + if (iter != m_entityMap.end()) + { + m_netEntityIdMap.erase(iter->second->GetId()); + } return m_entityMap.erase(iter); } diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.h b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.h index 7ff2d1da24..09238acaee 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.h +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.h @@ -22,6 +22,7 @@ namespace Multiplayer public: using EntityMap = AZStd::unordered_map; + using NetEntityIdMap = AZStd::unordered_map; using iterator = EntityMap::iterator; using const_iterator = EntityMap::const_iterator; @@ -36,6 +37,8 @@ namespace Multiplayer NetworkEntityHandle Get(NetEntityId netEntityId); ConstNetworkEntityHandle Get(NetEntityId netEntityId) const; + NetEntityId Get(const AZ::EntityId& entityId) const; + //! Returns true if the netEntityId exists. bool Exists(NetEntityId netEntityId) const; @@ -74,6 +77,7 @@ namespace Multiplayer private: EntityMap m_entityMap; + NetEntityIdMap m_netEntityIdMap; uint32_t m_deleteChangeDirty = 0; uint32_t m_addChangeDirty = 0; }; diff --git a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.inl b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.inl index c22fbb0da2..44098336be 100644 --- a/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.inl +++ b/Gems/Multiplayer/Code/Source/NetworkEntity/NetworkEntityTracker.inl @@ -48,6 +48,7 @@ namespace Multiplayer inline void NetworkEntityTracker::clear() { m_entityMap.clear(); + m_netEntityIdMap.clear(); } inline uint32_t NetworkEntityTracker::GetChangeDirty(const AZ::Entity* entity) const