Skip to content

Commit fb53fff

Browse files
committed
[ROS2] Add helper IsAutonomousOrNonMultiplayer
Added a new function "IsAutonomousOrNonMultiplayer" to the "ROS2GemUtilities" to help components determine if they are attached to an entity that is Networked. This is preliminary work to allow components to be activated selectively in the future, avoiding duplicated work for the ROS2 multiplayer architecture. Signed-off-by: Lars Gleim <[email protected]>
1 parent 609cb88 commit fb53fff

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Gems/ROS2/Code/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ ly_add_target(
3838
PUBLIC
3939
AZ::AzCore
4040
AZ::AzFramework
41+
# AZ::AzNetworking
4142
Gem::Atom_RPI.Public
4243
Gem::Atom_Feature_Common.Static
4344
Gem::Atom_Component_DebugCamera.Static
4445
Gem::StartingPointInput
46+
Gem::Multiplayer
4547
Gem::PhysX.Static
48+
PRIVATE
49+
Gem::Multiplayer.Unified.Static
4650
)
4751

4852
target_depends_on_ros2_packages(${gem_name}.Static rclcpp builtin_interfaces std_msgs sensor_msgs nav_msgs tf2_ros ackermann_msgs gazebo_msgs)
@@ -71,6 +75,7 @@ ly_add_target(
7175
Source
7276
BUILD_DEPENDENCIES
7377
PRIVATE
78+
Gem::Multiplayer.Unified.Static
7479
Gem::${gem_name}.Static
7580
Gem::Atom_Feature_Common.Static
7681
)

Gems/ROS2/Code/Include/ROS2/ROS2GemUtilities.h

+11
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
#include <AzCore/Component/ComponentBus.h>
1111
#include <AzCore/Component/Entity.h>
12+
#include <AzFramework/Components/TransformComponent.h>
1213
#ifdef ROS2_EDITOR
1314
#include <AzToolsFramework/ToolsComponents/GenericComponentWrapper.h>
1415
#endif
16+
#include <Multiplayer/Components/NetBindComponent.h>
17+
1518
namespace ROS2
1619
{
1720
namespace Utils
@@ -43,5 +46,13 @@ namespace ROS2
4346
return component;
4447
}
4548

49+
/// Checks if the entity or an ancestor is a multiplayer entity (i.e. has the NetBindComponent).
50+
/// If yes,
51+
/// @param entity
52+
/// @return
53+
Multiplayer::NetBindComponent* GetEntityOrAncestorNetBind(const AZ::Entity* entity);
54+
55+
bool IsAutonomousOrNonMultiplayer(const AZ::Entity* entity);
56+
4657
} // namespace Utils
4758
} // namespace ROS2

Gems/ROS2/Code/Source/ROS2GemUtilities.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,40 @@ namespace ROS2
3838
return AZ::InvalidComponentId;
3939
}
4040

41+
Multiplayer::NetBindComponent* Utils::GetEntityOrAncestorNetBind(const AZ::Entity* entity)
42+
{
43+
if(auto* component = GetGameOrEditorComponent<Multiplayer::NetBindComponent>(entity))
44+
{
45+
return component; // Found it!
46+
}
47+
48+
// Entity has no NetBindComponent. Let's check its ancestor
49+
auto* entityTransformInterface = GetGameOrEditorComponent<AzFramework::TransformComponent>(entity);
50+
if (!entityTransformInterface)
51+
{
52+
AZ_Error("GetEntityOrAncestorNetBind", false, "Invalid transform interface!");
53+
return nullptr;
54+
}
55+
56+
AZ::EntityId parentEntityId = entityTransformInterface->GetParentId();
57+
if (!parentEntityId.IsValid())
58+
{ // We have reached the top level, there is no parent entity
59+
return nullptr;
60+
}
61+
62+
AZ::Entity* parentEntity = nullptr;
63+
AZ::ComponentApplicationBus::BroadcastResult(parentEntity, &AZ::ComponentApplicationRequests::FindEntity, parentEntityId);
64+
AZ_Assert(parentEntity, "No parent entity id : %s", parentEntityId.ToString().c_str());
65+
66+
return GetEntityOrAncestorNetBind(parentEntity);
67+
}
68+
69+
bool Utils::IsAutonomousOrNonMultiplayer(const AZ::Entity* entity) {
70+
if(Multiplayer::NetBindComponent* nbc = GetEntityOrAncestorNetBind(entity))
71+
{
72+
return nbc->IsNetEntityRoleAutonomous();
73+
}
74+
return true; // Non-multiplayer: No NetBindComponent, so no multiplayer entity in the hierarchy
75+
}
76+
4177
} // namespace ROS2

0 commit comments

Comments
 (0)