Crash when spawn actor

I have c++ function for spawn actor, when i compiled i had no error, but when i try to use it in blueprinti so i have crash. What i do wrong ?

void UMyBlueprintFunctionLibrary::Spawn(class UClass* clas  )
{
	 FActorSpawnParameters param;
	param.Name = "1";
	FVector loc = FVector(0, 0, 0);
	FRotator rot = FRotator(0, 0, 0);
	UWorld* const World = GEngine->GetWorld();
	World->SpawnActor(clas,&loc,&rot,param);
	

}

Crash report

UE4Editor_Engine
UE4Editor_Engine
UE4Editor_MyProject_7534!UMyBlueprintFunctionLibrary::Spawn() [d:\sasha\myprojectszxc\myproject\source\myproject\myblueprintfunctionlibrary.cpp:29]
UE4Editor_MyProject_7534!UMyBlueprintFunctionLibrary::execSpawn() [d:\sasha\myprojectszxc\myproject\source\myproject\myblueprintfunctionlibrary.h:17]
UE4Editor_CoreUObject!UFunction::Invoke() [e:\ue4git\unrealengine\engine\source\runtime\coreuobject\private\uobject\class.cpp:4198]
UE4Editor_CoreUObject!UObject::CallFunction() [e:\ue4git\unrealengine\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:484]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [e:\ue4git\unrealengine\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1755]
UE4Editor_CoreUObject!UObject::ProcessInternal() [e:\ue4git\unrealengine\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:698]
UE4Editor_CoreUObject!UFunction::Invoke() [e:\ue4git\unrealengine\engine\source\runtime\coreuobject\private\uobject\class.cpp:4198]
UE4Editor_CoreUObject!UObject::ProcessEvent() [e:\ue4git\unrealengine\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1050]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd!UEditorEngine::CreatePIEGameInstance() [e:\ue4git\unrealengine\engine\source\editor\unrealed\private\playlevel.cpp:3012]
UE4Editor_UnrealEd!UEditorEngine::PlayInEditor() [e:\ue4git\unrealengine\engine\source\editor\unrealed\private\playlevel.cpp:2259]
UE4Editor_UnrealEd!UEditorEngine::StartQueuedPlayMapRequest() [e:\ue4git\unrealengine\engine\source\editor\unrealed\private\playlevel.cpp:1048]
UE4Editor_UnrealEd!UEditorEngine::Tick() [e:\ue4git\unrealengine\engine\source\editor\unrealed\private\editorengine.cpp:1244]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [e:\ue4git\unrealengine\engine\source\editor\unrealed\private\unrealedengine.cpp:361]
UE4Editor!FEngineLoop::Tick() [e:\ue4git\unrealengine\engine\source\runtime\launch\private\launchengineloop.cpp:2427]
UE4Editor!GuardedMain() [e:\ue4git\unrealengine\engine\source\runtime\launch\private\launch.cpp:142]
UE4Editor!GuardedMainWrapper() [e:\ue4git\unrealengine\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [e:\ue4git\unrealengine\engine\source\runtime\launch\private\windows\launchwindows.cpp:200]

Hello,

Could you provide the full code from your .h and .cpp files that you are using?

This could potentially be a naming conflict, because each time you are spawning the actor, you are giving it the name “1”. Try commenting this line out, and see if you get the same result.

param.Name = "1";

No its not help. I replaced my code but also crash

MyActor.h

 virtual void BeginPlay() override;


 virtual void Tick(float DeltaSeconds) override;
 
 UWorld* gtW(AActor* actor);


 UFUNCTION(BlueprintCallable, Category = "MyActor")
     static void SpawndA(class UClass* clas, class AActor* actor);

};
MyActor.cpp

UWorld* AMyActor::gtW(AActor* actor)
 {
 
     
     UWorld* const World = actor->GetWorld();
     if (World)
         return World;
     else
     {
 
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "World - false");
         return NULL;
     }
 }
   
  void AMyActor::SpawndA(class UClass* clas, class AActor* actor)
 {
      AMyActor a;
      FActorSpawnParameters param;
      FVector loc = FVector(0, 0, 0);
      FRotator rot = FRotator(0, 0, 0);
      if (clas == NULL)
          GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "clas - false");
      if (a.gtW(actor) != NULL)
      a.gtW(actor)->SpawnActor(clas, &loc, &rot, param);
 }

UE4Editor_Engine UE4Editor_Engine UE4Editor_MyProject_7534!UMyBlueprintFunctionLibrary::Spawn() [d:\sasha\myprojectszxc\myproject\source\myproject\myblueprintfunctionlibrary.cpp:29]

This is the line that caused the crash. Most likely because of dereferencing a NULL pointer.

Hello,

I was able to solve your issue by making a few changes to the code.

UWorld* AMyActor::gtW(AActor* actor)
{
	//Check if the actor is valid
	if (actor)
	{
		UWorld* const World = actor->GetWorld();
		if (World)
		{
			return World;
		}
		else
		{

			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "World - false");
			return NULL;
		}
	}
	//If the actor is not valid, return NULL
	else
	{
		return NULL;
	}
}

void AMyActor::SpawndA(class UClass* clas, class AActor* actor)
{
	FActorSpawnParameters param;
	FVector loc = FVector(0, 0, 0);
	FRotator rot = FRotator(0, 0, 0);
	if (clas == NULL)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "clas - false");
	if (gtW(actor) != NULL)
		gtW(actor)->SpawnActor(clas, &loc, &rot, param);
}

So the first thing I did was remove your AMyActor variable from the SpawndA function, as it was unnecessary and was never being initialized. After this, I took the a.gtW out, and just called gtW(actor), as it would use the MyActor class to call GetWorld().
Finally, I implemented a check to see if the actor being passed in was valid, because if it wasn’t then GetWorld would always fail.

Feel free to let me know if there are any further questions.

Have a great day

1 Like