Fix spawning nested prefab with different root actor

#1138
This commit is contained in:
Wojtek Figat
2023-06-11 20:43:31 +02:00
parent 60ddf0ea89
commit 958c7b2181
7 changed files with 61 additions and 23 deletions

View File

@@ -2,6 +2,7 @@
#include "Prefab.h"
#include "Engine/Serialization/JsonTools.h"
#include "Engine/Content/Content.h"
#include "Engine/Content/Factories/JsonAssetFactory.h"
#include "Engine/Core/Log.h"
#include "Engine/Level/Prefabs/PrefabManager.h"
@@ -21,6 +22,36 @@ Prefab::Prefab(const SpawnParams& params, const AssetInfo* info)
{
}
Guid Prefab::GetRootObjectId() const
{
ASSERT(IsLoaded());
ScopeLock lock(Locker);
// Root is always the first but handle case when prefab root was reordered in the base prefab while the nested prefab has still the old state
// TODO: resave and force sync prefabs during game cooking so this step could be skipped in game
int32 objectIndex = 0;
if (NestedPrefabs.HasItems())
{
const auto& data = *Data;
const Guid basePrefabId = JsonTools::GetGuid(data[objectIndex], "PrefabID");
if (const auto basePrefab = Content::Load<Prefab>(basePrefabId))
{
const Guid basePrefabRootId = basePrefab->GetRootObjectId();
for (int32 i = 0; i < ObjectsCount; i++)
{
const Guid prefabObjectId = JsonTools::GetGuid(data[i], "PrefabObjectID");
if (prefabObjectId == basePrefabRootId)
{
objectIndex = i;
break;
}
}
}
}
return ObjectsIds[objectIndex];
}
Actor* Prefab::GetDefaultInstance()
{
ScopeLock lock(Locker);