Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would I go about having multiple AI on the screen all at once all calculating separate movements without too much lag and within sane working memory. Before I have had many on the screen by putting objects of them in an array and looping through calling the move method, however this isn't the fastest method of handling them. Is it okay to use threads when you have say 200 AI at the same time on the screen? Can anyone help me work out the best structure for having multiple AI (which all move)?

share|improve this question
Where you actually experiencing speed problems when updating them in a list like you were? Or are you just looking for a faster way because you think you might need it? – Byte56 Jul 16 '12 at 22:31
up vote 2 down vote accepted

It is highly unlikely that you will have so many enemies, that calculating their movements slows the game down.

In a game with a large number of enemies (thousands), most will be off the screen, hence presumed to be (largely) dormant, at any given time. This reduces the amount of effort to do them to very little (perhaps zero).

Using threads is going to hugely increase the complexity of your code, introduce many bugs, and may not actually increase performance at all (for example, if you have more threads than cores). But worst of all, it makes the game nondeterministic, which is never a good thing. You want to be able to replay the game with the same inputs (for example, many mutliplayer network games depend on this).

share|improve this answer
I think I might have overlooked the AI handling off screen. All my AI move without caring for being on screen. Ill try this out later. Thanks. – Smallbro Jul 17 '12 at 7:23
Depending on the game, you may still want units off screen to update their positions. But they don't need to perform animation calculations or be drawn. – Byte56 Jul 17 '12 at 14:31
We only want to perform this optimisation if absolutely necessary, and if it doesn't screw up the gameplay – MarkR Jul 17 '12 at 22:21

Pluses: Java does fine with hundreds of threads used this way. You get to use all your cores. And while you only have a few cores, many of your threads will be blocking and unable to run, so having a few unblocked ones ready keeps things moving. (Though most of your threads should be sleeping most of the time. You want a bit of spare CPU usage to the OS can respond rapidly to things outside your game (internet connection, for instance) and even things in your game besides AI.)

Minus: This kind of multithreaded programming is a monster. If you want to do this, study it well. Write your code carefully, because the bugs will not repeat themselves when you want them to and so are extremely difficult to track down. And writing safe code is not enough; you have to minimize blocking. If you have 8 cores, but all the threads are trying to get one lock, you'll have only 12.5% CPU usage. Worse, the player is apt to notice the slowdown. Basically you need to synchronize everything and synchronize nothing at the same time.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.