Respawn actors

Hello! I am simply do three things. The first one is

	FActorSpawnParameters spawnParams;
	spawnParams.Name = "text1";
	created = GetWorld()->SpawnActor<AActor>(actorClass, spawnParams);

and after that goes the second (with delay of several tens of frames)

	created->Destroy();
	created = nullptr;

and lastly I try to repeat again (with delay of several tens of frames)

	FActorSpawnParameters spawnParams;
	spawnParams.Name = "text1";
	created = GetWorld()->SpawnActor<AActor>(actorClass, spawnParams);

Now I get exception. It seems that although actor is destroyed, its name is still in HashTables. Should I do smth more to destroy actor properly in 4.26? The main interest is recreating actor but keep same name for soft pointers to stay valid (custom save/load sys).

In this case, all profit of usage TSoftObjectPtrs with Level Actors are lost.

Is there a reason you need them to have the same name? I’ve never passed a name in and have always let the engine name them.

You could add your own custom property to store a name in & set it using spawn deferred?

I’m not sure what you mean. Could you explain how you’re using TSoftObjectPtrs?

Let say you have on your level actor A, and actor B. Actor B has soft reference to actor A. So you can easily save this soft reference value to Buffer and load it after some time from this Buffer. Suppose that you save game and after that you load it. Soft reference will get some path from Buffer and after that you can ask soft reference to get Actor by path, if there is one. However, if you cant destroy and respawn actor with same name, then soft reference can’t use their functionality on that actors… I know that TSoftObjectPtrs are used widely with assets (not actors), but they can also be used with actors in the way I say. Perhaps there is another approach to deal with Actors in the way I describe?

Yeah I think TSoftObjectPtrs are mainly used as a way to reference assets without loading them (so you don’t have to load everything at once).

The problem here is that when you delete A, and then spawn a new actor, it is not the same actor. A few ways I can think of to do what you need:

  • Change TSoftObjectPtr to A in B to AActor* pointer. Move the logic for spawning A in Actor B so that B get a reference to the new A actor.
  • Put the Destroying + Spawning logic in a separate actor C that never gets destroyed. Give B a reference to C and make C keep a reference of the current A

Does that make any sense? (I haven’t had enough coffee this morning yet :D)

Thanx for reply and for idea. I will think about steps you described and about another plan - to avoid destroy->respawn and keep existing actor and refresh only some internal data in it.

No worries. That sounds look a good plan!