Fix crash when reloading a saved map generator

This commit is contained in:
nsubiron 2017-03-15 14:11:48 +01:00
parent 004e2cc4c7
commit 734f0f0534
2 changed files with 25 additions and 13 deletions

View File

@ -38,12 +38,7 @@ void ACityMapMeshHolder::OnConstruction(const FTransform &Transform)
for (tag_size_t i = 0u; i < NUMBER_OF_TAGS; ++i) {
// Create an instantiator for each mesh.
auto instantiator = NewObject<UInstancedStaticMeshComponent>(this);
instantiator->SetMobility(EComponentMobility::Static);
instantiator->SetupAttachment(SceneRootComponent);
instantiator->SetStaticMesh(GetStaticMesh(CityMapMeshTag::FromUInt(i)));
MeshInstatiators.Add(instantiator);
instantiator->RegisterComponent();
GetInstantiator(CityMapMeshTag::FromUInt(i));
}
UpdateMapScale();
}
@ -98,9 +93,8 @@ void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, uint32 X, uint32 Y, fl
void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, FTransform Transform)
{
auto instantiator = MeshInstatiators[CityMapMeshTag::ToUInt(Tag)];
check(instantiator != nullptr);
instantiator->AddInstance(Transform);
auto &instantiator = GetInstantiator(Tag);
instantiator.AddInstance(Transform);
}
// =============================================================================
@ -110,10 +104,9 @@ void ACityMapMeshHolder::AddInstance(ECityMapMeshTag Tag, FTransform Transform)
void ACityMapMeshHolder::ResetInstantiators()
{
for (tag_size_t i = 0u; i < NUMBER_OF_TAGS; ++i) {
UInstancedStaticMeshComponent *instantiator = MeshInstatiators[i];
check(instantiator != nullptr);
instantiator->ClearInstances();
instantiator->SetStaticMesh(GetStaticMesh(CityMapMeshTag::FromUInt(i)));
auto &instantiator = GetInstantiator(CityMapMeshTag::FromUInt(i));
instantiator.ClearInstances();
instantiator.SetStaticMesh(GetStaticMesh(CityMapMeshTag::FromUInt(i)));
}
}
@ -140,3 +133,19 @@ void ACityMapMeshHolder::UpdateMapScale()
MapScale = size.X;
}
}
UInstancedStaticMeshComponent &ACityMapMeshHolder::GetInstantiator(ECityMapMeshTag Tag)
{
UInstancedStaticMeshComponent *instantiator = MeshInstatiators[CityMapMeshTag::ToUInt(Tag)];
if (instantiator == nullptr) {
// Create and register an instantiator.
instantiator = NewObject<UInstancedStaticMeshComponent>(this);
instantiator->SetMobility(EComponentMobility::Static);
instantiator->SetupAttachment(SceneRootComponent);
instantiator->SetStaticMesh(GetStaticMesh(Tag));
MeshInstatiators[CityMapMeshTag::ToUInt(Tag)] = instantiator;
instantiator->RegisterComponent();
}
check(instantiator != nullptr);
return *instantiator;
}

View File

@ -80,6 +80,9 @@ private:
/// Set the scale to the dimensions of the base mesh.
void UpdateMapScale();
/// Creates a new one if necessary.
UInstancedStaticMeshComponent &GetInstantiator(ECityMapMeshTag Tag);
UPROPERTY()
USceneComponent *SceneRootComponent;