Performance Wise, Should I use Behaviour Trees or Standard BP script for NPCs? (Roaming AI)

I’m trying to optimise my game for the best possible performance, so I’m wondering what’s better for performance? BP scripted events or Behaviour tree for basic roaming AI?

All I’m trying to achieve is to have random AI roaming about a level, passively, What is better on performance to achieve this? BP scripting or Behaviour tree?

Performance wise you could probably answer this either way depending on how you set things up. One wrong move could throw either off the rails. The real difference probably comes down to effort and organization instead. I personally would use behavior trees because those already automate a lot of the code you could be writing in your own scripts.

For example, you could see better performance by ticking your AI tasks at a slower pace than frame by frame (which you’d have to do with custom timers in blueprint instead). Some of the testing around ‘have I reached my target yet’ / ‘should I pick a new target’ is really easy in a tree. EQS queries might be super handy in your case, even for something passive. On the flip side, if you’re new to behavior trees you might have to spend a bunch of time trying to figure them out.

Regarding organization, writing your own scripts right inside one AI blueprint might be more appealing to you than having some code in a tree, some code in tasks, some code in the AI, etc. I’d still go with trees because a lot of those tasks etc could be reused for other AI. But if you’re doing something super simple, these advanced tools might be overkill.

You have to assume that performance is important to everybody working with AI. So, try to answer this question: Are you smarter than Mieszko Zielinksi? If yes, then maybe your own custom scripts would be better than using trees - I just doubt it personally… :slight_smile:

How many AI do you have ? Are all of the AI visible ?

1 Like

I disagree with this quote: "Are you smarter than Mieszko Zielinksi? "
There is nothing to do with being smarter or not.

Mieszko Zielinksi is capped to make a generic AI, while YOU yes you can make your game specific AI, and YOU can make it much faster in terms of performance.

1 Like

I have about 100 actors running around a level. Since they’re just passive roaming AI, I used event driven BP scripting instead of using tick etc… Many of them are visible at a time yes.

No significant performance hit as of yet though.

So why would you want to optimize if you do not have a performance issues ?

In general I don’t know which one is better for performance - but to achieve my goal (it was 400 - 500) AI. I did it with non of the above__ (Generally I used C++, but most of the tricks could be achieved with BP, still c++ is faster and more options to do).

For roaming characters I used a Background thread to generate a pool of random numbers (directions my AIs would go to) - here is my tutorial Wiki

In addition I did not use EQS and ofc no any shape to detect overlappings (to know if the player or vice versa is in the attacking radius) - I did the it also on a separate thread which measured distances.

Another and very significant optimization was subclassing the CharacterMovementComponent
Dividing it’s tick to 4 groups (when the character is not rendered) means there are no 500 AI ticking together, but only one group per tick.

I also had to invent a movement prediction mechanism to cull of RVO and fire it only when I predicting that units are on a collision course (well this one is questionable - I needed this not for an optimizations but for a smoother gameplay unit movement).

My AI is tick based - but the key is to span tasks across the tick - once again it is like dividing the tick in to several groups - for example:

Tick1 (Query (thread based) enemy unit detection).

Tick2 (Perform some logic - in my case behaviors).

Tick3 (Perform other logic).

The key is you are not doing all the tasks in the same tick - but diving it to 3 or more ticks, which will increase CPU performance.
(For instance if I still was CPU bottle necked and I would add more AI - I would rework the Pathfinding to be asynchronous it would add up some more milliseconds to my bank of time).
And honestly my 500 Unit CAP is GPU bottle-necked :-(.

But my point is: – If you are not lacking performance and everything is smooth enough - just leave it (“Premature optimizations are the root of all evil”)!

Anyway good luck and happy optimizations :wink:

Wanted to push the number of actors up further to be honest.

as from what i have seen ue4 does not do AI its only done with unreal asset that need to be bought for any ai to work in unreal unless you count the 90% games out on steam that only cover zombie and third person shooter now there are alot of those