Implemented a deferred LoadLevel queue for the SpawnableLevelSystem (#4561)

* Moved the SettingsRegistryTests.cpp and
SettingsRegistryMergeUtilsTests.cpp to the Settings folder

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Implemented a deferred level load queue, that allows the
SpawnableLevelSystem to re-run the last LoadLevel command that occured
before it was constructed.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added SettingsRegistryVisitorUtils to reduce Array and Object visitor
boilerplate.

The VisitArray and VisitObject functions allows iteration over each
element of array and object respectively via a callback.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed the queuing logic for levels that attempt to load before the SpawnableLevelSystem is available

Only the last level name that could not load is stored off and deferred until the SpawnableLevelsystem is created.

Made the FieldVisitor AggregateTypes constructor protected and added a comment specifying the expected values.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Bring in the SettingsRegistry::Visitor::Visit functions into scope to fix MSVC compilation errors.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Changed the list of supported SettingsRegistry types to visit to an enum to constrain the values to Array and/or Object.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
This commit is contained in:
lumberyard-employee-dm
2021-10-08 20:09:22 -05:00
committed by GitHub
parent e4d3ab118c
commit 7b1dd01d1d
10 changed files with 456 additions and 8 deletions
@@ -22,22 +22,33 @@
#include <LyShine/ILyShine.h>
#include <AzCore/Component/TickBus.h>
#include <AzCore/IO/Path/Path.h>
#include <AzCore/Settings/SettingsRegistryVisitorUtils.h>
#include <AzCore/StringFunc/StringFunc.h>
#include <AzCore/Script/ScriptSystemBus.h>
namespace LegacyLevelSystem
{
constexpr AZStd::string_view DeferredLoadLevelKey = "/O3DE/Runtime/SpawnableLevelSystem/DeferredLoadLevel";
//------------------------------------------------------------------------
static void LoadLevel(const AZ::ConsoleCommandContainer& arguments)
{
AZ_Error("SpawnableLevelSystem", !arguments.empty(), "LoadLevel requires a level file name to be provided.");
AZ_Error("SpawnableLevelSystem", arguments.size() == 1, "LoadLevel requires a single level file name to be provided.");
if (!arguments.empty() && gEnv->pSystem && gEnv->pSystem->GetILevelSystem() && !gEnv->IsEditor())
if (!arguments.empty() && gEnv && gEnv->pSystem && gEnv->pSystem->GetILevelSystem() && !gEnv->IsEditor())
{
gEnv->pSystem->GetILevelSystem()->LoadLevel(arguments[0].data());
}
else if (!arguments.empty())
{
// The SpawnableLevelSystem isn't available yet.
// Defer the level load until later by storing it in the SettingsRegistry
if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
{
settingsRegistry->Set(DeferredLoadLevelKey, arguments.front());
}
}
}
//------------------------------------------------------------------------
@@ -45,7 +56,7 @@ namespace LegacyLevelSystem
{
AZ_Warning("SpawnableLevelSystem", !arguments.empty(), "UnloadLevel doesn't use any arguments.");
if (gEnv->pSystem && gEnv->pSystem->GetILevelSystem() && !gEnv->IsEditor())
if (gEnv && gEnv->pSystem && gEnv->pSystem->GetILevelSystem() && !gEnv->IsEditor())
{
gEnv->pSystem->GetILevelSystem()->UnloadLevel();
}
@@ -73,6 +84,24 @@ namespace LegacyLevelSystem
}
AzFramework::RootSpawnableNotificationBus::Handler::BusConnect();
// If there were LoadLevel command invocations before the creation of the level system
// then those invocations were queued.
// load the last level in the queue, since only one level can be loaded at a time
if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
{
if (AZ::SettingsRegistryInterface::FixedValueString deferredLevelName;
settingsRegistry->Get(deferredLevelName, DeferredLoadLevelKey) && !deferredLevelName.empty())
{
// since this is the constructor any derived classes vtables aren't setup yet
// call this class LoadLevel function
AZ_TracePrintf("SpawnableLevelSystem", "The Level System is now available."
" Loading level %s which could not be loaded earlier\n", deferredLevelName.c_str());
SpawnableLevelSystem::LoadLevel(deferredLevelName.c_str());
// Delete the key with the deferred level name
settingsRegistry->Remove(DeferredLoadLevelKey);
}
}
}
//------------------------------------------------------------------------
@@ -173,7 +202,7 @@ namespace LegacyLevelSystem
}
// Make sure a spawnable level exists that matches levelname
AZStd::string validLevelName = "";
AZStd::string validLevelName;
AZ::Data::AssetId rootSpawnableAssetId;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
rootSpawnableAssetId, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetIdByPath, levelName, nullptr, false);