SpawnableEntitiesManagerTests.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzCore/UserSettings/UserSettingsComponent.h>
  10. #include <AzFramework/Application/Application.h>
  11. #include <AzFramework/Spawnable/SpawnableAssetHandler.h>
  12. #include <AzFramework/Spawnable/SpawnableEntitiesManager.h>
  13. #include <AzFramework/Components/TransformComponent.h>
  14. #include <AzTest/AzTest.h>
  15. namespace UnitTest
  16. {
  17. class TestApplication : public AzFramework::Application
  18. {
  19. public:
  20. // ComponentApplication
  21. void SetSettingsRegistrySpecializations(AZ::SettingsRegistryInterface::Specializations& specializations) override
  22. {
  23. Application::SetSettingsRegistrySpecializations(specializations);
  24. specializations.Append("test");
  25. specializations.Append("spawnable");
  26. }
  27. };
  28. // Test component that has a reference to a different entity for use in validating per-instance entity id fixups.
  29. class ComponentWithEntityReference : public AZ::Component
  30. {
  31. public:
  32. AZ_COMPONENT(ComponentWithEntityReference, "{CF5FDE59-86E5-40B6-9272-BBC1C4AFD061}");
  33. void Activate() override
  34. {
  35. }
  36. void Deactivate() override
  37. {
  38. }
  39. static void Reflect(AZ::ReflectContext* reflection)
  40. {
  41. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection))
  42. {
  43. serializeContext->Class<ComponentWithEntityReference, AZ::Component>()
  44. ->Field("EntityReference", &ComponentWithEntityReference::m_entityReference)
  45. ;
  46. }
  47. }
  48. AZ::EntityId m_entityReference;
  49. };
  50. class SourceSpawnableComponent : public AZ::Component
  51. {
  52. public:
  53. AZ_COMPONENT(SourceSpawnableComponent, "{47FF79CE-A95B-420E-8BEB-F1CC58087B87}");
  54. void Activate() override {}
  55. void Deactivate() override {}
  56. static void Reflect(AZ::ReflectContext* reflection)
  57. {
  58. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection))
  59. {
  60. serializeContext->Class<SourceSpawnableComponent, AZ::Component>();
  61. }
  62. }
  63. };
  64. class TargetSpawnableComponent : public AZ::Component
  65. {
  66. public:
  67. AZ_COMPONENT(TargetSpawnableComponent, "{B4041561-63A7-4E1E-80F1-78C08D497960}");
  68. TargetSpawnableComponent() = default;
  69. explicit TargetSpawnableComponent(AZ::EntityId parent)
  70. : m_parent(parent)
  71. {
  72. }
  73. void Activate() override {}
  74. void Deactivate() override {}
  75. static void Reflect(AZ::ReflectContext* reflection)
  76. {
  77. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection))
  78. {
  79. serializeContext->Class<TargetSpawnableComponent, AZ::Component>()
  80. ->Field("Parent", &TargetSpawnableComponent::m_parent);
  81. }
  82. }
  83. AZ::EntityId m_parent;
  84. };
  85. class SpawnableEntitiesManagerTest : public LeakDetectionFixture
  86. {
  87. public:
  88. constexpr static AZ::u64 EntityIdStartId = 40;
  89. void SetUp() override
  90. {
  91. LeakDetectionFixture::SetUp();
  92. m_application = new TestApplication();
  93. AZ::ComponentApplication::Descriptor descriptor;
  94. AZ::ComponentApplication::StartupParameters startupParameters;
  95. startupParameters.m_loadSettingsRegistry = false;
  96. m_application->Start(descriptor, startupParameters);
  97. m_application->RegisterComponentDescriptor(ComponentWithEntityReference::CreateDescriptor());
  98. m_application->RegisterComponentDescriptor(SourceSpawnableComponent::CreateDescriptor());
  99. m_application->RegisterComponentDescriptor(TargetSpawnableComponent::CreateDescriptor());
  100. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  101. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  102. // in the unit tests.
  103. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  104. m_spawnable = aznew AzFramework::Spawnable(
  105. AZ::Data::AssetId::CreateString("{EB2E8A2B-F253-4A90-BBF4-55F2EED786B8}:0"), AZ::Data::AssetData::AssetStatus::Ready);
  106. m_spawnableAsset = new AZ::Data::Asset<AzFramework::Spawnable>(m_spawnable, AZ::Data::AssetLoadBehavior::Default);
  107. m_ticket = aznew AzFramework::EntitySpawnTicket(*m_spawnableAsset);
  108. auto managerInterface = AzFramework::SpawnableEntitiesInterface::Get();
  109. m_manager = azrtti_cast<AzFramework::SpawnableEntitiesManager*>(managerInterface);
  110. }
  111. void TearDown() override
  112. {
  113. delete m_ticket;
  114. m_ticket = nullptr;
  115. // One more tick on the spawnable entities manager in order to delete the ticket fully.
  116. while (m_manager->ProcessQueue(
  117. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::High |
  118. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::Regular) !=
  119. AzFramework::SpawnableEntitiesManager::CommandQueueStatus::NoCommandsLeft)
  120. ;
  121. delete m_spawnableAsset;
  122. m_spawnableAsset = nullptr;
  123. // This will also delete m_spawnable.
  124. delete m_application;
  125. m_application = nullptr;
  126. LeakDetectionFixture::TearDown();
  127. }
  128. void ProcessQueueTillEmtpy()
  129. {
  130. for (size_t i=0; i<1000; ++i) // Don't do this indefinitely to avoid deadlocking on a failing test.
  131. {
  132. if (m_manager->ProcessQueue(
  133. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::High |
  134. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::Regular) ==
  135. AzFramework::SpawnableEntitiesManager::CommandQueueStatus::NoCommandsLeft)
  136. {
  137. break;
  138. }
  139. }
  140. }
  141. void FillSpawnable(size_t numElements)
  142. {
  143. AzFramework::Spawnable::EntityList& entities = m_spawnable->GetEntities();
  144. entities.clear();
  145. entities.reserve(numElements);
  146. for (size_t i=0; i<numElements; ++i)
  147. {
  148. auto entry = AZStd::make_unique<AZ::Entity>();
  149. entry->AddComponent(aznew SourceSpawnableComponent());
  150. entry->SetId(AZ::EntityId(EntityIdStartId + i));
  151. entities.push_back(AZStd::move(entry));
  152. }
  153. }
  154. AZ::Data::Asset<AzFramework::Spawnable> CreateTargetSpawnable(size_t numElements, bool requiresMatchingEntityIds)
  155. {
  156. auto target = aznew AzFramework::Spawnable(
  157. AZ::Data::AssetId(AZ::Uuid("{716CD8C3-0BA8-4F32-B579-0EC7C967796F}")), AZ::Data::AssetData::AssetStatus::Ready);
  158. AzFramework::Spawnable::EntityList& entities = target->GetEntities();
  159. entities.reserve(numElements);
  160. if (requiresMatchingEntityIds)
  161. {
  162. for (size_t i = 0; i < numElements; ++i)
  163. {
  164. auto entry = AZStd::make_unique<AZ::Entity>();
  165. if (i != 0)
  166. {
  167. entry->AddComponent(aznew TargetSpawnableComponent(AZ::EntityId(EntityIdStartId + i - 1)));
  168. }
  169. else
  170. {
  171. entry->AddComponent(aznew TargetSpawnableComponent());
  172. }
  173. entry->SetId(AZ::EntityId(EntityIdStartId + i));
  174. entities.push_back(AZStd::move(entry));
  175. }
  176. }
  177. else
  178. {
  179. for (size_t i = 0; i < numElements; ++i)
  180. {
  181. auto entry = AZStd::make_unique<AZ::Entity>();
  182. entry->AddComponent(aznew TargetSpawnableComponent());
  183. entities.push_back(AZStd::move(entry));
  184. }
  185. }
  186. return AZ::Data::Asset<AzFramework::Spawnable>(target, AZ::Data::AssetLoadBehavior::NoLoad);
  187. }
  188. template<size_t AliasCount>
  189. void InsertEntityAliases(
  190. const AZStd::array<uint32_t, AliasCount>& sourceIds,
  191. const AZStd::array<uint32_t, AliasCount>& targetIds,
  192. const AZStd::array<AzFramework::Spawnable::EntityAliasType, AliasCount>& aliasTypes,
  193. AZ::Data::Asset<AzFramework::Spawnable>* target = nullptr)
  194. {
  195. AzFramework::Spawnable::EntityAliasVisitor visitor = m_spawnable->TryGetAliases();
  196. for (uint32_t i = 0; i < AliasCount; ++i)
  197. {
  198. if (target)
  199. {
  200. visitor.AddAlias(*target, AZ::Crc32(i), sourceIds[i], targetIds[i], aliasTypes[i], false);
  201. }
  202. else
  203. {
  204. AZ::Data::Asset<AzFramework::Spawnable> spawnable(
  205. AZ::Data::AssetId(AZ::Uuid("{4CBEC17A-52D6-42D5-9037-F4C05B9CE1D9}"), i), azrtti_typeid<AzFramework::Spawnable>());
  206. visitor.AddAlias(AZStd::move(spawnable), AZ::Crc32(i), sourceIds[i], targetIds[i], aliasTypes[i], false);
  207. }
  208. }
  209. }
  210. static bool AreAllEntitiesReplaced(AzFramework::SpawnableConstEntityContainerView entities)
  211. {
  212. for (const AZ::Entity* entity : entities)
  213. {
  214. if (entity)
  215. {
  216. if (entity->FindComponent<SourceSpawnableComponent>() != nullptr ||
  217. entity->FindComponent<TargetSpawnableComponent>() == nullptr)
  218. {
  219. return false;
  220. }
  221. }
  222. else
  223. {
  224. return false;
  225. }
  226. }
  227. return true;
  228. }
  229. static bool DoParentEntityIdsMatch(AzFramework::SpawnableConstEntityContainerView entities)
  230. {
  231. if (entities.empty())
  232. {
  233. return false;
  234. }
  235. const AZ::Entity* previous = nullptr;
  236. for (const AZ::Entity* entity : entities)
  237. {
  238. if (entity)
  239. {
  240. if (previous)
  241. {
  242. if (TargetSpawnableComponent* link = entity->FindComponent<TargetSpawnableComponent>(); link != nullptr)
  243. {
  244. if (link->m_parent != previous->GetId())
  245. {
  246. return false;
  247. }
  248. }
  249. previous = entity;
  250. }
  251. }
  252. else
  253. {
  254. return false;
  255. }
  256. }
  257. return true;
  258. }
  259. static bool IsEveryOtherEntityAReplacement(AzFramework::SpawnableConstEntityContainerView entities)
  260. {
  261. bool onAlternative = true;
  262. for (const AZ::Entity* entity : entities)
  263. {
  264. if (entity)
  265. {
  266. if (onAlternative)
  267. {
  268. if (entity->FindComponent<SourceSpawnableComponent>() == nullptr ||
  269. entity->FindComponent<TargetSpawnableComponent>() != nullptr)
  270. {
  271. return false;
  272. }
  273. }
  274. else
  275. {
  276. if (entity->FindComponent<SourceSpawnableComponent>() != nullptr ||
  277. entity->FindComponent<TargetSpawnableComponent>() == nullptr)
  278. {
  279. return false;
  280. }
  281. }
  282. onAlternative = !onAlternative;
  283. }
  284. else
  285. {
  286. return false;
  287. }
  288. }
  289. return true;
  290. }
  291. static bool AreAllMerged(AzFramework::SpawnableConstEntityContainerView entities)
  292. {
  293. for (const AZ::Entity* entity : entities)
  294. {
  295. if (entity)
  296. {
  297. if (entity->FindComponent<SourceSpawnableComponent>() == nullptr ||
  298. entity->FindComponent<TargetSpawnableComponent>() == nullptr)
  299. {
  300. return false;
  301. }
  302. }
  303. else
  304. {
  305. return false;
  306. }
  307. }
  308. return true;
  309. }
  310. void CreateRecursiveHierarchy()
  311. {
  312. AzFramework::Spawnable::EntityList& entities = m_spawnable->GetEntities();
  313. size_t numElements = entities.size();
  314. AZ::EntityId parent;
  315. for (size_t i=0; i<numElements; ++i)
  316. {
  317. AZStd::unique_ptr<AZ::Entity>& entity = entities[i];
  318. auto component = entity->CreateComponent<AzFramework::TransformComponent>();
  319. if (i > 0)
  320. {
  321. component->SetParent(parent);
  322. }
  323. parent = entity->GetId();
  324. }
  325. }
  326. void CreateSingleParent()
  327. {
  328. AzFramework::Spawnable::EntityList& entities = m_spawnable->GetEntities();
  329. size_t numElements = entities.size();
  330. if (numElements > 0)
  331. {
  332. AZ::EntityId parent = entities[0]->GetId();
  333. for (size_t i = 0; i < numElements; ++i)
  334. {
  335. AZStd::unique_ptr<AZ::Entity>& entity = entities[i];
  336. auto component = entity->CreateComponent<AzFramework::TransformComponent>();
  337. if (i > 0)
  338. {
  339. component->SetParent(parent);
  340. }
  341. }
  342. }
  343. }
  344. enum class EntityReferenceScheme
  345. {
  346. AllReferenceFirst,
  347. AllReferenceLast,
  348. AllReferenceThemselves,
  349. AllReferenceNextCircular,
  350. AllReferencePreviousCircular
  351. };
  352. void CreateEntityReferences(EntityReferenceScheme refScheme)
  353. {
  354. AzFramework::Spawnable::EntityList& entities = m_spawnable->GetEntities();
  355. size_t numElements = entities.size();
  356. for (size_t i = 0; i < numElements; ++i)
  357. {
  358. AZStd::unique_ptr<AZ::Entity>& entity = entities[i];
  359. auto component = entity->CreateComponent<ComponentWithEntityReference>();
  360. switch (refScheme)
  361. {
  362. case EntityReferenceScheme::AllReferenceFirst :
  363. component->m_entityReference = entities[0]->GetId();
  364. break;
  365. case EntityReferenceScheme::AllReferenceLast:
  366. component->m_entityReference = entities[numElements - 1]->GetId();
  367. break;
  368. case EntityReferenceScheme::AllReferenceThemselves:
  369. component->m_entityReference = entities[i]->GetId();
  370. break;
  371. case EntityReferenceScheme::AllReferenceNextCircular:
  372. component->m_entityReference = entities[(i + 1) % numElements]->GetId();
  373. break;
  374. case EntityReferenceScheme::AllReferencePreviousCircular:
  375. component->m_entityReference = entities[(i + numElements - 1) % numElements]->GetId();
  376. break;
  377. }
  378. }
  379. }
  380. // Verify that the entity references are pointing to the correct other entities within the same spawn batch.
  381. // A "spawn batch" is the set of entities produced for each SpawnAllEntities command.
  382. void ValidateEntityReferences(
  383. EntityReferenceScheme refScheme, size_t entitiesPerBatch, AzFramework::SpawnableConstEntityContainerView entities)
  384. {
  385. size_t numElements = entities.size();
  386. for (size_t i = 0; i < numElements; ++i)
  387. {
  388. // Calculate the element offset that's the start of each batch of entities spawned.
  389. size_t curSpawnBatch = i / entitiesPerBatch;
  390. size_t curBatchOffset = curSpawnBatch * entitiesPerBatch;
  391. size_t curBatchIndex = i - curBatchOffset;
  392. const AZ::Entity* const entity = *(entities.begin() + i);
  393. auto component = entity->FindComponent<ComponentWithEntityReference>();
  394. ASSERT_NE(nullptr, component);
  395. AZ::EntityId comparisonId;
  396. // Ids should be local to a batch, so each of these will be compared within a batch of entities, not globally across
  397. // the entire set.
  398. switch (refScheme)
  399. {
  400. case EntityReferenceScheme::AllReferenceFirst:
  401. // Compare against the first entity in each batch
  402. comparisonId = (*(entities.begin() + curBatchOffset))->GetId();
  403. break;
  404. case EntityReferenceScheme::AllReferenceLast:
  405. // Compare against the last entity in each batch
  406. comparisonId = (*(entities.begin() + curBatchOffset + (entitiesPerBatch - 1)))->GetId();
  407. break;
  408. case EntityReferenceScheme::AllReferenceThemselves:
  409. // Compare against itself
  410. comparisonId = entity->GetId();
  411. break;
  412. case EntityReferenceScheme::AllReferenceNextCircular:
  413. // Compare against the next entity in each batch, looping around so that the last entity in the batch should refer
  414. // to the first entity in the batch.
  415. comparisonId = (*(entities.begin() + curBatchOffset + ((curBatchIndex + 1) % entitiesPerBatch)))->GetId();
  416. break;
  417. case EntityReferenceScheme::AllReferencePreviousCircular:
  418. // Compare against the previous entity in each batch, looping around so that the first entity in the batch should refer
  419. // to the last entity in the batch.
  420. comparisonId = (*(entities.begin() + curBatchOffset + ((curBatchIndex + numElements - 1) % entitiesPerBatch)))->GetId();
  421. break;
  422. }
  423. EXPECT_EQ(comparisonId, component->m_entityReference);
  424. }
  425. };
  426. protected:
  427. AZ::Data::Asset<AzFramework::Spawnable>* m_spawnableAsset { nullptr };
  428. AzFramework::SpawnableEntitiesManager* m_manager { nullptr };
  429. AzFramework::EntitySpawnTicket* m_ticket { nullptr };
  430. AzFramework::Spawnable* m_spawnable { nullptr };
  431. TestApplication* m_application { nullptr };
  432. };
  433. //
  434. // Constructors
  435. //
  436. TEST_F(SpawnableEntitiesManagerTest, EntitySpawnTicket_Move_Works)
  437. {
  438. AzFramework::EntitySpawnTicket ticket1(*m_spawnableAsset);
  439. AzFramework::EntitySpawnTicket ticket2(*m_spawnableAsset);
  440. const AzFramework::EntitySpawnTicket::Id ticket1Id = ticket1.GetId();
  441. const AzFramework::EntitySpawnTicket::Id ticket2Id = ticket2.GetId();
  442. AzFramework::EntitySpawnTicket ticketMoveConstructor(AZStd::move(ticket1));
  443. EXPECT_TRUE(ticketMoveConstructor.IsValid());
  444. EXPECT_EQ(ticketMoveConstructor.GetId(), ticket1Id);
  445. AzFramework::EntitySpawnTicket ticketMoveOperator;
  446. ticketMoveOperator = AZStd::move(ticket2);
  447. EXPECT_TRUE(ticketMoveOperator.IsValid());
  448. EXPECT_EQ(ticketMoveOperator.GetId(), ticket2Id);
  449. }
  450. //
  451. // SpawnAllEntitities
  452. //
  453. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_Call_AllEntitiesSpawned)
  454. {
  455. static constexpr size_t NumEntities = 4;
  456. FillSpawnable(NumEntities);
  457. size_t spawnedEntitiesCount = 0;
  458. auto callback =
  459. [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  460. {
  461. spawnedEntitiesCount += entities.size();
  462. };
  463. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  464. optionalArgs.m_completionCallback = AZStd::move(callback);
  465. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  466. ProcessQueueTillEmtpy();
  467. EXPECT_EQ(NumEntities, spawnedEntitiesCount);
  468. }
  469. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_SetParentOnSpawnedEntities_LineageIsPreserved)
  470. {
  471. static constexpr size_t NumEntities = 4;
  472. FillSpawnable(NumEntities);
  473. CreateRecursiveHierarchy();
  474. auto callback = [](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  475. {
  476. AZ::EntityId parentId;
  477. bool isFirst = true;
  478. for (const AZ::Entity* entity : entities)
  479. {
  480. if (!isFirst)
  481. {
  482. auto transform = entity->GetTransform();
  483. ASSERT_NE(nullptr, transform);
  484. EXPECT_EQ(parentId, transform->GetParentId());
  485. }
  486. else
  487. {
  488. isFirst = false;
  489. }
  490. parentId = entity->GetId();
  491. }
  492. };
  493. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  494. optionalArgs.m_completionCallback = AZStd::move(callback);
  495. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  496. ProcessQueueTillEmtpy();
  497. }
  498. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_AllEntitiesReferenceOtherEntities_EntityIdsAreMappedCorrectly)
  499. {
  500. // This tests that entity id references get mapped correctly in a SpawnAllEntities call whether they're forward referencing
  501. // in the list, backwards referencing, or self-referencing. The circular tests are to ensure the implementation works regardless
  502. // of entity ordering.
  503. for (EntityReferenceScheme refScheme : {
  504. EntityReferenceScheme::AllReferenceFirst, EntityReferenceScheme::AllReferenceLast,
  505. EntityReferenceScheme::AllReferenceThemselves, EntityReferenceScheme::AllReferenceNextCircular,
  506. EntityReferenceScheme::AllReferencePreviousCircular })
  507. {
  508. constexpr size_t NumEntities = 4;
  509. FillSpawnable(NumEntities);
  510. CreateEntityReferences(refScheme);
  511. auto callback = [this, refScheme]
  512. (AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  513. {
  514. ValidateEntityReferences(refScheme, NumEntities, entities);
  515. };
  516. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  517. optionalArgs.m_completionCallback = AZStd::move(callback);
  518. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  519. ProcessQueueTillEmtpy();
  520. }
  521. }
  522. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_AllEntitiesReferenceOtherEntities_EntityIdsOnlyReferWithinASingleCall)
  523. {
  524. // This tests that entity id references get mapped correctly with multiple SpawnAllEntities calls. Each call should only map
  525. // the entities to other entities within the same call, regardless of forward or backward mapping.
  526. // For example, suppose entities 1, 2, and 3 refer to 4. In the first SpawnAllEntities call, entities 1-3 will refer to 4.
  527. // In the second SpawnAllEntities call, entities 1-3 will refer to the second 4, not the previously-spawned 4.
  528. for (EntityReferenceScheme refScheme :
  529. { EntityReferenceScheme::AllReferenceFirst, EntityReferenceScheme::AllReferenceLast,
  530. EntityReferenceScheme::AllReferenceThemselves, EntityReferenceScheme::AllReferenceNextCircular,
  531. EntityReferenceScheme::AllReferencePreviousCircular
  532. })
  533. {
  534. // Make sure we start with a fresh ticket each time, or else each iteration through this loop would continue to build up
  535. // more and more entities.
  536. delete m_ticket;
  537. m_ticket = aznew AzFramework::EntitySpawnTicket(*m_spawnableAsset);
  538. constexpr size_t NumEntities = 4;
  539. FillSpawnable(NumEntities);
  540. CreateEntityReferences(refScheme);
  541. auto callback = [this, refScheme]
  542. (AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  543. {
  544. ValidateEntityReferences(refScheme, NumEntities, entities);
  545. };
  546. // Spawn twice.
  547. constexpr size_t NumSpawnAllCalls = 2;
  548. for (int spawns = 0; spawns < NumSpawnAllCalls; spawns++)
  549. {
  550. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  551. optionalArgs.m_completionCallback = AZStd::move(callback);
  552. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  553. }
  554. m_manager->ListEntities(*m_ticket, callback);
  555. ProcessQueueTillEmtpy();
  556. }
  557. }
  558. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_DeleteTicketBeforeCall_NoCrash)
  559. {
  560. {
  561. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  562. m_manager->SpawnAllEntities(ticket);
  563. }
  564. ProcessQueueTillEmtpy();
  565. }
  566. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_AllAliasesWithDisabled_NoEntitiesSpawned)
  567. {
  568. using namespace AzFramework;
  569. static constexpr size_t NumEntities = 4;
  570. FillSpawnable(NumEntities);
  571. InsertEntityAliases<NumEntities>(
  572. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  573. { Spawnable::EntityAliasType::Disable, Spawnable::EntityAliasType::Disable, Spawnable::EntityAliasType::Disable,
  574. Spawnable::EntityAliasType::Disable });
  575. size_t spawnedEntitiesCount = 0;
  576. auto callback = [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  577. {
  578. spawnedEntitiesCount += entities.size();
  579. };
  580. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  581. optionalArgs.m_completionCallback = AZStd::move(callback);
  582. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  583. ProcessQueueTillEmtpy();
  584. EXPECT_EQ(0, spawnedEntitiesCount);
  585. }
  586. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_SomeAliasesWithDisabled_RegularEntitiesAreSpawned)
  587. {
  588. using namespace AzFramework;
  589. static constexpr size_t NumEntities = 8;
  590. FillSpawnable(NumEntities);
  591. InsertEntityAliases<2>({ 1, 3 }, { 1, 3 }, { Spawnable::EntityAliasType::Disable, Spawnable::EntityAliasType::Disable });
  592. size_t spawnedEntitiesCount = 0;
  593. auto callback = [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  594. {
  595. spawnedEntitiesCount += entities.size();
  596. };
  597. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  598. optionalArgs.m_completionCallback = AZStd::move(callback);
  599. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  600. ProcessQueueTillEmtpy();
  601. EXPECT_EQ(6, spawnedEntitiesCount);
  602. }
  603. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_AllAliasesWithReplace_EntitiesSpawnedFromTarget)
  604. {
  605. using namespace AzFramework;
  606. static constexpr size_t NumEntities = 4;
  607. FillSpawnable(NumEntities);
  608. constexpr bool requiresMatchingEntityIds = true;
  609. AZ::Data::Asset<Spawnable> target = CreateTargetSpawnable(4, requiresMatchingEntityIds);
  610. InsertEntityAliases<4>(
  611. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  612. { Spawnable::EntityAliasType::Replace, Spawnable::EntityAliasType::Replace, Spawnable::EntityAliasType::Replace,
  613. Spawnable::EntityAliasType::Replace },
  614. &target);
  615. size_t spawnedEntitiesCount = 0;
  616. bool allReplaced = false;
  617. bool allEntityIdsPatched = false;
  618. auto callback = [&spawnedEntitiesCount, &allReplaced, &allEntityIdsPatched](
  619. AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  620. {
  621. spawnedEntitiesCount += entities.size();
  622. allReplaced = AreAllEntitiesReplaced(entities);
  623. allEntityIdsPatched = DoParentEntityIdsMatch(entities);
  624. };
  625. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  626. optionalArgs.m_completionCallback = AZStd::move(callback);
  627. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  628. ProcessQueueTillEmtpy();
  629. EXPECT_EQ(4, spawnedEntitiesCount);
  630. EXPECT_TRUE(allReplaced);
  631. EXPECT_TRUE(allEntityIdsPatched);
  632. }
  633. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_AllAliasesWithAdditional_SourceAndTargetComponentsMerged)
  634. {
  635. using namespace AzFramework;
  636. static constexpr size_t NumEntities = 4;
  637. FillSpawnable(NumEntities);
  638. constexpr bool requiresMatchingEntityIds = false;
  639. AZ::Data::Asset<Spawnable> target = CreateTargetSpawnable(4, requiresMatchingEntityIds);
  640. InsertEntityAliases<4>(
  641. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  642. { Spawnable::EntityAliasType::Additional, Spawnable::EntityAliasType::Additional, Spawnable::EntityAliasType::Additional,
  643. Spawnable::EntityAliasType::Additional },
  644. &target);
  645. size_t spawnedEntitiesCount = 0;
  646. bool allAdded = false;
  647. bool allEntityIdsPatched = false;
  648. auto callback = [&spawnedEntitiesCount, &allAdded, &allEntityIdsPatched](
  649. AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  650. {
  651. spawnedEntitiesCount += entities.size();
  652. allAdded = IsEveryOtherEntityAReplacement(entities);
  653. allEntityIdsPatched = DoParentEntityIdsMatch(entities);
  654. };
  655. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  656. optionalArgs.m_completionCallback = AZStd::move(callback);
  657. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  658. ProcessQueueTillEmtpy();
  659. EXPECT_EQ(8, spawnedEntitiesCount);
  660. EXPECT_TRUE(allAdded);
  661. EXPECT_TRUE(allEntityIdsPatched);
  662. }
  663. TEST_F(SpawnableEntitiesManagerTest, SpawnAllEntities_AllAliasesWithMerge_SourceAndTargetComponentsMerged)
  664. {
  665. using namespace AzFramework;
  666. static constexpr size_t NumEntities = 4;
  667. FillSpawnable(NumEntities);
  668. constexpr bool requiresMatchingEntityIds = true;
  669. AZ::Data::Asset<Spawnable> target = CreateTargetSpawnable(4, requiresMatchingEntityIds);
  670. InsertEntityAliases<4>(
  671. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  672. { Spawnable::EntityAliasType::Merge, Spawnable::EntityAliasType::Merge, Spawnable::EntityAliasType::Merge,
  673. Spawnable::EntityAliasType::Merge },
  674. &target);
  675. size_t spawnedEntitiesCount = 0;
  676. bool allMerged = false;
  677. bool allEntityIdsPatched = false;
  678. auto callback = [&spawnedEntitiesCount, &allMerged, &allEntityIdsPatched](
  679. AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  680. {
  681. spawnedEntitiesCount += entities.size();
  682. allMerged = AreAllMerged(entities);
  683. allEntityIdsPatched = DoParentEntityIdsMatch(entities);
  684. };
  685. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  686. optionalArgs.m_completionCallback = AZStd::move(callback);
  687. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  688. ProcessQueueTillEmtpy();
  689. EXPECT_EQ(4, spawnedEntitiesCount);
  690. EXPECT_TRUE(allMerged);
  691. EXPECT_TRUE(allEntityIdsPatched);
  692. }
  693. //
  694. // SpawnEntities
  695. //
  696. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_Call_AllEntitiesSpawned)
  697. {
  698. static constexpr size_t NumEntities = 4;
  699. FillSpawnable(NumEntities);
  700. AZStd::vector<uint32_t> indices = { 0, 2, 3, 1 };
  701. size_t spawnedEntitiesCount = 0;
  702. auto callback = [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  703. {
  704. spawnedEntitiesCount += entities.size();
  705. };
  706. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  707. optionalArgs.m_completionCallback = AZStd::move(callback);
  708. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  709. ProcessQueueTillEmtpy();
  710. EXPECT_EQ(NumEntities, spawnedEntitiesCount);
  711. }
  712. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_SpawnTheSameEntity_AllEntitiesSpawned)
  713. {
  714. static constexpr size_t NumEntities = 1;
  715. FillSpawnable(NumEntities);
  716. AZStd::vector<uint32_t> indices = { 0, 0 };
  717. size_t spawnedEntitiesCount = 0;
  718. auto callback =
  719. [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  720. {
  721. spawnedEntitiesCount += entities.size();
  722. };
  723. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  724. optionalArgs.m_completionCallback = AZStd::move(callback);
  725. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  726. ProcessQueueTillEmtpy();
  727. EXPECT_EQ(NumEntities * 2, spawnedEntitiesCount);
  728. }
  729. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_MultipleSpawns_AllEntitiesSpawned)
  730. {
  731. static constexpr size_t NumEntities = 4;
  732. FillSpawnable(NumEntities);
  733. AZStd::vector<uint32_t> indices = { 0, 2, 3, 1 };
  734. size_t spawnedEntitiesCount = 0;
  735. auto callback =
  736. [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  737. {
  738. spawnedEntitiesCount += entities.size();
  739. };
  740. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  741. optionalArgs.m_completionCallback = AZStd::move(callback);
  742. m_manager->SpawnEntities(*m_ticket, indices, optionalArgs);
  743. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  744. ProcessQueueTillEmtpy();
  745. EXPECT_EQ(NumEntities * 2, spawnedEntitiesCount);
  746. }
  747. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_ReferencesAreRemappedForNewBatch_AllPointToLatestParent)
  748. {
  749. static constexpr size_t NumEntities = 4;
  750. FillSpawnable(NumEntities);
  751. CreateSingleParent();
  752. AZStd::vector<uint32_t> indices = { 0, 1, 2, 3 };
  753. AZStd::vector<AZ::EntityId> parents;
  754. auto callback = [&parents](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  755. {
  756. AZ::EntityId parent = (*entities.begin())->GetId();
  757. parents.push_back(parent);
  758. auto it = entities.begin();
  759. ++it; // Skip the first as that is the parent.
  760. for (; it != entities.end(); ++it)
  761. {
  762. AZ::TransformInterface* transform = (*it)->GetTransform();
  763. ASSERT_NE(nullptr, transform);
  764. ASSERT_EQ(parent, transform->GetParentId());
  765. }
  766. };
  767. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  768. optionalArgs.m_completionCallback = AZStd::move(callback);
  769. optionalArgs.m_referencePreviouslySpawnedEntities = false;
  770. m_manager->SpawnEntities(*m_ticket, indices, optionalArgs);
  771. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  772. ProcessQueueTillEmtpy();
  773. EXPECT_NE(parents[0], parents[1]);
  774. }
  775. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_ReferencesAreRemappedForContinuedBatch_AllPointToLatestParent)
  776. {
  777. static constexpr size_t NumEntities = 4;
  778. FillSpawnable(NumEntities);
  779. CreateSingleParent();
  780. AZStd::vector<uint32_t> indices = { 0, 1, 2, 3 };
  781. AZStd::vector<AZ::EntityId> parents;
  782. auto callback =
  783. [&parents](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  784. {
  785. AZ::EntityId parent = (*entities.begin())->GetId();
  786. parents.push_back(parent);
  787. auto it = entities.begin();
  788. ++it; // Skip the first as that is the parent.
  789. for (; it!=entities.end(); ++it)
  790. {
  791. AZ::TransformInterface* transform = (*it)->GetTransform();
  792. ASSERT_NE(nullptr, transform);
  793. ASSERT_EQ(parent, transform->GetParentId());
  794. }
  795. };
  796. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  797. optionalArgs.m_completionCallback = AZStd::move(callback);
  798. optionalArgs.m_referencePreviouslySpawnedEntities = true;
  799. m_manager->SpawnEntities(*m_ticket, indices, optionalArgs);
  800. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  801. ProcessQueueTillEmtpy();
  802. EXPECT_NE(parents[0], parents[1]);
  803. }
  804. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_ReferencesAreRemappedAcrossBatches_AllPointToLatestParent)
  805. {
  806. FillSpawnable(4);
  807. CreateSingleParent();
  808. // Spawn a regular batch but with two parents and store the id of the last entity. This will the parent for the next batch.
  809. AZ::EntityId parent;
  810. auto getParent = [&parent](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  811. {
  812. ASSERT_NE(entities.begin(), entities.end());
  813. parent = (*AZStd::prev(entities.end()))->GetId();
  814. };
  815. AzFramework::SpawnEntitiesOptionalArgs optionalArgsFirstBatch;
  816. optionalArgsFirstBatch.m_completionCallback = AZStd::move(getParent);
  817. optionalArgsFirstBatch.m_referencePreviouslySpawnedEntities = true;
  818. m_manager->SpawnEntities(*m_ticket, {0, 1, 2, 3, 0}, AZStd::move(optionalArgsFirstBatch));
  819. // Next, spawn all the entities that have a reference to the parent that was just stored.
  820. auto parentCheck = [&parent](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  821. {
  822. for (auto& it : entities)
  823. {
  824. AZ::TransformInterface* transform = it->GetTransform();
  825. ASSERT_NE(nullptr, transform);
  826. ASSERT_EQ(parent, transform->GetParentId());
  827. }
  828. };
  829. AzFramework::SpawnEntitiesOptionalArgs optionalArgsSecondBatch;
  830. optionalArgsSecondBatch.m_completionCallback = AZStd::move(parentCheck);
  831. optionalArgsSecondBatch.m_referencePreviouslySpawnedEntities = true;
  832. m_manager->SpawnEntities(*m_ticket, {1, 2, 3}, AZStd::move(optionalArgsSecondBatch));
  833. ProcessQueueTillEmtpy();
  834. }
  835. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllEntitiesReferenceOtherEntities_ForwardReferencesWorkInSingleCall)
  836. {
  837. constexpr EntityReferenceScheme refScheme = EntityReferenceScheme::AllReferenceNextCircular;
  838. constexpr size_t NumEntities = 4;
  839. FillSpawnable(NumEntities);
  840. CreateEntityReferences(refScheme);
  841. AZ_PUSH_DISABLE_WARNING(5233, "-Wunused-lambda-capture") // Older versions of MSVC toolchain require to pass constexpr in the
  842. // capture. Newer versions issue unused warning
  843. auto callback =
  844. [this, refScheme, NumEntities](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  845. AZ_POP_DISABLE_WARNING
  846. {
  847. ValidateEntityReferences(refScheme, NumEntities, entities);
  848. };
  849. // Verify that by default, entities that refer to other entities that haven't been spawned yet have the correct references
  850. // when the spawning all occurs in the same call
  851. m_manager->SpawnEntities(*m_ticket, { 0, 1, 2, 3 });
  852. m_manager->ListEntities(*m_ticket, callback);
  853. ProcessQueueTillEmtpy();
  854. }
  855. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllEntitiesReferenceOtherEntities_ForwardReferencesWorkAcrossCalls)
  856. {
  857. constexpr EntityReferenceScheme refScheme = EntityReferenceScheme::AllReferenceNextCircular;
  858. constexpr size_t NumEntities = 4;
  859. FillSpawnable(NumEntities);
  860. CreateEntityReferences(refScheme);
  861. AZ_PUSH_DISABLE_WARNING(5233, "-Wunused-lambda-capture") // Older versions of MSVC toolchain require to pass constexpr in the
  862. // capture. Newer versions issue unused warning
  863. auto callback =
  864. [this, refScheme, NumEntities](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  865. AZ_POP_DISABLE_WARNING
  866. {
  867. ValidateEntityReferences(refScheme, NumEntities, entities);
  868. };
  869. // Verify that by default, entities that refer to other entities that haven't been spawned yet have the correct references
  870. // even when the spawning is across multiple calls
  871. m_manager->SpawnEntities(*m_ticket, { 0 });
  872. m_manager->SpawnEntities(*m_ticket, { 1 });
  873. m_manager->SpawnEntities(*m_ticket, { 2 });
  874. m_manager->SpawnEntities(*m_ticket, { 3 });
  875. m_manager->ListEntities(*m_ticket, callback);
  876. ProcessQueueTillEmtpy();
  877. }
  878. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllEntitiesReferenceOtherEntities_ReferencesPointToFirstOrLatest)
  879. {
  880. // With SpawnEntities, entity references should either refer to the first entity that *will* be spawned, or the last entity
  881. // that *has* been spawned. This test will create entities 0 1 2 3 that all refer to entity 3, and it will create two batches
  882. // of those. In the first batch, they'll forward-reference. In the second batch, they should backward-reference, except for
  883. // the second entity 3, which will now refer to itself as the last one that's been spawned.
  884. constexpr EntityReferenceScheme refScheme = EntityReferenceScheme::AllReferenceLast;
  885. constexpr size_t NumEntities = 4;
  886. FillSpawnable(NumEntities);
  887. CreateEntityReferences(refScheme);
  888. auto callback =
  889. [](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  890. {
  891. size_t numElements = entities.size();
  892. for (size_t i = 0; i < numElements; ++i)
  893. {
  894. const AZ::Entity* const entity = *(entities.begin() + i);
  895. auto component = entity->FindComponent<ComponentWithEntityReference>();
  896. ASSERT_NE(nullptr, component);
  897. AZ::EntityId comparisonId;
  898. if (i < (numElements - 1))
  899. {
  900. // There are two batches of NumEntities elements. Every entity should either forward-reference or backward-reference
  901. // to the last entity of the first batch, except for the very last entity of the second batch, which should reference
  902. // itself.
  903. comparisonId = (*(entities.begin() + (NumEntities- 1)))->GetId();
  904. }
  905. else
  906. {
  907. // The very last entity of the second batch should reference itself because it's now the latest instance of that
  908. // entity to be spawned.
  909. comparisonId = entity->GetId();
  910. }
  911. EXPECT_EQ(comparisonId, component->m_entityReference);
  912. }
  913. };
  914. // Create 2 batches of forward references. In the first batch, entities 0 1 2 will point forward to 3. In the second batch,
  915. // entities 0 1 2 will point *backward* to the first 3, and the second entity 3 will point to itself.
  916. m_manager->SpawnEntities(*m_ticket, { 0, 1, 2, 3 });
  917. m_manager->SpawnEntities(*m_ticket, { 0, 1, 2, 3 });
  918. m_manager->ListEntities(*m_ticket, callback);
  919. ProcessQueueTillEmtpy();
  920. }
  921. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllEntitiesReferenceOtherEntities_MultipleSpawnsInSameCallReferenceCorrectly)
  922. {
  923. // With SpawnEntities, entity references should either refer to the first entity that *will* be spawned, or the last entity
  924. // that *has* been spawned. This test will create entities 0 1 2 3 that all refer to entity 3, and it will create three sets
  925. // of those in the same call, with the following results:
  926. // - The first 0 1 2 will forward-reference to the first 3
  927. // - The first 3 will reference itself
  928. // - The second 0 1 2 will backwards-reference to the first 3
  929. // - The second 3 will reference itself
  930. // - The third 0 1 2 will backwards-reference to the second 3
  931. // - The third 3 will reference itself
  932. constexpr EntityReferenceScheme refScheme = EntityReferenceScheme::AllReferenceLast;
  933. constexpr size_t NumEntities = 4;
  934. FillSpawnable(NumEntities);
  935. CreateEntityReferences(refScheme);
  936. auto callback =
  937. [](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  938. {
  939. size_t numElements = entities.size();
  940. for (size_t i = 0; i < numElements; ++i)
  941. {
  942. const AZ::Entity* const entity = *(entities.begin() + i);
  943. auto component = entity->FindComponent<ComponentWithEntityReference>();
  944. ASSERT_NE(nullptr, component);
  945. AZ::EntityId comparisonId;
  946. if (i < ((NumEntities * 2) - 1))
  947. {
  948. // The first 7 entities (0 1 2 3 0 1 2) will all refer to the 4th one (1st '3').
  949. comparisonId = (*(entities.begin() + (NumEntities - 1)))->GetId();
  950. }
  951. else if (i < (numElements - 1))
  952. {
  953. // The next 4 entities (3 0 1 2) will all refer to the 8th one (2nd '3').
  954. comparisonId = (*(entities.begin() + ((NumEntities * 2) - 1)))->GetId();
  955. }
  956. else
  957. {
  958. // The very last entity (3) will reference itself (3rd '3').
  959. comparisonId = entity->GetId();
  960. }
  961. EXPECT_EQ(comparisonId, component->m_entityReference);
  962. }
  963. };
  964. // Create the 3 batches of entities 0, 1, 2, 3. The entity references should work as described at the top of the test.
  965. m_manager->SpawnEntities(*m_ticket, { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 });
  966. m_manager->ListEntities(*m_ticket, callback);
  967. ProcessQueueTillEmtpy();
  968. }
  969. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllEntitiesReferenceOtherEntities_OptionalFlagClearsReferenceMap)
  970. {
  971. constexpr EntityReferenceScheme refScheme = EntityReferenceScheme::AllReferenceLast;
  972. constexpr size_t NumEntities = 4;
  973. FillSpawnable(NumEntities);
  974. CreateEntityReferences(refScheme);
  975. AZ_PUSH_DISABLE_WARNING(5233, "-Wunused-lambda-capture") // Older versions of MSVC toolchain require to pass constexpr in the
  976. // capture. Newer versions issue unused warning
  977. auto callback =
  978. [this, refScheme, NumEntities](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  979. AZ_POP_DISABLE_WARNING
  980. {
  981. ValidateEntityReferences(refScheme, NumEntities, entities);
  982. };
  983. // By setting the "referencePreviouslySpawnedEntities" flag to false, the map will get cleared on each call, so in both batches
  984. // the entities will forward-reference to the last entity in the batch. If the flag were true, entities 0 1 2 in the second
  985. // batch would refer backwards to the first entity 3.
  986. AzFramework::SpawnEntitiesOptionalArgs optionalArgsSecondBatch;
  987. optionalArgsSecondBatch.m_completionCallback = AZStd::move(callback);
  988. optionalArgsSecondBatch.m_referencePreviouslySpawnedEntities = false;
  989. m_manager->SpawnEntities(*m_ticket, { 0, 1, 2, 3 }, optionalArgsSecondBatch);
  990. m_manager->SpawnEntities(*m_ticket, { 0, 1, 2, 3 }, AZStd::move(optionalArgsSecondBatch));
  991. m_manager->ListEntities(*m_ticket, callback);
  992. ProcessQueueTillEmtpy();
  993. }
  994. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_DeleteTicketBeforeCall_NoCrash)
  995. {
  996. {
  997. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  998. m_manager->SpawnEntities(ticket, {/* Deliberate empty list of indices. */});
  999. }
  1000. ProcessQueueTillEmtpy();
  1001. }
  1002. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllAliasesWithDisabled_NoEntitiesSpawned)
  1003. {
  1004. using namespace AzFramework;
  1005. static constexpr size_t NumEntities = 4;
  1006. FillSpawnable(NumEntities);
  1007. InsertEntityAliases<NumEntities>(
  1008. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  1009. { Spawnable::EntityAliasType::Disable, Spawnable::EntityAliasType::Disable, Spawnable::EntityAliasType::Disable,
  1010. Spawnable::EntityAliasType::Disable });
  1011. AZStd::vector<uint32_t> indices = { 0, 2, 3, 1 };
  1012. size_t spawnedEntitiesCount = 0;
  1013. auto callback = [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  1014. {
  1015. spawnedEntitiesCount += entities.size();
  1016. };
  1017. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  1018. optionalArgs.m_completionCallback = AZStd::move(callback);
  1019. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  1020. ProcessQueueTillEmtpy();
  1021. EXPECT_EQ(0, spawnedEntitiesCount);
  1022. }
  1023. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_SomeAliasesWithDisabled_RegularEntitiesAreSpawned)
  1024. {
  1025. using namespace AzFramework;
  1026. FillSpawnable(8);
  1027. InsertEntityAliases<3>(
  1028. { 1, 3, 6 }, { 1, 3, 6 },
  1029. { Spawnable::EntityAliasType::Disable, Spawnable::EntityAliasType::Disable, Spawnable::EntityAliasType::Disable });
  1030. AZStd::vector<uint32_t> indices = { 0, 2, 3, 1, 2, 3, 0, 1, 6, 4, 5, 7, 4, 1, 0, 6 };
  1031. size_t spawnedEntitiesCount = 0;
  1032. auto callback = [&spawnedEntitiesCount](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  1033. {
  1034. spawnedEntitiesCount += entities.size();
  1035. };
  1036. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  1037. optionalArgs.m_completionCallback = AZStd::move(callback);
  1038. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  1039. ProcessQueueTillEmtpy();
  1040. EXPECT_EQ(9, spawnedEntitiesCount);
  1041. }
  1042. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllAliasesWithReplace_EntitiesSpawnedFromTarget)
  1043. {
  1044. using namespace AzFramework;
  1045. static constexpr size_t NumEntities = 4;
  1046. FillSpawnable(NumEntities);
  1047. constexpr bool requiresMatchingEntityIds = true;
  1048. AZ::Data::Asset<Spawnable> target = CreateTargetSpawnable(4, requiresMatchingEntityIds);
  1049. InsertEntityAliases<4>(
  1050. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  1051. { Spawnable::EntityAliasType::Replace, Spawnable::EntityAliasType::Replace, Spawnable::EntityAliasType::Replace,
  1052. Spawnable::EntityAliasType::Replace },
  1053. &target);
  1054. AZStd::vector<uint32_t> indices = { 0, 2, 3, 1 };
  1055. size_t spawnedEntitiesCount = 0;
  1056. bool allReplaced = false;
  1057. bool allEntityIdsPatched = false;
  1058. auto callback = [&spawnedEntitiesCount, &allReplaced, &allEntityIdsPatched](
  1059. AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  1060. {
  1061. spawnedEntitiesCount += entities.size();
  1062. allReplaced = AreAllEntitiesReplaced(entities);
  1063. allEntityIdsPatched = DoParentEntityIdsMatch(entities);
  1064. };
  1065. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  1066. optionalArgs.m_completionCallback = AZStd::move(callback);
  1067. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  1068. ProcessQueueTillEmtpy();
  1069. EXPECT_EQ(4, spawnedEntitiesCount);
  1070. EXPECT_TRUE(allReplaced);
  1071. EXPECT_TRUE(allEntityIdsPatched);
  1072. }
  1073. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllAliasesWithAdditional_SourceAndTargetComponentsMerged)
  1074. {
  1075. using namespace AzFramework;
  1076. static constexpr size_t NumEntities = 4;
  1077. FillSpawnable(NumEntities);
  1078. constexpr bool requiresMatchingEntityIds = false;
  1079. AZ::Data::Asset<Spawnable> target = CreateTargetSpawnable(4, requiresMatchingEntityIds);
  1080. InsertEntityAliases<4>(
  1081. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  1082. { Spawnable::EntityAliasType::Additional, Spawnable::EntityAliasType::Additional, Spawnable::EntityAliasType::Additional,
  1083. Spawnable::EntityAliasType::Additional },
  1084. &target);
  1085. AZStd::vector<uint32_t> indices = { 0, 2, 3, 1 };
  1086. size_t spawnedEntitiesCount = 0;
  1087. bool allAdded = false;
  1088. bool allEntityIdsPatched = false;
  1089. auto callback =
  1090. [&spawnedEntitiesCount, &allAdded, &allEntityIdsPatched](
  1091. AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  1092. {
  1093. spawnedEntitiesCount += entities.size();
  1094. allAdded = IsEveryOtherEntityAReplacement(entities);
  1095. allEntityIdsPatched = DoParentEntityIdsMatch(entities);
  1096. };
  1097. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  1098. optionalArgs.m_completionCallback = AZStd::move(callback);
  1099. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  1100. ProcessQueueTillEmtpy();
  1101. EXPECT_EQ(8, spawnedEntitiesCount);
  1102. EXPECT_TRUE(allAdded);
  1103. EXPECT_TRUE(allEntityIdsPatched);
  1104. }
  1105. TEST_F(SpawnableEntitiesManagerTest, SpawnEntities_AllAliasesWithMerge_SourceAndTargetComponentsMerged)
  1106. {
  1107. using namespace AzFramework;
  1108. static constexpr size_t NumEntities = 4;
  1109. FillSpawnable(NumEntities);
  1110. constexpr bool requiresMatchingEntityIds = true;
  1111. AZ::Data::Asset<Spawnable> target = CreateTargetSpawnable(4, requiresMatchingEntityIds);
  1112. InsertEntityAliases<4>(
  1113. { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
  1114. { Spawnable::EntityAliasType::Merge, Spawnable::EntityAliasType::Merge, Spawnable::EntityAliasType::Merge,
  1115. Spawnable::EntityAliasType::Merge },
  1116. &target);
  1117. AZStd::vector<uint32_t> indices = { 0, 2, 3, 1 };
  1118. size_t spawnedEntitiesCount = 0;
  1119. bool allMerged = false;
  1120. bool allEntityIdsPatched = false;
  1121. auto callback = [&spawnedEntitiesCount, &allMerged, &allEntityIdsPatched](
  1122. AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  1123. {
  1124. spawnedEntitiesCount += entities.size();
  1125. allMerged = AreAllMerged(entities);
  1126. allEntityIdsPatched = DoParentEntityIdsMatch(entities);
  1127. };
  1128. AzFramework::SpawnEntitiesOptionalArgs optionalArgs;
  1129. optionalArgs.m_completionCallback = AZStd::move(callback);
  1130. m_manager->SpawnEntities(*m_ticket, AZStd::move(indices), AZStd::move(optionalArgs));
  1131. ProcessQueueTillEmtpy();
  1132. EXPECT_EQ(4, spawnedEntitiesCount);
  1133. EXPECT_TRUE(allMerged);
  1134. EXPECT_TRUE(allEntityIdsPatched);
  1135. }
  1136. //
  1137. // DespawnAllEntities
  1138. //
  1139. TEST_F(SpawnableEntitiesManagerTest, DespawnAllEntities_DeleteTicketBeforeCall_NoCrash)
  1140. {
  1141. {
  1142. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  1143. m_manager->DespawnAllEntities(ticket);
  1144. }
  1145. ProcessQueueTillEmtpy();
  1146. }
  1147. //
  1148. // ReloadSpawnable
  1149. //
  1150. TEST_F(SpawnableEntitiesManagerTest, ReloadSpawnable_DeleteTicketBeforeCall_NoCrash)
  1151. {
  1152. {
  1153. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  1154. m_manager->ReloadSpawnable(ticket, *m_spawnableAsset);
  1155. }
  1156. ProcessQueueTillEmtpy();
  1157. }
  1158. //
  1159. // ListEntitities
  1160. //
  1161. TEST_F(SpawnableEntitiesManagerTest, ListEntities_Call_AllEntitiesAreReported)
  1162. {
  1163. static constexpr size_t NumEntities = 4;
  1164. FillSpawnable(NumEntities);
  1165. bool allValidEntityIds = true;
  1166. size_t spawnedEntitiesCount = 0;
  1167. auto callback = [&allValidEntityIds, &spawnedEntitiesCount]
  1168. (AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView entities)
  1169. {
  1170. for (auto&& entity : entities)
  1171. {
  1172. allValidEntityIds = entity->GetId().IsValid() && allValidEntityIds;
  1173. }
  1174. spawnedEntitiesCount += entities.size();
  1175. };
  1176. m_manager->SpawnAllEntities(*m_ticket);
  1177. m_manager->ListEntities(*m_ticket, AZStd::move(callback));
  1178. ProcessQueueTillEmtpy();
  1179. EXPECT_TRUE(allValidEntityIds);
  1180. EXPECT_EQ(NumEntities, spawnedEntitiesCount);
  1181. }
  1182. TEST_F(SpawnableEntitiesManagerTest, ListEntities_DeleteTicketBeforeCall_NoCrash)
  1183. {
  1184. auto callback = [](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView) {};
  1185. {
  1186. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  1187. m_manager->ListEntities(ticket, AZStd::move(callback));
  1188. }
  1189. ProcessQueueTillEmtpy();
  1190. }
  1191. //
  1192. // ListIndicesAndEntities
  1193. //
  1194. TEST_F(SpawnableEntitiesManagerTest, ListIndicesAndEntities_Call_AllEntitiesAreReportedAndIncrementByOne)
  1195. {
  1196. static constexpr size_t NumEntities = 4;
  1197. FillSpawnable(NumEntities);
  1198. bool allValidEntityIds = true;
  1199. size_t spawnedEntitiesCount = 0;
  1200. auto callback = [&allValidEntityIds, &spawnedEntitiesCount]
  1201. (AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstIndexEntityContainerView entities)
  1202. {
  1203. for (auto&& indexEntityPair : entities)
  1204. {
  1205. // Since all entities are spawned a single time, the indices should be 0..NumEntities.
  1206. if (indexEntityPair.GetIndex() == spawnedEntitiesCount)
  1207. {
  1208. spawnedEntitiesCount++;
  1209. }
  1210. allValidEntityIds = indexEntityPair.GetEntity()->GetId().IsValid() && allValidEntityIds;
  1211. }
  1212. };
  1213. m_manager->SpawnAllEntities(*m_ticket);
  1214. m_manager->ListIndicesAndEntities(*m_ticket, AZStd::move(callback));
  1215. ProcessQueueTillEmtpy();
  1216. EXPECT_TRUE(allValidEntityIds);
  1217. EXPECT_EQ(NumEntities, spawnedEntitiesCount);
  1218. }
  1219. TEST_F(SpawnableEntitiesManagerTest, ListIndicesAndEntities_DeleteTicketBeforeCall_NoCrash)
  1220. {
  1221. auto callback = [](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstIndexEntityContainerView) {};
  1222. {
  1223. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  1224. m_manager->ListIndicesAndEntities(ticket, AZStd::move(callback));
  1225. }
  1226. ProcessQueueTillEmtpy();
  1227. }
  1228. //
  1229. // ClaimEntities
  1230. //
  1231. TEST_F(SpawnableEntitiesManagerTest, ClaimEntities_Call_AllEntitiesWereClaimedAndNotDeleted)
  1232. {
  1233. static constexpr size_t NumEntities = 4;
  1234. FillSpawnable(NumEntities);
  1235. AZStd::vector<AZ::Entity*> claimedEntities;
  1236. auto callback = [&claimedEntities](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableEntityContainerView container)
  1237. {
  1238. for (AZ::Entity* entity : container)
  1239. {
  1240. claimedEntities.push_back(entity);
  1241. }
  1242. };
  1243. {
  1244. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  1245. m_manager->SpawnAllEntities(ticket);
  1246. m_manager->ClaimEntities(ticket, AZStd::move(callback));
  1247. ProcessQueueTillEmtpy();
  1248. }
  1249. EXPECT_EQ(NumEntities, claimedEntities.size());
  1250. // If these calls fail it means that the ticket has still deleted the entities, so they weren't properly claimed.
  1251. for (AZ::Entity* entity : claimedEntities)
  1252. {
  1253. delete entity;
  1254. }
  1255. }
  1256. TEST_F(SpawnableEntitiesManagerTest, ClaimEntities_DeleteTicketBeforeCall_NoCrash)
  1257. {
  1258. auto callback = [](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableEntityContainerView) {};
  1259. {
  1260. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  1261. m_manager->ClaimEntities(ticket, AZStd::move(callback));
  1262. }
  1263. ProcessQueueTillEmtpy();
  1264. }
  1265. //
  1266. // Barrier
  1267. //
  1268. TEST_F(SpawnableEntitiesManagerTest, Barrier_DeleteTicketBeforeCall_NoCrash)
  1269. {
  1270. auto callback = [](AzFramework::EntitySpawnTicket::Id) {};
  1271. {
  1272. AzFramework::EntitySpawnTicket ticket(*m_spawnableAsset);
  1273. m_manager->Barrier(ticket, AZStd::move(callback));
  1274. }
  1275. ProcessQueueTillEmtpy();
  1276. }
  1277. //
  1278. // Misc. - Priority tests
  1279. //
  1280. TEST_F(SpawnableEntitiesManagerTest, Priority_HighBeforeDefault_HigherPriorityCallHappensBeforeDefaultPriorityEvenWhenQueuedLater)
  1281. {
  1282. static constexpr size_t NumEntities = 4;
  1283. FillSpawnable(NumEntities);
  1284. AzFramework::EntitySpawnTicket highPriorityTicket(*m_spawnableAsset);
  1285. size_t callCounter = 1;
  1286. size_t highPriorityCallId = 0;
  1287. size_t defaultPriorityCallId = 0;
  1288. auto highCallback = [&callCounter, &highPriorityCallId]
  1289. (AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView)
  1290. {
  1291. highPriorityCallId = callCounter++;
  1292. };
  1293. auto defaultCallback = [&callCounter, &defaultPriorityCallId]
  1294. (AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView)
  1295. {
  1296. defaultPriorityCallId = callCounter++;
  1297. };
  1298. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  1299. optionalArgs.m_completionCallback = AZStd::move(defaultCallback);
  1300. optionalArgs.m_priority = AzFramework::SpawnablePriority_Default;
  1301. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  1302. AzFramework::SpawnAllEntitiesOptionalArgs highPriortyOptionalArgs;
  1303. highPriortyOptionalArgs.m_completionCallback = AZStd::move(highCallback);
  1304. highPriortyOptionalArgs.m_priority = AzFramework::SpawnablePriority_High;
  1305. m_manager->SpawnAllEntities(highPriorityTicket, AZStd::move(highPriortyOptionalArgs));
  1306. ProcessQueueTillEmtpy();
  1307. EXPECT_LT(highPriorityCallId, defaultPriorityCallId);
  1308. }
  1309. TEST_F(SpawnableEntitiesManagerTest, Priority_SameTicket_DefaultPriorityCallHappensBeforeHighPriority)
  1310. {
  1311. static constexpr size_t NumEntities = 4;
  1312. FillSpawnable(NumEntities);
  1313. size_t callCounter = 1;
  1314. size_t highPriorityCallId = 0;
  1315. size_t defaultPriorityCallId = 0;
  1316. auto highCallback =
  1317. [&callCounter, &highPriorityCallId](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView)
  1318. {
  1319. highPriorityCallId = callCounter++;
  1320. };
  1321. auto defaultCallback =
  1322. [&callCounter, &defaultPriorityCallId](AzFramework::EntitySpawnTicket::Id, AzFramework::SpawnableConstEntityContainerView)
  1323. {
  1324. defaultPriorityCallId = callCounter++;
  1325. };
  1326. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  1327. optionalArgs.m_completionCallback = AZStd::move(defaultCallback);
  1328. optionalArgs.m_priority = AzFramework::SpawnablePriority_Default;
  1329. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(optionalArgs));
  1330. AzFramework::SpawnAllEntitiesOptionalArgs highPriortyOptionalArgs;
  1331. highPriortyOptionalArgs.m_completionCallback = AZStd::move(highCallback);
  1332. highPriortyOptionalArgs.m_priority = AzFramework::SpawnablePriority_High;
  1333. m_manager->SpawnAllEntities(*m_ticket, AZStd::move(highPriortyOptionalArgs));
  1334. m_manager->ProcessQueue(
  1335. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::High |
  1336. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::Regular);
  1337. // Run a second time as the high priority task will be pending at this point.
  1338. m_manager->ProcessQueue(
  1339. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::High |
  1340. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::Regular);
  1341. EXPECT_LT(defaultPriorityCallId, highPriorityCallId);
  1342. }
  1343. } // namespace UnitTest