How to have a character uncontrolled and possess by an AI controller?

My game has a concept that there are two characters both can be possessed by a player controller, and in game you can have a character controlled and the other using an AI controller. And I can swap to control another character, and make the character uncontrolled to be possessed by an AI controller.
Both of the characters have an AI controller class In the class default values. It works ok in the beginning where it successfully let me control one character, and the other character running behavior tree. But I find out that if I use function “possess” to control the other character, I can control the other character, but the other character just stand still and cannot run the behavior tree. Then I swap again, and the character previously running AI controller is just standing still.
I wonder is there a way to let a character possessed by a player controller later possessed by an AI controller?

1 Like

OK I have (probably?) solved the problem. It is simple, just save your ai controller reference on character begin play, and when you need to use ai, call the possess node of the ai controller and run behaviour tree.
My solution is as follows:
1)Make sure your characters in map don’t have auto possess player 0!
I write a possess function in the player controller, just get all actors in class and run possess node.

2)In Character’s event begin play, use get AI controller node and save a reference of it.
If you didn’t close auto possess, you won’t get an ai controller from the node!
(The comment is the original node name. Never mind me I’m lazy )
1

3)Use possess and run behavior tree nodes when you want the character possessed by ai controller
You don’t need to unpossess the player controller. Just use ai controller reference to possess and run your behavior tree.
(The comment is the original node name. Never mind me I’m lazy )

Hope this one helps… If I have further questions I will update the post.

2 Likes

I would actually put the swapping logic in the Game Mode or some other entity above the controllers.

You want to avoid putting your controller logic on something that would be considered ‘below’ your controllers (your controllers control the the characters, not the other way around). And because you want something to control what your controllers do, you put it in an entity you could consider ‘above’ your controllers, so the GameMode is a good place for that logic.

Have references to your characters and your controllers in your Game Mode, and the logic to UnPossess and Possess them.

GameMode Graph

In your character and controllers, when they are created have them tell the Game Mode what they are for reference for the Game Mode.

Controller Graph

Character Graph

In your AIController you have have an OnPossess event that runs the BehaviourTree.

AIController Graph

I made everything events so you can clearly see what is happening, but you could turn much of that into functions.

6 Likes