[HELP] Tricky problem related to Spawned Actors, they disappear when reopening a level

Hi, I am currently working on a plugin which is supposed to spawn a number of characters in the scene.

I have plugin button which spawns actors when pressed. The actors have a skeleton choosed randomly in a range of skeletons from my Content browser. Here the problems start:

  • In order to have a different skeleton for each character, I use SpawnActorDeferred in the GenerateCharacters.cpp like this, in order to pass in parameter the skeleton I want before the construction of the actor:
AMyCharacter* myCharacter = world->SpawnActorDeferred(AMyCharacter::StaticClass(), spawnTransform);
myCharacter->Init(skeletalMeshPath[random_skeleton]);
UGameplayStatics::FinishSpawningActor(myCharacter, spawnTransform);
  • With Init function set up like this, in MyCharacter.cpp:
void AMyCharacter::Init(FString path)
{
skeletonPath = "SkeletalMesh'" + path + "'";
}
  • Then, OnConstruction script is called when FinishSpawningActor:
void AMyCharacter::OnConstruction(const FTransform& transform) {
Super::OnConstruction(transform);
USkeletalMesh* SK_Mesh = Cast(StaticLoadObject(USkeletalMesh::StaticClass(), NULL, *skeletonPath));
myMesh->SetSkeletalMesh(SK_Mesh);
myMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
}

This way, I can have a different skeleton for each characters. But, I have to initialize skeletonPath to null at first in order to compile.

So, here is the problem: when I spawn my characters it works well. But then, if I save my level, close UE and reopen it, my characters are still in the outliner but not in the scene because it seems that UE is initializing AMyCharacter class and see that skeletonPath is empty, so it couldn’t load my previously spawned characters with their corresponding skeletons.

My implementation is probably wrong somewhere, anybody see how I can conserve all the characters I spawned after I reopen the level just like they were before I close it ?

I hope it’s clear, thanks A LOT for your help on that !