How to get AIController from pawn

Hello. I spawned a character. Now I want to use MoveToLocation node and it requires AIController. But I can’t see any way to get it from my character.

You want to do that for AI character or Player character, because player character has PlayerController insted

All you need to do is to GetController from your pawn and then cast it to AIController. If casting results in None then it means it’s a player controlled pawn.

Cheers,

–mieszko

Hello mieszko,

It seems that I’m doing something wrong. I have an actor in library which extends Character. I spawn it in runtime with SpawnActor. And when I’m trying to get it’s controller - it is null.

I set AIController Class in defaults for spawned actor as AIController class. No success.
I set AIController class in runtime. Still no success. Here how I’m doing this:

This code is in main PlayerController. The last node - PrintString shows true - that means that controller is null.

SpawAIFromClass requires BehaviorTree. I don’t have one and for my task I don’t need it. And spawning an actor this way does nothing - bot not spawning.

And why does setting Controller at runtime have no effect?

Try setting AIControllerClass in your extended character and let me know if that fixed it.

Setting AIControllerClass for a pawn at runtime does nothing, but setting it in Defaults should work. I’ll investigate it.

An alternative (and preferred as a matter of fact) way of spawning AI is using SpawAIFromClass function which allows you to indicate both pawn and controller class. Give it a try and let me know how it went.

And why does setting Controller at runtime have no effect?

AIControllerClass is the default class a pawn will spawn for himself upon creation. When you set it for an already spawned pawn it’s past the point where that’s used.

What UE4 version are you using? Some of the things you describe sound like bugs that have been already fixed.

Thank you for explanation.
Today I updated to 4.2 version.

I’ve looked at the code and here’s how it works:

  • AIControllerClass is used as expected for pawns placed in pawns in editor mode and present and level start.
  • It’s also used for pawns spawned with SpawnAIFromClass, so if you set your controller in your pawn’s defaults and spawn it with SpawnAIFromClass it will get applied
  • SpawnAIFromClass woks just fine without passing BT asset as a parameter. It’s optional.

For some basic AI inspiration look at Sandbox/AIExampleBP project.

Thank you for help. Everything clear now. Conclusion:

  • It’s possible to use SpawnAIFromClass for spawning a bot and this bot will have controller specified in defaults (I can’t say why this doesn’t work before - perhaps some bug in my code).
  • Using SpawnActor spawns an actor but without any controller.
  • It’s possible to spawn a controller with SpawnActor and possess an actor with this controller. So you’ll end up with necessary pawn with necessary controller.

Mieszko, what is Sandbox/AIExampleBP? Where can I find it?

I was about to tell you to look in Samples/Sandbox folder of your UE4 installation, but now I’m not sure it shipped with 4.2 :confused: I’ll see if that was the case and get back to you.

Thanks a lot, I was going crazy with this one !