I have used Actor Components to share some checks and functionality between my Player Character and AI Characters. For example this was used to check if a melee attack is possible:
Now I’m trying to migrate all Actor Components like this into modular Behavior Trees. I have had some success in using the tree below with an AI Character:
But I’m not sure how can I share it now with my Player Character? With components, all I needed to do was to add it to a Character, maybe also hook it up to the Controller, and it worked.
Is there some similar mechanism to Behavior Trees but for Player Characters? Or maybe my Player Character should actually be a simpler AI that somehow gets input? I don’t want to duplicate logic in both Actor Components and Behavior Trees…
Another idea I had, would be to keep all checks in Actor Components and call them through Behavior Tree Tasks, but then I would lose the flexibility of decorators.
Why are you moving the logic out of the actor components and into behavior trees?
You can keep all the functionality in components and ahve your AI communicate with the components just like the player does.
I think the reasoning makes sense. In RTS games, for example, there are many characters that are both AI and Player Controlled (if currently selected). So there must be a way to do this.
In common RTS you don’t really have any pawn, you command concepts like squads to which characters respond. Might be possible to just spawn an AI Controller from code and run a brain component on it without it controlling a pawn… but that needs some testing and I’m not convinced yet XD, I use c++ whenever I can and try to avoid these visual state machines.
What was missing from the instructions in the thread:
Your “Semi Player Controller” should, in the Event On Possess method:
a) spawn a “Semi AI Controller” and hold it as a variable
b) spawn the Character and possess it with the “Semi AI Controller”
c) switch camera to the one in Character, by using Set View Target with Blend
Create events to receive input in the “Semi AI Controller”. Then invoke them when you receive actual input from “Semi Player Controller”.
Camera will break… So far the only way to fix it I found is to:
a) in “Semi Player Controller”: Auto Manage Active Camera Target = OFF
b) in “Semi AI Controller”: Set Control Rotation from Pawn = OFF, and on tick (yeah…) you need to Set Control Rotation to match the Camera (not camera boom!) rotation
c) in Character: Inherit Pitch/Yaw/Roll in Spring Arm (camera boom) = OFF, Component Replicates = ON
d) when you want to rotate you need to rotate the camera boom instead of using the controller methods (because the pitch method will NOT work in AIController, see unreal engine4 - UE4 How to set pitch in controlRotation for AIController - Stack Overflow)
I think that was all. Then you can run a behavior tree and pass your input as a bool, which you immediately have set back to false in a behavior tree task before performing an action, otherwise it will just loop. Passing axis values (movement, camera) to a behavior tree seems impossible, because the parallelism is very limited and one action would block the other.
I’m going to keep building on top of that and see if there are any issues down the line, but so far it works (even in multiplayer).