Skip to content

Commit

Permalink
Preserve asset ids for assets that fail to load, when deserializing f…
Browse files Browse the repository at this point in the history
…rom json (#847)

Sometimes deserializing a Json document happens when asset handlers are not
registered. In that case, `FindOrCreateAsset` will fail to create the
asset, since there's no handler registered to create it. When this happens,
`FindOrCreateAsset` returns an Asset instance with a null asset id. This
effectively causes the json deserializer to lose that data, even in
situations where the the actual asset data doesn't need to be loaded, but
the asset id needs to be preserved.
  • Loading branch information
burelc-amzn committed May 21, 2021
1 parent 6cd8661 commit d8bd6ef
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Code/Framework/AzCore/AzCore/Asset/AssetJsonSerializer.cpp
Expand Up @@ -133,7 +133,15 @@ namespace AZ
if (!id.m_guid.IsNull())
{
*instance = AssetManager::Instance().FindOrCreateAsset(id, instance->GetType(), instance->GetAutoLoadBehavior());

if (!instance->GetId().IsValid())
{
// If the asset failed to be created, FindOrCreateAsset returns an asset instance with a null
// id. To preserve the asset id in the source json, reset the asset to an empty one, but with
// the right id.
const auto loadBehavior = instance->GetAutoLoadBehavior();
*instance = Asset<AssetData>(id, instance->GetType());
instance->SetAutoLoadBehavior(loadBehavior);
}

result.Combine(context.Report(result, "Successfully created Asset<T> with id."));
}
Expand Down

0 comments on commit d8bd6ef

Please sign in to comment.