# How to get AIController from pawn

**URL:** <https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959>\
**Category:** Blueprint\
**Tags:** aicontroller, unreal-engine, Blueprint, question\
**Created:** [June 4, 2014, 2:40pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959 "2014-06-04T14:40:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_69853c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69853c21](https://forums.unrealengine.com/u/anonymous_user_69853c21)\
**Post date:** [June 4, 2014, 2:40pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/1 "2014-06-04T14:40:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [June 4, 2014, 2:56pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/2 "2014-06-04T14:56:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MieszkoZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MieszkoZ](https://forums.unrealengine.com/u/MieszkoZ)\
**Post date:** [June 4, 2014, 4:55pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/3 "2014-06-04T16:55:50Z")

</div>

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,

–

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_69853c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69853c21](https://forums.unrealengine.com/u/anonymous_user_69853c21)\
**Post date:** [June 5, 2014, 8:05am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/4 "2014-06-05T08:05:02Z")

</div>

Hello ,

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.

---

<div class="post-metadata">

**Author:** ![MieszkoZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MieszkoZ](https://forums.unrealengine.com/u/MieszkoZ)\
**Post date:** [June 5, 2014, 8:16am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/5 "2014-06-05T08:16:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_69853c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69853c21](https://forums.unrealengine.com/u/anonymous_user_69853c21)\
**Post date:** [June 5, 2014, 8:41am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/6 "2014-06-05T08:41:06Z")

</div>

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:

 ![7795-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/a/0/7a0b6002938c122f3ad3ab4a5a38d6d5b51581db.png)

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

---

<div class="post-metadata">

**Author:** ![MieszkoZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MieszkoZ](https://forums.unrealengine.com/u/MieszkoZ)\
**Post date:** [June 5, 2014, 8:58am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/7 "2014-06-05T08:58:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_69853c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69853c21](https://forums.unrealengine.com/u/anonymous_user_69853c21)\
**Post date:** [June 5, 2014, 9:09am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/8 "2014-06-05T09:09:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![MieszkoZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MieszkoZ](https://forums.unrealengine.com/u/MieszkoZ)\
**Post date:** [June 5, 2014, 9:25am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/9 "2014-06-05T09:25:17Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_69853c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69853c21](https://forums.unrealengine.com/u/anonymous_user_69853c21)\
**Post date:** [June 5, 2014, 9:30am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/10 "2014-06-05T09:30:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MieszkoZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MieszkoZ](https://forums.unrealengine.com/u/MieszkoZ)\
**Post date:** [June 5, 2014, 1:50pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/11 "2014-06-05T13:50:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_69853c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69853c21](https://forums.unrealengine.com/u/anonymous_user_69853c21)\
**Post date:** [June 5, 2014, 2:25pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/12 "2014-06-05T14:25:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_69853c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69853c21](https://forums.unrealengine.com/u/anonymous_user_69853c21)\
**Post date:** [June 5, 2014, 3:11pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/13 "2014-06-05T15:11:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MieszkoZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MieszkoZ](https://forums.unrealengine.com/u/MieszkoZ)\
**Post date:** [June 6, 2014, 9:33am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/14 "2014-06-06T09:33:44Z")

</div>

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 😕 I’ll see if that was the case and get back to you.

---

<div class="post-metadata">

**Author:** ![tngri](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tngri/32/954151_2.png) [@tngri](https://forums.unrealengine.com/u/tngri)\
**Post date:** [August 8, 2019, 7:35am UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/15 "2019-08-08T07:35:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ExpectedYou](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/expectedyou/32/557745_2.png) [@ExpectedYou](https://forums.unrealengine.com/u/ExpectedYou)\
**Post date:** [July 1, 2022, 3:28pm UTC](https://forums.unrealengine.com/t/how-to-get-aicontroller-from-pawn/284959/16 "2022-07-01T15:28:48Z")

</div>

How did you spawn in a controller with the spawnactor? What nodes did you use?
