Why is my blueprint AI causing the frame rate to drop

Disclaimer I will look into behavior trees tomorrow, but I’m looking for some general suggestions on why the following blueprints are causing the framerate to drop so I can learn more about making efficient BPs.

What I’m trying to do

I have about 8 NPC’s in the scene (all instances of the same BasicAI class). They move around randomly but when the player looks at one of them, the others flock towards that instance. When the player looks away, they all return to random wandering.

Current situation

I actually have this behavior working inside the First Person Template, which I’ve added the attached BPs to. The problem is that the frame rate starts out around 80fps when I load the game, but then drops continuously to below 10fps after about a minute of playing.

Can you help me make this more efficient?

I’m wondering if there’s anything(s) in the BP’s that might obviously slow things down, but I’m also open to general suggestions on how other people would approach this goal (within BP’s). The first blueprint is the BasicAI Blueprint, the second is my modified FirstPersonCharacter BP file that comes with the template. I hope I’ve put enough commenting in the images to explain how I’m thinking about it, but if its not clear, I can explain further.

More info on my approach

The FirstPersonCharacter BP draws a trace looking out for any Pawns. If it hits one, it:
a) Sets this as the Pawn the others flock towards
b) Stops the pawns from roaming free (the pawnFreeRoam bool is set back to true if the Cast fails).

The BasicAI BP checks to see if pawnFreeRoam is true

  1. if it is then the pawns are put into an array and each is moved to a random location IF they’ve reached the last random point (if velocity is <=0)

  2. if pawnFreeRoam is false, the pawns are again put into an array and then move towards whichever instance is stored in the other BP’s pawnOfInterest variable.

Thanks in advance for any replies!

My investigations with AI have definitely run into this. I have a similar setup with herding sheep. The problem you’re having is basically with the Tick event. The more you do off of Tick the slower it will go. So try to make things event based as much as possible. If I’m doing something on Tick, I compartmentalize it so it’s not always running. I know you’re looking into BTs tomorrow, but they really help with compartmentalizing.

The only things I have on Tick right now are some head tracking (rotating the head bone to face the player) when the player is within a certain range and some Debug Draw Text info (because I want the text to appear above the AI and they move each tick, so the text needs to move with them).

If you can let the BT do the work for you it should speed things up. I do think Tick ends up being more straightforward and smoother, but it’s really expensive.

Yeah, go with BTs :slight_smile: As far as I can tell the source of your slowdowns is constant calling of SimpleMoveTo which is a latent function meaning you tell AI were to go and it will go there on its own. Instead of letting AI code do the work every tick yo re-tell it where to go and it scratches the old request, finds a new path and restarts the path following.

BTW, SimpleMoveTo is the least configurable tool for making AI move. Just saying :slight_smile: