-
Notifications
You must be signed in to change notification settings - Fork 3
[2409] Csv Spawner Notification Bus #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: o3de-2409
Are you sure you want to change the base?
Changes from 1 commit
6470d8a
6ccf97e
915d029
1b9affd
57322f8
ee5c565
399c2d1
5b7dd26
3fc175d
9cf218a
4651355
45bd34c
ffa7f34
23e9891
7049997
c91a03a
f70295a
5373024
09e44bf
ee39e1c
2fbc807
002c35b
139bb38
318d19f
0c45656
bb29e40
5a24a36
54e6f12
22c3628
51e1941
476f2b4
26cb4bd
62358ef
9e3cf1a
95b70d1
b30aac2
75e3739
61fc9aa
13671a4
2c17d49
896f067
687aff9
b30dda3
3694333
2e4d01a
3345918
48cd8d2
24c4e8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,14 @@ namespace CsvSpawner | |
* @brief Called when entity spawning begins. | ||
* @param m_spawnInfo Struct holding information about entities to be spawned. | ||
*/ | ||
virtual void OnEntitiesSpawnBegin(CsvSpawnerUtils::SpawnInfo& m_spawnInfo) = 0; | ||
virtual void OnEntitiesSpawnBegin(CsvSpawnerUtils::SpawnInfo m_spawnInfo) = 0; | ||
|
||
/** | ||
* @brief Called when entity spawning finishes. | ||
* @param m_spawnInfo Struct holding information about entities to be spawned. | ||
* @param m_statusCode Status code indicating success, failure and warnings of the spawn. | ||
*/ | ||
virtual void OnEntitiesSpawnFinished(CsvSpawnerUtils::SpawnInfo& m_spawnInfo, CsvSpawnerUtils::SpawnStatus m_statusCode) = 0; | ||
virtual void OnEntitiesSpawnFinished(CsvSpawnerUtils::SpawnInfo m_spawnInfo, CsvSpawnerUtils::SpawnStatus m_statusCode) = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for removing those references? |
||
|
||
/// EBus Configuration - Allows multiple listeners to handle events. | ||
static constexpr AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; | ||
|
@@ -62,12 +62,12 @@ namespace CsvSpawner | |
OnEntitiesSpawnBegin, | ||
OnEntitiesSpawnFinished); | ||
|
||
void OnEntitiesSpawnBegin(CsvSpawnerUtils::SpawnInfo& m_spawnInfo) override | ||
void OnEntitiesSpawnBegin(CsvSpawnerUtils::SpawnInfo m_spawnInfo) override | ||
{ | ||
Call(FN_OnEntitiesSpawnBegin, m_spawnInfo); | ||
} | ||
|
||
void OnEntitiesSpawnFinished(CsvSpawnerUtils::SpawnInfo& m_spawnInfo, CsvSpawnerUtils::SpawnStatus m_statusCode) override | ||
void OnEntitiesSpawnFinished(CsvSpawnerUtils::SpawnInfo m_spawnInfo, CsvSpawnerUtils::SpawnStatus m_statusCode) override | ||
{ | ||
Call(FN_OnEntitiesSpawnFinished, m_spawnInfo, m_statusCode); | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,8 @@ | |||||||||||
|
||||||||||||
#include "CsvSpawnerUtils.h" | ||||||||||||
|
||||||||||||
#include "AzCore/std/smart_ptr/make_shared.h" | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
#include <AzFramework/Physics/CollisionBus.h> | ||||||||||||
norbertprokopiuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
#include <CsvSpawner/CsvSpawnerInterface.h> | ||||||||||||
|
||||||||||||
|
@@ -223,11 +225,22 @@ namespace CsvSpawner::CsvSpawnerUtils | |||||||||||
{ | ||||||||||||
SpawnInfo broadcastSpawnInfo = | ||||||||||||
SpawnInfo{ entitiesToSpawn, physicsSceneName, parentId }; // Spawn Info used in CsvSpawner EBus notify. | ||||||||||||
SpawnStatus spawnStatusCode = SpawnStatus::Success; // Spawn Status Code used for CsvSpawner EBus notify - OnEntitiesSpawnFinished. | ||||||||||||
// SpawnStatus spawnStatusCode = SpawnStatus::Success; // Spawn Status Code used for CsvSpawner EBus notify - | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend to remove this code if it is commented |
||||||||||||
// OnEntitiesSpawnFinished. | ||||||||||||
auto spawnStatusCode = AZStd::make_shared<SpawnStatus>( | ||||||||||||
SpawnStatus::Success); // Spawn Status Code used for CsvSpawner EBus notify - OnEntitiesSpawnFinished. | ||||||||||||
|
||||||||||||
// Call CsvSpawner EBus notification - Begin | ||||||||||||
CsvSpawnerNotificationBus::Broadcast(&CsvSpawnerInterface::OnEntitiesSpawnBegin, broadcastSpawnInfo); | ||||||||||||
|
||||||||||||
// Check if there are no entities to spawn | ||||||||||||
if (entitiesToSpawn.empty()) | ||||||||||||
{ | ||||||||||||
*spawnStatusCode |= SpawnStatus::Fail; | ||||||||||||
CsvSpawnerNotificationBus::Broadcast(&CsvSpawnerInterface::OnEntitiesSpawnFinished, broadcastSpawnInfo, *spawnStatusCode); | ||||||||||||
return {}; | ||||||||||||
} | ||||||||||||
|
||||||||||||
auto sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get(); | ||||||||||||
AZ_Assert(sceneInterface, "Unable to get physics scene interface"); | ||||||||||||
const auto sceneHandle = sceneInterface->GetSceneHandle(physicsSceneName); | ||||||||||||
|
@@ -236,6 +249,8 @@ namespace CsvSpawner::CsvSpawnerUtils | |||||||||||
auto spawner = AZ::Interface<AzFramework::SpawnableEntitiesDefinition>::Get(); | ||||||||||||
AZ_Assert(spawner, "Unable to get spawnable entities definition"); | ||||||||||||
|
||||||||||||
auto pendingSpawns = AZStd::make_shared<AZStd::atomic_int>(static_cast<int>(entitiesToSpawn.size())); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inside for loop through |
||||||||||||
|
||||||||||||
// get parent transform | ||||||||||||
AZ::Transform parentTransform = AZ::Transform::CreateIdentity(); | ||||||||||||
if (parentId.IsValid()) | ||||||||||||
|
@@ -251,18 +266,14 @@ namespace CsvSpawner::CsvSpawnerUtils | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Track how many tickets are spawned and how many have completed | ||||||||||||
size_t totalTickets = 0; | ||||||||||||
std::atomic_size_t completedTickets = 0; | ||||||||||||
|
||||||||||||
for (const auto& entityConfig : entitiesToSpawn) | ||||||||||||
{ | ||||||||||||
if (!spawnableAssetConfiguration.contains(entityConfig.m_name)) | ||||||||||||
{ | ||||||||||||
AZ_Error("CsvSpawner", false, "SpawnableAssetConfiguration %s not found", entityConfig.m_name.c_str()); | ||||||||||||
|
||||||||||||
// Add notify code status | ||||||||||||
spawnStatusCode |= SpawnStatus::Warning; | ||||||||||||
*spawnStatusCode |= SpawnStatus::Warning; | ||||||||||||
continue; | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -293,7 +304,7 @@ namespace CsvSpawner::CsvSpawnerUtils | |||||||||||
else | ||||||||||||
{ | ||||||||||||
// Add notify code status | ||||||||||||
spawnStatusCode |= SpawnStatus::Warning; | ||||||||||||
*spawnStatusCode |= SpawnStatus::Warning; | ||||||||||||
|
||||||||||||
continue; // Skip this entity if we can't find a valid position and | ||||||||||||
// place on terrain is enabled. | ||||||||||||
|
@@ -309,7 +320,7 @@ namespace CsvSpawner::CsvSpawnerUtils | |||||||||||
if (view.empty()) | ||||||||||||
{ | ||||||||||||
// Add notify code status | ||||||||||||
spawnStatusCode |= SpawnStatus::Warning | SpawnStatus::Stopped; | ||||||||||||
*spawnStatusCode |= SpawnStatus::Warning | SpawnStatus::Stopped; | ||||||||||||
|
||||||||||||
return; | ||||||||||||
} | ||||||||||||
|
@@ -320,40 +331,31 @@ namespace CsvSpawner::CsvSpawnerUtils | |||||||||||
transformInterface->SetWorldTM(transform); | ||||||||||||
}; | ||||||||||||
optionalArgs.m_completionCallback = | ||||||||||||
[parentId, &spawnStatusCode, &broadcastSpawnInfo, totalTickets, &completedTickets]( | ||||||||||||
[parentId, spawnStatusCode, pendingSpawns, broadcastSpawnInfo]( | ||||||||||||
[[maybe_unused]] AzFramework::EntitySpawnTicket::Id ticketId, AzFramework::SpawnableConstEntityContainerView view) | ||||||||||||
{ | ||||||||||||
if (view.empty()) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code within this if statement exhibits undefined behavior because |
||||||||||||
{ | ||||||||||||
// Add notify code status | ||||||||||||
spawnStatusCode |= SpawnStatus::Warning | SpawnStatus::Stopped; | ||||||||||||
|
||||||||||||
return; | ||||||||||||
*spawnStatusCode |= SpawnStatus::Warning | SpawnStatus::Stopped; | ||||||||||||
} | ||||||||||||
else | ||||||||||||
{ | ||||||||||||
const AZ::Entity* root = *view.begin(); | ||||||||||||
AZ::TransformBus::Event(root->GetId(), &AZ::TransformBus::Events::SetParent, parentId); | ||||||||||||
Comment on lines
+346
to
+347
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
} | ||||||||||||
const AZ::Entity* root = *view.begin(); | ||||||||||||
AZ::TransformBus::Event(root->GetId(), &AZ::TransformBus::Events::SetParent, parentId); | ||||||||||||
|
||||||||||||
completedTickets++; | ||||||||||||
if (completedTickets == totalTickets) | ||||||||||||
// Decrement the pending counter | ||||||||||||
const int remaining = --(*pendingSpawns); | ||||||||||||
if (remaining == 0) | ||||||||||||
{ | ||||||||||||
// Call CsvSpawner EBus notification - Finished | ||||||||||||
// All spawns are finished, now broadcast finished notification | ||||||||||||
CsvSpawnerNotificationBus::Broadcast( | ||||||||||||
&CsvSpawnerInterface::OnEntitiesSpawnFinished, broadcastSpawnInfo, spawnStatusCode); | ||||||||||||
&CsvSpawnerInterface::OnEntitiesSpawnFinished, broadcastSpawnInfo, *spawnStatusCode); | ||||||||||||
} | ||||||||||||
}; | ||||||||||||
optionalArgs.m_priority = AzFramework::SpawnablePriority_Lowest; | ||||||||||||
spawner->SpawnAllEntities(ticket, optionalArgs); | ||||||||||||
tickets[entityConfig.m_id] = AZStd::move(ticket); | ||||||||||||
|
||||||||||||
totalTickets++; | ||||||||||||
} | ||||||||||||
|
||||||||||||
// If no tickets were created at all (no entities spawned), send finish immediately | ||||||||||||
if (totalTickets == 0) | ||||||||||||
{ | ||||||||||||
spawnStatusCode |= SpawnStatus::Fail; | ||||||||||||
// Call CsvSpawner EBus notification - Finished | ||||||||||||
CsvSpawnerNotificationBus::Broadcast(&CsvSpawnerInterface::OnEntitiesSpawnFinished, broadcastSpawnInfo, spawnStatusCode); | ||||||||||||
} | ||||||||||||
|
||||||||||||
return tickets; | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that created notification methods are correct, but I've noticed that you set
AddressPolicy
asSingle
. We can have multiple CSVSpawner components on the scene. It means that we should be send notification either when all components finishes their jobs (you would need system component for that), or you can change this notification busAddressPolicy
toById
to allow each component to send their own notification. Then it would be client call to decide on which entity/component notification they want to connect with.