Fix custom json asset usage for game settings if loaded before game scripting module is loaded
This commit is contained in:
@@ -284,6 +284,8 @@ void JsonAsset::unload(bool isReloading)
|
||||
bool JsonAsset::CreateInstance()
|
||||
{
|
||||
ScopeLock lock(Locker);
|
||||
if (Instance)
|
||||
return false;
|
||||
|
||||
// Try to scripting type for this data
|
||||
const StringAsANSI<> dataTypeNameAnsi(DataTypeName.Get(), DataTypeName.Length());
|
||||
|
||||
@@ -107,7 +107,7 @@ public:
|
||||
ScriptingTypeHandle InstanceType;
|
||||
|
||||
/// <summary>
|
||||
/// The deserialized unmanaged object instance (e.g. PhysicalMaterial).
|
||||
/// The deserialized unmanaged object instance (e.g. PhysicalMaterial). Might be null if asset was loaded before binary module with that asset was loaded (use GetInstance for this case).
|
||||
/// </summary>
|
||||
void* Instance;
|
||||
|
||||
@@ -118,6 +118,7 @@ public:
|
||||
template<typename T>
|
||||
T* GetInstance() const
|
||||
{
|
||||
const_cast<JsonAsset*>(this)->CreateInstance();
|
||||
return Instance && InstanceType.IsAssignableFrom(T::TypeInitializer) ? (T*)Instance : nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,9 +62,10 @@ public:
|
||||
Guid assetId = Guid::Empty; \
|
||||
gameSettings->CustomSettings.TryGet(TEXT(name), assetId); \
|
||||
const auto asset = Content::Load<JsonAsset>(assetId); \
|
||||
if (asset && asset->Instance && asset->InstanceType == type::TypeInitializer) \
|
||||
if (asset) \
|
||||
{ \
|
||||
result = static_cast<type*>(asset->Instance); \
|
||||
if (auto* instance = asset->GetInstance<type>()) \
|
||||
result = instance; \
|
||||
} \
|
||||
} \
|
||||
return result; \
|
||||
|
||||
Reference in New Issue
Block a user