How to clone the LevelInstance in c++?

I would like to clone a LevelInstance which holds the bunch of models.


For instance , my outliner shows the list of LevelInstance files which are loaded on the current level. And I would like to clone the “BPP_CHA_Ref_Level01_N1”(PackLevelActor) to the World.

FActorSpawnParameters param;
param.Template = mPackedLevel;
GetWorld()->SpawnActor<AActor>(AActor::StaticClass(), FTransform3d(mRot, mPos, mScale), param);

This didn’t work…
Warning: SpawnActor failed because template class (BPP_CHA_Ref_N1_C) does not match spawn class (Actor)

I tried in another way.

FActorSpawnParameters param;
TArray< UActorComponent* > components;
mPackedLevel->GetPackedComponents(components);
AActor* actor = GetWorld()->SpawnActor<AActor>(AActor::StaticClass(), FTransform3d(mRot, mPos, mScale), param);
for (auto& c : components) {
	actor->AddComponentByClass(c->StaticClass(), true, FTransform3d(mRot, mPos, FVector(mScale)), true);
}

I’ve got a system-fault…

I really want to duplicate the models and place them.
Whoever know the answer-code, please show me.

Thanks in advance.

1 Like

I don’t know if it’s gonna work, but the error message should be an easy fix :

param.Template = mPackedLevel;
GetWorld()->SpawnActor<AActor>(mPackedLevel->GetClass(), FTransform3d(mRot, mPos, mScale), param);

Hello!

I tried your code, but still I got same warning, and the LevelInstance wasn’t cloned.

LogSpawn: Warning: SpawnActor failed because template class (BPP_CHA_Ref_Level01_N1_C) does not match spawn class (PackedLevelActor)

Anyway, thanks for reply. I’m appreciate with you kindness.
So sorry for late reply!! I couldn’t upload my reply, because of server error 500 from UE site.

I’m ashamed , I’ve mis-typed your code…
Your code works alright!!
The error has been gone. Thank’s a lot!!!

But, new warning comes out.
LogLevel: Warning: Failed to find streaming level object associated with ‘None’

Thit might be a new issue which I have to confront with.
It seems to be StreamingLevel problem.

Thank you!

My final answer for cloning the LevelInstance.

UWorld* data = LoadObject<UWorld>(nullptr, TEXT("/Game/ path for LevelInstance "), nullptr, LOAD_None, nullptr);
ALevelInstance* inst = GetWorld()->SpawnActor<ALevelInstance>(ALevelInstance::StaticClass(), FVector(100,0,0), FRotator(0,0,0));
inst->SetWorldAsset(data);
inst->LoadLevelInstance();

You can cache the UWorld-data into some variables.

static TMap<ID,UWorld*> gCache ;

like this. ID could be hashcode of filename.

So, only you have to do is…

  1. Spawn the ALevelInstance at certain place
  2. Set the asset-data by SetWorldAsset()
  3. Call LoadLevelInstance()