As a solution for Lecture 112: Enemy tank dances and flies away after Start is clicked , I’m spawning my AI tank in the BeginPlay of the level blueprint. Thus, in the Tank_BP details, I set “Auto possess AI” to “Placed in World or Spawned”. But it doesn’t work, the spawned tank gets no TankAIController, even not if I set “Auto possess AI” to “Spawned”.
If I place a tank istead of spawning it, then everything works fine. However, I cannot place it due to the “physics gone wild bug” and real games will also have many spawned pawns, so there must be a way to make this work…
Okay, it seems like BeginPlay() is just the wrong place/moment to log this.
As can be seen from the following log, the following happens:
1.) AIController_0 was set for Tank_BP_2522 (a placed in world tank)
2.) AIController_1 was set for Tank_BP_C_0 (the player tank)
3.) In BeginPlay(), AIController_0 reports controlling Tank_BP_2522
4.) In BeginPlay(), AIController_1 reports not controlling any tank. => Thus, between step2 and step4, AIController_1 was removed from Tank_BP_C_0. This seems logic, as it is the player tank, which should get a PlayerController, which is correctly reported in the next step.
5.) In BeginPlay() of the PlayerController, it reports controlling Tank_BP_C_0 (the one that was formerly controlled by AIController_1
6.) In BeginPlay(), AIController_2 reports not controlling any tank
7.) AIController_2 is now controlling Tank_BP_C_1 (the spawned one)
Seems like a resource leak to me, because AIController_1 was instantiated, but will never be used. I think it might be better not to assign the controller via blueprint settings, but to instantiate them manually in C++.
LogWorld: Bringing World /Game/Levels/UEDPIE_0_BattleGround.BattleGround up for play (max tick rate 0) at 2017.06.18-14.44.33
LogTemp:Warning: Tank_BP_2522 after Super::PostInitializeComponents(): controller is TankAIController_0
LogWorld: Bringing up level for play took: 0.002983
LogTemp:Warning: Tank_BP_C_0 after Super::PostInitializeComponents(): controller is TankAIController_1
LogTemp:Warning: TankAIController_0 BeginPlay
LogTemp:Warning: TankAIController_0: Controlled tank is Tank_BP_2522
LogTemp:Warning: TankAIController_1 BeginPlay
LogTemp:Error: TankAIController_1 does not control any tank
LogTemp:Warning: TankPlayerController_BP_C_0 BeginPlay
LogTemp:Warning: TankPlayerController_BP_C_0: Controlled tank is Tank_BP_C_0
LogTemp:Warning: TankAIController_2 BeginPlay
LogTemp:Error: TankAIController_2 does not control any tank
LogTemp:Warning: Tank_BP_C_1 after Super::PostInitializeComponents(): controller is TankAIController_2
PIE: Info Play in editor start time for /Game/Levels/UEDPIE_0_BattleGround -0.734
Hi, I fixed this issue by disabling the auto possess ai in the tank_BP under class defaults tab and set AI Controller Class to TankAIController as the default class.
Create a function called FindAiController() in TankAIController code or with a similar name. This function will be called by the blueprint after we spawn in the tanks and after we create the default controllers for each. Since we disabled the auto possess ai in the tank_BP, calling GetControlledTank() in BeginPlay() would not work as the tanks don’t start with controllers attached when spawned, instead GetControlledTank() will return NULL. NULL->GetName() crashes the engine.
In the BattleGround level blueprint, spawn each enemy tank, spawn their default controller, and find their default controller by calling the function made in the c++ code. The variables enemyStart are empty actors that are placed into the map for their spawn location.
First Half
Second Half
The end result should give two AI tank controllers attached to two enemy tanks given by the console log.
Thanks for your fix. However for me Auto Posses AI had to be enabled, otherwise it wouldn’t work.