Designing scaleable city management game AI

I am currently using my own FSM to control the AI in my city management game. It can be anything from woodcutters, bakers, blacksmiths to soldiers or ogres.

It works really well but as the game grows it's scaling quite poorly and I am re-writing a ton of code for each professions since they share almost 80% of the behaviour.

I used this asset once but found it hard to work with when the AI got really complex. But I want to give it another try, but cannot really find any documentation that goes beyond the basics, which is not interesting.

Say I have a worker, and at runtime this worker can change profession between 30 different professions, and when it has freetime it can choose between a ton of different activities. I cannot imagine doing that in a single behaviour tree view. How can I manage a scenario like this to keep things seperated and tidy and scaleable? In a perfect world each profession would be its own behaviour tree, and there would probably be a "main" behaviour tree that checked like:

1. Am I hungry? -> Go eat
2. Do I have a job? -> Go work profession where professionKey == "woodCutter".
3. If I reach here I am idle -> Find an activity to do.

Is there some samples or anything I can look to that would help me get started writing a really big tree like this? Maybe that uses "sub-trees" for each profession to keep things seperated.

EDIT: (Please move this to behaviour designer forum)
 
Last edited:
Have moved this to the behaviour designer forum. It's not the area I know most about, but it sounds to me like you should take a look at external behaviour trees: https://opsive.com/support/documentation/behavior-designer/external-behavior-trees/ You can build behaviour trees in a very modular fashion, i.e. use several external behaviour trees as separate pieces of an AI's behaviour that you can then piece together in various ways. You can pass variables to these external behaviour trees etc. as if they were part of the same parent behaviour tree. It's pretty powerful!
 
Have moved this to the behaviour designer forum. It's not the area I know most about, but it sounds to me like you should take a look at external behaviour trees: https://opsive.com/support/documentation/behavior-designer/external-behavior-trees/ You can build behaviour trees in a very modular fashion, i.e. use several external behaviour trees as separate pieces of an AI's behaviour that you can then piece together in various ways. You can pass variables to these external behaviour trees etc. as if they were part of the same parent behaviour tree. It's pretty powerful!

Thanks, yes that seems to be what I need.

Is there some stress test done anywhere with simple AI on how behaviour designer performs with hundreds of active units at the same time? Since I cannot control how often BD runs its update Im worried it will become a huge performance issue later in development when players have cities with 1000 units for example.

Compared to my FSM that simply updates the current state every X frames.
 
Actually, if I may, I think you do have a way to control how often BD runs its update. Look into the BehaviorManager GameObject you have in your scene, you can specify whether to make BD run every frame or every X seconds (0.1 or 0.2 is not bad a number). Also, what I do in my project is uncheck the "restart when complete" box in the Behavior tab of every behavior tree in my scene (in the BD editor window), and have one Repeater decorator node in every big branch of my tree. That way I control what repeats and under what conditions.

You can also, for each task, specify whether it is instant (meaning as soon as it is done evaluating its own result, BD switches to the next task) or not (meaning when it is done evaluating, BD will wait for the next tick which is either the next frame or after a delay that you specified in the BehaviorManager GO). That way you can keep the important sub-trees fast and put the less important ones on the backburner.
 
Right, Justin's solution is even better because by running each Behavior Tree manually, you can batch the execution of your BTs and smooth it over time. For example, with 1000 AIs, you could run a different batch of 100 or 200 NPCs every 1/10th of a second, making them all make their decisions once or twice every second but not all 1000 of them at the same time.
 
Last edited:
Top