diff --git a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.ScriptCanvasNodeable.xml b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.ScriptCanvasNodeable.xml index d0c4cfd806..e9b1ce9f4e 100644 --- a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.ScriptCanvasNodeable.xml +++ b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.ScriptCanvasNodeable.xml @@ -10,16 +10,16 @@ Version="0" GeneratePropertyFriend="True" Namespace="ScriptCanvas" - Description="Spawn"> + Description="Spawns a selected prefab, positioned using the provided transform inputs"> - - + + - + /> diff --git a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.cpp b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.cpp index 37bb64745a..1bfd3e2386 100644 --- a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.cpp +++ b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.cpp @@ -15,127 +15,125 @@ #include #include -namespace ScriptCanvas +namespace ScriptCanvas::Nodeables::Spawning { - namespace Nodeables + SpawnNodeable::SpawnNodeable(const SpawnNodeable& rhs) + : m_spawnableAsset(rhs.m_spawnableAsset) + {} + + SpawnNodeable& SpawnNodeable::operator=(SpawnNodeable& rhs) { - namespace Spawning + m_spawnableAsset = rhs.m_spawnableAsset; + return *this; + } + + void SpawnNodeable::OnInitializeExecutionState() + { + if (!AZ::TickBus::Handler::BusIsConnected()) { - SpawnNodeable::SpawnNodeable(const SpawnNodeable& rhs) + AZ::TickBus::Handler::BusConnect(); + } + + m_spawnTicket = AzFramework::EntitySpawnTicket(m_spawnableAsset); + } + + void SpawnNodeable::OnDeactivate() + { + AZ::TickBus::Handler::BusDisconnect(); + + m_spawnTicket = AzFramework::EntitySpawnTicket(); + } + + void SpawnNodeable::OnTick([[maybe_unused]] float delta, [[maybe_unused]] AZ::ScriptTimePoint timePoint) + { + AZStd::vector swappedSpawnedEntityList; + AZStd::vector swappedSpawnBatchSizes; + { + AZStd::lock_guard lock(m_idBatchMutex); + + swappedSpawnedEntityList.swap(m_spawnedEntityList); + swappedSpawnBatchSizes.swap(m_spawnBatchSizes); + } + + AZ::EntityId* batchBegin = swappedSpawnedEntityList.data(); + for (size_t batchSize : swappedSpawnBatchSizes) + { + if (batchSize == 0) { - m_spawnableAsset = rhs.m_spawnableAsset; + continue; } - void SpawnNodeable::OnInitializeExecutionState() - { - if (!AZ::TickBus::Handler::BusIsConnected()) - { - AZ::TickBus::Handler::BusConnect(); - } + AZStd::vector spawnedEntitiesBatch( + batchBegin, batchBegin + batchSize); - m_spawnTicket = AzFramework::EntitySpawnTicket(m_spawnableAsset); + CallOnSpawn(AZStd::move(spawnedEntitiesBatch)); + + batchBegin += batchSize; + } + } + + void SpawnNodeable::OnSpawnAssetChanged() + { + if (m_spawnableAsset.GetId().IsValid()) + { + AZStd::string rootSpawnableFile; + AzFramework::StringFunc::Path::GetFileName(m_spawnableAsset.GetHint().c_str(), rootSpawnableFile); + + rootSpawnableFile += AzFramework::Spawnable::DotFileExtension; + + AZ::u32 rootSubId = AzFramework::SpawnableAssetHandler::BuildSubId(AZStd::move(rootSpawnableFile)); + + if (m_spawnableAsset.GetId().m_subId != rootSubId) + { + AZ::Data::AssetId rootAssetId = m_spawnableAsset.GetId(); + rootAssetId.m_subId = rootSubId; + + m_spawnableAsset = AZ::Data::AssetManager::Instance(). + FindOrCreateAsset(rootAssetId, AZ::Data::AssetLoadBehavior::PreLoad); } - - void SpawnNodeable::OnDeactivate() + else { - if (AZ::TickBus::Handler::BusIsConnected()) - { - AZ::TickBus::Handler::BusDisconnect(); - } - - m_spawnTicket = AzFramework::EntitySpawnTicket(); - } - - void SpawnNodeable::OnTick([[maybe_unused]] float delta, [[maybe_unused]] AZ::ScriptTimePoint timePoint) - { - AZStd::vector swappedSpawnedEntityList; - AZStd::vector swappedSpawnBatchSizes; - { - AZStd::lock_guard lock(m_idBatchMutex); - - swappedSpawnedEntityList.swap(m_spawnedEntityList); - swappedSpawnBatchSizes.swap(m_spawnBatchSizes); - } - - AZ::EntityId* batchBegin = swappedSpawnedEntityList.data(); - for (size_t batchSize : swappedSpawnBatchSizes) - { - if (batchSize == 0) - { - continue; - } - - AZStd::vector spawnedEntitiesBatch( - batchBegin, batchBegin + batchSize); - - CallOnSpawn(AZStd::move(spawnedEntitiesBatch)); - - batchBegin += batchSize; - } - } - - void SpawnNodeable::OnSpawnAssetChanged() - { - if (m_spawnableAsset.GetId().IsValid()) - { - AZStd::string rootSpawnableFile; - AzFramework::StringFunc::Path::GetFileName(m_spawnableAsset.GetHint().c_str(), rootSpawnableFile); - - rootSpawnableFile += AzFramework::Spawnable::DotFileExtension; - - AZ::u32 rootSubId = AzFramework::SpawnableAssetHandler::BuildSubId(AZStd::move(rootSpawnableFile)); - - if (m_spawnableAsset.GetId().m_subId != rootSubId) - { - AZ::Data::AssetId rootAssetId = m_spawnableAsset.GetId(); - rootAssetId.m_subId = rootSubId; - - m_spawnableAsset = AZ::Data::AssetManager::Instance(). - FindOrCreateAsset(rootAssetId, AZ::Data::AssetLoadBehavior::Default); - } - - m_spawnableAsset.SetAutoLoadBehavior(AZ::Data::AssetLoadBehavior::PreLoad); - } - } - - void SpawnNodeable::RequestSpawn(Data::Vector3Type translation, Data::Vector3Type rotation, Data::NumberType scale) - { - if (!m_spawnableAsset.IsReady()) - { - return; - } - - auto preSpawnCB = [this, translation, rotation, scale]([[maybe_unused]] AzFramework::EntitySpawnTicket& ticket, - AzFramework::SpawnableEntityContainerView view) - { - AZ::Entity* rootEntity = *view.begin(); - - AzFramework::TransformComponent* entityTransform = - rootEntity->FindComponent(); - - if (entityTransform) - { - AZ::Vector3 rotationCopy = rotation; - AZ::Quaternion rotationQuat = AZ::Quaternion::CreateFromEulerAnglesDegrees(rotationCopy); - - entityTransform->SetWorldTM(AZ::Transform(translation, rotationQuat, AZ::Vector3(scale, scale, scale))); - } - }; - - auto spawnCompleteCB = [this]([[maybe_unused]] AzFramework::EntitySpawnTicket& ticket, - AzFramework::SpawnableConstEntityContainerView view) - { - AZStd::lock_guard lock(m_idBatchMutex); - m_spawnedEntityList.reserve(m_spawnedEntityList.size() + view.size()); - for (const AZ::Entity* entity : view) - { - m_spawnedEntityList.emplace_back(entity->GetId()); - } - m_spawnBatchSizes.push_back(view.size()); - }; - - AzFramework::SpawnableEntitiesInterface::Get()->SpawnAllEntities(m_spawnTicket, preSpawnCB, spawnCompleteCB); + m_spawnableAsset.SetAutoLoadBehavior(AZ::Data::AssetLoadBehavior::PreLoad); } } } + + void SpawnNodeable::RequestSpawn(Data::Vector3Type translation, Data::Vector3Type rotation, Data::NumberType scale) + { + if (!m_spawnableAsset.IsReady()) + { + return; + } + + auto preSpawnCB = [this, translation, rotation, scale]([[maybe_unused]] AzFramework::EntitySpawnTicket& ticket, + AzFramework::SpawnableEntityContainerView view) + { + AZ::Entity* rootEntity = *view.begin(); + + AzFramework::TransformComponent* entityTransform = + rootEntity->FindComponent(); + + if (entityTransform) + { + AZ::Vector3 rotationCopy = rotation; + AZ::Quaternion rotationQuat = AZ::Quaternion::CreateFromEulerAnglesDegrees(rotationCopy); + + entityTransform->SetWorldTM(AZ::Transform(translation, rotationQuat, AZ::Vector3(scale, scale, scale))); + } + }; + + auto spawnCompleteCB = [this]([[maybe_unused]] AzFramework::EntitySpawnTicket& ticket, + AzFramework::SpawnableConstEntityContainerView view) + { + AZStd::lock_guard lock(m_idBatchMutex); + m_spawnedEntityList.reserve(m_spawnedEntityList.size() + view.size()); + for (const AZ::Entity* entity : view) + { + m_spawnedEntityList.emplace_back(entity->GetId()); + } + m_spawnBatchSizes.push_back(view.size()); + }; + + AzFramework::SpawnableEntitiesInterface::Get()->SpawnAllEntities(m_spawnTicket, preSpawnCB, spawnCompleteCB); + } } diff --git a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.h b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.h index 2b2a22601d..0f3a27d2ea 100644 --- a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.h +++ b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Libraries/Spawning/SpawnNodeable.h @@ -22,36 +22,31 @@ #include #include -namespace ScriptCanvas +namespace ScriptCanvas::Nodeables::Spawning { - namespace Nodeables + class SpawnNodeable + : public ScriptCanvas::Nodeable, + public AZ::TickBus::Handler { - namespace Spawning - { - class SpawnNodeable - : public ScriptCanvas::Nodeable, - public AZ::TickBus::Handler - { - SCRIPTCANVAS_NODE(SpawnNodeable); - public: - SpawnNodeable() = default; - SpawnNodeable(const SpawnNodeable& rhs); + SCRIPTCANVAS_NODE(SpawnNodeable); + public: + SpawnNodeable() = default; + SpawnNodeable(const SpawnNodeable& rhs); + SpawnNodeable& operator=(SpawnNodeable& rhs); - void OnInitializeExecutionState() override; - void OnDeactivate() override; + void OnInitializeExecutionState() override; + void OnDeactivate() override; - //TickBus - void OnTick(float delta, AZ::ScriptTimePoint timePoint) override; + //TickBus + void OnTick(float delta, AZ::ScriptTimePoint timePoint) override; - void OnSpawnAssetChanged(); + void OnSpawnAssetChanged(); - private: - AzFramework::EntitySpawnTicket m_spawnTicket; + private: + AzFramework::EntitySpawnTicket m_spawnTicket; - AZStd::vector m_spawnedEntityList; - AZStd::vector m_spawnBatchSizes; - AZStd::recursive_mutex m_idBatchMutex; - }; - } - } + AZStd::vector m_spawnedEntityList; + AZStd::vector m_spawnBatchSizes; + AZStd::recursive_mutex m_idBatchMutex; + }; }