Can Actors spawn Actors? (crash)

I’m trying to spawn an actor in my c++ code, but it seems to automatically get deleted. Furthermore, if I run the spawning event twice, the whole editor crashes. The code I use for spawning is the following:

void AMyPawn::Shoot()
{
	UWorld* World = GetWorld();
	if (World != nullptr)
	{
		FVector Location = GetActorLocation();
		FRotator Rotation = GetActorRotation();
		FActorSpawnParameters Params;
		Params.Owner = this;
		Params.Instigator = Instigator;
		Params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		Params.Name = TEXT("SPAWNEDDD");
		World->SpawnActor<AFloatingActor>(Location, Rotation, Params);
	}
}

Which seems to be what one should do after reading other questions and resources on the web. FloatingActor’s constructor:

AFloatingActor::AFloatingActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	UE_LOG(LogTemp, Warning, TEXT("Spawned an actor"));
	FloatingMagnitude = 20.f;
	InitialLifeSpan = 100.f;
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Salamander"));
}

The one thing I see that is different from some tutorials is that they tell you to spawn actors from the GameMode. Is that the problem? Why, and if not, how do I get the actor to spawn correctly? As said, it shows up in the world outliner as “deleted actor” but the debug messages from the constructor do get printed.

Edit 2: I’ve discovered it does not crash, and indeed seems to work fine if I omit the spawn parameters when calling spawnActor, i.e.:

World->SpawnActor<AFloatingActor>(Location, Rotation);

Edit: Here’s the callstack that shows up after the crash. As said, it only crashes if I spawn the actor twice.

Callstack.txt

Hello Ljaniuk,

This should work correctly from what you’ve posted. I’d suggest running the console command “slomo 0.1” or even lower to slow down the time before you spawn the actor. This should allow you to see if the actor is being spawned and actually just flying off somewhere due to spawning inside of another collision. It could be hitting the Kill Z which would cause it to become a deleted actor.

Also, could you post the callstack of the crash you’re getting when you try to spawn it a second time?

Sure, added it. Also added another tidbit I discovered, it does seem to work if I don’t add the spawn parameters.

I also have to work out how to make the actor have a staticmesh when spawned (until now I have added it as a component in the editor, but can’t do that with an actor not yet spawned) so it shows up in the level. I know I can use the asset finder (as in the twin stick shooter example) in its constructor but I’d prefer not to have to change code when deciding for a different mesh. Does that mean I should add a UStaticMeshComponent* as a uproperty to the floatingActor, create a blueprint version of the class, and set the defualt mesh there? This is my current plan.

I believe the reason it didn’t work when you had the SpawnParams set was due to the setting you used for the SpawnCollisionHandlingOverride to ESpawnActorCollisionHandlingMethod::AlwaysSpawn. The default may be that it will not spawn on top of it but offset itself from any existing collision.

You’re correct, the best way to do that would be to make it into a UPROPERTY so that you can set it from inside of the editor.

In that case, what situation should the setting be used in? I used it because spawning despite collision was the behavior I wanted, why should that cause a crash? Nevertheless, this answers my question, so you can add it as an answer if you like.

I would imagine something else is causing the crash, such as the result of using that (the actor going bonkers and getting destroyed) which causes the crash instead.