-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIG/Network - an example of using Filtering feature (i.e. omitting so…
…me entities on per connection basis) See o3de/o3de#1700 for the related core interfaces changes
- Loading branch information
Showing
9 changed files
with
3,758 additions
and
31 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
Gem/Code/Source/Components/ExampleFilteredEntityComponent.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) Contributors to the Open 3D Engine Project | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 OR MIT | ||
* | ||
*/ | ||
|
||
#include <AzCore/Serialization/EditContext.h> | ||
#include <Components/ExampleFilteredEntityComponent.h> | ||
|
||
AZ_CVAR(bool, mps_EnableFilteringEntities, true, nullptr, AZ::ConsoleFunctorFlags::Null, "If true, enables the example of filtering entities"); | ||
|
||
namespace MultiplayerSample | ||
{ | ||
void ExampleFilteredEntityComponent::Reflect(AZ::ReflectContext* context) | ||
{ | ||
AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context); | ||
if (serializeContext) | ||
{ | ||
serializeContext->Class<ExampleFilteredEntityComponent, AZ::Component>() | ||
->Field( "Enabled", &ExampleFilteredEntityComponent::m_enabled ) | ||
->Field( "Filter Names for Even Connections", &ExampleFilteredEntityComponent::m_filterNamesForEvenConnectionIds ) | ||
->Field( "Filter Names for Odd Connections", &ExampleFilteredEntityComponent::m_filterNamesForOddConnectionIds ) | ||
->Version(1); | ||
|
||
if (AZ::EditContext* editContext = serializeContext->GetEditContext()) | ||
{ | ||
using namespace AZ::Edit; | ||
editContext->Class<ExampleFilteredEntityComponent>("ExampleFilteredEntityComponent", "An example of filtering entities out in network replication") | ||
->ClassElement(ClassElements::EditorData, "") | ||
->Attribute(AZ::Edit::Attributes::Category, "MultiplayerSample") | ||
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Level")) | ||
->DataElement(nullptr, &ExampleFilteredEntityComponent::m_enabled, "Enabled", "enabled if checked") | ||
->DataElement(nullptr, &ExampleFilteredEntityComponent::m_filterNamesForEvenConnectionIds, "Filter for Even", | ||
"if an entity starts with this prefix, don't send them to even connections") | ||
->DataElement(nullptr, &ExampleFilteredEntityComponent::m_filterNamesForOddConnectionIds, "Filter for Odd", | ||
"if an entity starts with this prefix, don't send them to odd connections") | ||
; | ||
} | ||
} | ||
} | ||
|
||
void ExampleFilteredEntityComponent::Activate() | ||
{ | ||
Multiplayer::GetMultiplayer()->SetFilterEntityManager( this ); | ||
} | ||
|
||
void ExampleFilteredEntityComponent::Deactivate() | ||
{ | ||
Multiplayer::GetMultiplayer()->SetFilterEntityManager( nullptr ); | ||
} | ||
|
||
bool ExampleFilteredEntityComponent::IsEntityFiltered( | ||
[[maybe_unused]] AZ::Entity* entity, | ||
[[maybe_unused]] Multiplayer::ConstNetworkEntityHandle controllerEntity, | ||
[[maybe_unused]] AzNetworking::ConnectionId connectionId) | ||
{ | ||
if (m_enabled && mps_EnableFilteringEntities) | ||
{ | ||
// Note: @IsEntityFiltered is a hot code path, so do your best to optimize this method. | ||
// This example just uses entity names for filtering, for the sake of simplicity. | ||
// In practice, one might implement this lookup using a specialized structure such as AZStd::map<AZ::EntityId, bool>, etc. | ||
|
||
const bool evenConnectionId = static_cast<uint32_t>(connectionId) % 2 == 0; | ||
|
||
if (entity->GetName().starts_with( evenConnectionId ? m_filterNamesForEvenConnectionIds : m_filterNamesForOddConnectionIds )) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Gem/Code/Source/Components/ExampleFilteredEntityComponent.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) Contributors to the Open 3D Engine Project | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 OR MIT | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <AzCore/Component/Component.h> | ||
#include <Multiplayer/NetworkEntity/IFilterEntityManager.h> | ||
|
||
namespace MultiplayerSample | ||
{ | ||
//! @class ExampleFilteredEntityComponent | ||
//! @brief An example of using IFilterEntityManager to filter entities to clients. | ||
class ExampleFilteredEntityComponent final | ||
: public AZ::Component | ||
, public Multiplayer::IFilterEntityManager | ||
{ | ||
public: | ||
AZ_COMPONENT(MultiplayerSample::ExampleFilteredEntityComponent, "{7BF3BF1D-383A-40E7-BCF2-1ED5B2D2A43C}"); | ||
|
||
static void Reflect(AZ::ReflectContext* context); | ||
|
||
//! AZ::Component overrides. | ||
//! @{ | ||
void Activate() override; | ||
void Deactivate() override; | ||
//! }@ | ||
|
||
//! IFilterEntityManager overrides. | ||
//! @{ | ||
bool IsEntityFiltered(AZ::Entity* entity, Multiplayer::ConstNetworkEntityHandle controllerEntity, AzNetworking::ConnectionId connectionId) override; | ||
//! }@ | ||
|
||
private: | ||
bool m_enabled = true; | ||
|
||
AZStd::string m_filterNamesForEvenConnectionIds{ "Filter Even" }; | ||
AZStd::string m_filterNamesForOddConnectionIds{ "Filter Odd" }; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.