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?
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.
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; } }
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.
– anon80012675This 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:
– senad// 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; } }