Switch between Player Controlled Pawns and AI Controlled Pawns

Hi,

I need to be able to switch between multiple characters and have AI take control of any characters not currently being controlled by the player.
Currently I have multiple “Player Characters” placed in the level and use the number keys to switch between character possesion using the number keys, as explained here:

https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/PossessPawns/Blueprints/index.html

I am using Character Blueprints, though i’m a little confused with the difference between each tbh…

EDIT: From what I can tell I think i’m using the wrong type of Blueprint for characters and should just be using Pawns… The game I am looking to make is an UE4 version of the PS1 game “Team Buddies”. You pretty much use crates to “craft” characters, then fight other teams using those characters.

If anybody could thrown me into the right direction that’d be great. I’ve currently watched the “Introduction to Blueprints” and " Third Person Character Blueprints"
Thanks for your help :slight_smile:

2 Likes

AI can absolutely drive Character BPs. Source: it’s how I do enemies in my game.

What drives the Character is the CONTROLLER; by default your character is driven by a Player Controller, but it doesn’t have to be.

You can “Unposess” and “Posess” a Character at any time, the same way you would a Pawn; to have the AI take over a character you would just Unpossess your character, then Possess him with an AI Controller as the Target instead… Then you would Unpossess some AI-controlled actor, then Possess him with the Player Controller as the target.

Depending on the game, what I would do is, upon Spawning the “default” character (so, on the default character’s Event Begin Play), also spawn whatever secondary characters you need, by default using AI controllers. That way you can store all of the alternate characters as variables on your main character and easily access them when you want to Possess them.

2 Likes

Thanks for the awesome response, RhythmScript. I understand everything you’ve said, which is a good start :), now it’s time to actually get something working!

Ok, so i’m a little confused with this…

I currenly have this blueprint:

When I possess either of the 2 placed characters in my level, it no longer lets me press 1 to return control to the default character…
Also could somebody tell me or give me some hints to possessing the uncontrolled characters with AI.

I have a Basic AI controller made with the Behaivour Tree and Blackboard quickstart tutorial. I just can’t figure out how to have the AI Controller Posses the uncontrolled character

Cheers

First: remember that Input events are only handled by the actor which has a player controller. You’ll need to make sure you duplicate that switching logic on all controllable characters, otherwise switching off player 1 will leave you attached to a character that doesn’t do anything when you hit 2.

Second, do you see the “Target” pins that you have connected to the playercontroller? Just connect them to an AIcontroller reference instead.

Thanks again RhythmScript!

I’ve got the Ai possession working but pressing 1 still doesn’t return control to the default character…

  1. I press play and begin moving as my Default Character.
  2. After I press 2 or 3 my controller possesses either “MyCharacter2” or “MyCharacter”, pressing either 2 or 3 moves back and forth to the corresponding character fine.
  3. When I press 1 to return control to the original default character, nothing happens…

Is something stopping the Level Blueprint from referencing the “DefaultCharacter” variable as the initial player character at index 0?
Once again thanks for your help, it means a lot :).

I am boggled… I have done numerous AI tutorials but for some reason this just isn’t working, I can’t get my head around why it’s not working…

I have done the guard Ai tutorial here: Unreal Engine AI Tutorial #1 - AI Guard Part One - YouTube

My AI Controller, BlackBoard/Behavior Trees all work fine, if I set the “Auto Possess AI” on the “Character Blueprint” to “Placed in World”, then the character starts moving to the “Target location”.
Here’s a GIF showing the third character walking to it’s Target, and the character switch working:

I want to be able to Possess the Character with it’s AI Controller when I press 3, but it just doesn’t work…

Here’s the level blueprint I have which isn’t working…

Thanks

Your AI commend block does nothing. The controller which is already in Pawn 3 is possessing it again.

For anyone still trying to switch between player and AI controllers, all you need is the Spawn Default Controller node and as target put the character that you need to be possesed by the AI.Here is my setup and it works like a charm! In the character options set AutoPossessAI to “placed in world”

5 Likes

Thanks man! It worked for me! (looking for hours)

1 Like

It worked, but I think the World Outliner gets cluttered with AI controller after a lot of switches, is there a way to get rid of old AI controllers?

1 Like

For anyone else following along this thread, this is the link to the relevant docs: Possessing Pawns | Unreal Engine Documentation

They do it in the level blueprint, which makes more sense then having it in the player itself.

I have a question as well however. In the final version, they are using the SpawnDefaultController node. Wouldn’t that basically mean there are a bunch of default controllers laying around after you toggle between the two pawns a few times or am I misunderstanding something?

Thanks for the thread, it has been very helpful!

Just disturbing the dust on this super old post but this was exactly the solution I needed as my AI was not taking over after switching pawns and I needed them to follow the player after the switch.

Regarding the AI controllers, yes, it does keep creating them and cluttering up your outliner but I found if you add in a “Detach from Controller Pending Destroy” node before you call your first possess and assign it to the same character, it destroys the AI controller that was created last switch so yes, a new controller keeps being created each time but the old one is destroyed each time as well so there is only ever the one at a time.

Post was a huge help, thanks and keep up the great work yall!

1 Like