Editor crash at spawn actor call c++

Hello.

I’ve been struggling with this bug for a few days now.
I’m just a begginer at programming have been following tutorials here and there.
I’m trying to spawn actors using the following code in cpp file (in BeginPlay, but have tried in Tick as well) :

if(this->GetWorld())
{
	if(ActorToSpawn != NULL)
	{
		this->GetWorld()->SpawnActor<AActor>(ActorToSpawn->GetClass(), GetActorLocation(), GetActorRotation());
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("ActorToSpawn pointer null!"));
	}
}
else
{
	UE_LOG(LogTemp, Error, TEXT("World pointer null!"));
}

and following code in .h file, in private section :

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spare part", meta = (AllowPrivateAccess = "true"))
TSubclassOf<AActor> ActorToSpawn;

Everytime I hit play, the window freezes and the editor crashes.
Since I use pointer checking, I guess I am not dereferencing null pointers.
I’ve had a look at similar issues, but without success so far.
Any idea of what is going on?

Thank you.

Thanks for the answer , but apparently, I can’t get the world from this actor :

LoginId:
EpicAccountId:

Assertion failed: world == nullptr [] [Line: 26] No world!

UE4Editor_Core
UE4Editor_Core
UE4Editor_MECATHERM_2114!ASpawner::BeginPlay() []
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

Any idea why?

Hey,

By doing GetClass, you are not really getting the class you want (you are getting blueprint generated class) To get the class that is referenced by the TSubclassOf, use Get() or simply dereference it. Ah, and there is really no need (rather discouraged) for using “this” keyword unless you need to use it.

Try doing something like this (I have used zero rotator/vector for test, but you can leave your transform):

    UWorld* const world = GetWorld();
	UClass* const actorClassToSpawn = ActorToSpawn.Get();
	if (world && actorClassToSpawn)
    {
		AActor* const spawned = world->SpawnActor<AActor>(actorClassToSpawn, FVector::ZeroVector, FRotator::ZeroRotator);
		if(spawned)
		{
			// yay
		}
    }

From the log I reckon you did this:

check(world == nullptr);

check ensures that the given condition is valid, in this case above, you want to “ensure” that the world is not valid, you would need to change it to

check(world);

, I made the changes you mentioned. Indeed, I had the check wrong.
I’m making progress because, I can spawn basic actors thanks to your recommandations.
However, when I want to spawn custom actors from class ASparePart, the editor crashes again.

][4]

Can you post the crash callstack? Going to be definitely easier to pinpoint the problem.

Here it is ,
link text

Can it be because of the scene component that is not used?

Hello,

I had a similar issue 30 minutes ago. After several trials & errors, i figured out that the fact i was specifying the name of the actor to span in my FActorSpawnParameters was triggering a deep engine crash at the second spawn only. I’m running Unreal 5.1.

FVector loc;
FRotator rot;
FActorSpawnParameters sinfo;
//sinfo.Name = "my UNIQUE object name";
sinfo.Owner = this;
sinfo.Instigator = this;
sinfo.bCreateActorPackage = false;
sinfo.bNoFail = true;
sinfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
scanner = GetWorld()->SpawnActorAbsolute<MyActor>(loc, rot, sinfo);

If i uncomment the line sinfo.Name = "photogrammetry scanner";, i automatically triggers a error in line 3534 of World.h:

template< class T >
T* SpawnActorAbsolute(UClass* Class, FTransform const& Transform,const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters())
{
	return CastChecked<T>(SpawnActorAbsolute(Class, Transform, SpawnParameters), ECastCheckedType::NullAllowed);
}

It seems that using the same name twice for an actor provokes automatically an engine crash…