- Home /
Scaling AI for Many Actors/Agents
I'm creating an RTS-style game, and while I have AI set up for my actors (turrets, tanks, etc) that works fine, it doesn't scale well. Once a few dozen actors are present, my game starts to slow way down.
I need some guidance on how to design my AI so it scales well. What are some techniques I can use to reduce the workload?
I'm currently using Behave's behavior trees to handle the AI. Is there anything I should be aware of when using Behave for an RTS-style game with many agents?
Answer by senad · Mar 27, 2012 at 09:17 AM
I do not have links, but here are a few thoughts that come to mind:
If you have a general slowdown you might try to reduce update rates. Reducing it from say 200ms to 500ms might not be noticable in the game but double your performance. It might even look more natural if the actors take some time to think.
If you have a lot of actors you might experience performance peeks or stuttering if you have a lot of BT updates in one frame. For this you might want to cap the number of updates which are allowed in one frame.
Another idea might be to move the AI to a separate thread altogether. You will have to spend some work on synchronization and it is a tricky task however.
Code optimization might help you if your AI code has poor performance. Profiling will help you find the bottlenecks.
I hope this helps. Good luck, happy coding. :) :)
Thanks senad. I'm using the indie version of Unity, so the profiler is unfortunately unavailable (I really need to save up for it). I double-checked my update rates for the behavior trees, and it turns out they were running twice as often as I thought, which was a major problem. I hadn't considered limiting the number of updates per frame, and I don't know if it's easy (or possible) with Behave, but it's an excellent piece of advice.
Hey $$anonymous$$, glad I could be helpful. :)
There is a GUI field for the update rate in Behave, you still have to use it in the code yourself. But it is relatively easy to do so. I found most of the infos in the Behave forum thread very useful: http://forum.unity3d.com/threads/61849-AI-Behaviour-trees-in-Unity-Behave-1.1-released
Also the videos from angry ant.
For the profiling, since unity is using .NET, I think it should be possible to use any .NET profiler out there. I have not tried this though. There are many free and commercial profilers available. The one I have been using at work is ANTS, it has a 14 days trial period which should be enough to profile your AI. :)
This is how implement the behaviour tree update to use a frequency set in the GUI. You can also have different frequencies for different actors, making some important ones faster thinkers: // Update is called once per frame
void Update () {
m_timeSinceLastTick += Time.deltaTime;
float frequency = m_tree.Frequency;
if ( frequency != 0 && m_timeSinceLastTick > 1f / frequency ) {
m_tree.Tick();
m_timeSinceLastTick = 0;
}
}
Your answer
Follow this Question
Related Questions
How to make 2 groups of agents attack eachother? 0 Answers
Using Behave to alert other AI 1 Answer
Tutorials for AngryAnt's Behave 1 Answer