Help with behavior trees and spawning enemies?

I am trying to make the enemies in my game walk between a set of locations from an array when the blackboard booleans YellowAlert and CanSeePlayer are false, run to the player’s last observed location when YellowAlert is true and CanSeePlayer is false, and attack the player when CanSeePlayer is true regardless of the value of YellowAlert. I also want to spawn enemies at random locations from the same array of locations that they walk between.

I know how to do most of this, but I don’t see any blackboard decorator that goes off of two booleans, don’t know how to run a task on a timer in the behavior tree, and couldn’t find any functions for moving at a certain speed and spawning actors into levels at specified locations.

Any help would be greatly appreciated.

Hi,

The blackboard decorator only accepts one boolean, however, you can easily put two decorators on the same Selector or Sequence node. You can also use one boolean variable that stores the result of AND operation between your other two booleans (though I don’t recommend this as it might make things messy).

To run a Task with a timer at a different rate you have 2 options: Using Services or setting a timer in the Task itself. Services work similar to Tasks. They start executing if your decorators are all true and they provide you with an option to change the ticking interval once you add them to your behavior tree. You can override their Event Receive Tick and implement the logic you want.

Tasks, on the other hand, don’t give you an option to change their ticking interval, however, there are easy ways to make them tick in the interval you want. Instead of using Receive Tick event, you would use Receive Execute and bind your functionalities to a custom timer. Receive Execute works just like Begin Play and is called once when the Task gets activated. As a good practice, I recommend that you store this timer handle and call Clear and Invalidate on it once your task is finished executing by either using the Receive Abort event or another delegate that you have set somewhere else.

This official tutorial here might help you understand how to spawn actors using blueprints.

If your AI’s are based on Character, then you can use the inherited Movement component to change their Max Speed. if you’re inside your own character BP, simply drag the Movement component from the Components list to get a reference to it, click and drag from that reference and type “max speed”. You’ll be able to set it and change it to your liking even during gameplay and runtime.

Hope this helps.

Hmm… Make sure that when you spawn your enemies you specify the controller class that they should use. In the AIController, you can try overriding On Posses event and run the Behavior Tree through that. If none works, please post a screenshot of your AIController and the Behavior Tree so that I can better assist you.

Don’t make it overcomplicated and don’t try to activate it in your Pawn’s constructor (It might not work if the pawn is still being constructed). Remove all of these and simply use BeginPlay or OnPosses in the AIController to run the behavior tree. But first, make sure that these events fire by printing a debug message. If they do, then check the behavior tree itself. It might be failing at the first decorator (if you have any) and hence not executing at all! If the issue still persists then please post a picture of your behavior tree itself along with the updated AIController blueprint.

I was able to spawn everything in as intended, but the enemies’ behavior tree says “Inactive” while playing in the editor and they just stand there. I thought the AI Controller wasn’t executing its begin play which runs the behavior tree, so I made a function to begin it and called it within the controlled pawn’s begin play, construction script, and event tick, none of which made the behavior tree actually do something. The print statements attached as the first nodes did execute. When I place enemies into the level normally instead of through the “SpawnActorFromClass” function they work correctly. What could be the problem?

I didn’t see an input on the SpawnActorFromClass to specify the AI Controller, but I did double check in the pawn’s blueprint

that the correct controller is being used. Neither on posses nor begin play fires. Nothing prints. I unhooked my unsuccessful bypass in the pawn’s script so it doesn’t do anything on construction. Attached are pictures of the behavior tree and AI Controller as well as the script that spawns the enemies.

Alright. the issue here is that although you’re spawning your AI character, you’re not spawning a controller for it. Add these two nodes to your spawn script and verify that both BeginPlay and OnPossess fire.