[Question] AIController not initialized in actors spawned from BP dynamically

Hi all,

I have a character class which uses a custom AI controller class. I have created a BP using my character class which adds a skeletal mesh actor to it. When I just drag and drop the BP into the editor and play my game the custom AI controller works fine. However when I spawn the BP from code the AI controller is NULL. I am not sure why is this happening.

In the constructor for my character class I define my AIController class

AIControllerClass = AStrategyAIController::StaticClass();

I am also doing some testing in the PostInitializeComponents function for my Character class

if(!Controller)
	{
		UHelperFunctions::drawDebugText("[AStrategyChar] Controller NULL before post!");
	}
	Super::PostInitializeComponents();
	if(!Controller)
	{
		UHelperFunctions::drawDebugText("[AStrategyChar] Controller NULL after post!");
	}

	UHelperFunctions::drawDebugText("Post Initializing");

The second print doesn’t happen for BPs spawned by dragging and dropping in the editor. However when spawned from C++ code both the prints happen.

I spawn my characters using the following code

spawnedActor = GetWorld()->SpawnActor(ElephantBP, loc + FVector(0,0,522), FRotator(0,0,0));
spawnedActor->SetActorRelativeScale3D(FVector(6,6,6));

I am not sure why this is happening. Any clues ?

Many thanks.

Cheers,
Shailen

To answer my question I wasn’t spawning the default controller. Now I do the following and it all works fine.

spawnedActor = GetWorld()->SpawnActor(ElephantBP, loc + FVector(0,0,522), FRotator(0,0,0));
			if(spawnedActor)
			{
				Cast(spawnedActor)->SpawnDefaultController();
			}

Although I don’t understand why doesn’t the Controller gets spawned itself since I have already defined the class to be used for spawning the default controller. Anyhow, the problem stated in the question is resolved now.

hey, see here: [Question][C++]AI Spawning - Programming & Scripting - Epic Developer Community Forums