So far I’ve had only a couple of actors on my game board. Granted I might not need more than 6 to 12 actors onscreen at the same time to bring some excitement, There are additional factors to consider:

  • It is better to be able to process interactions for a large number of characters, on and off-screen.
  • It may be simpler to be able to add all character for a game level right away, rather than having to generate NPCs on the fly.

I’m running a mere 100 actors now and the game is already crawling on our iTouch.

For now the rendering overhead is very low (all additional actors are just rectangles). With the help of instruments, I reduced several overheads related to game logic – I didn’t get a smooth running game again until clearing everything.

  • Actors need to find about nearby actors while idle. This allows an actor to determine what to do next using a ReflexMap class.
    • Added bound checks to reduce the overhead of finding nearby actors.
    • Removed unnecessary messaging involving type casting and type inference.
    • Removed all code associated with actors picking objects. This code may need to be optimised later but for now it is simply unused.
  • Disable debug related rendering. Just this uses 18% of the processing time (drawing strings over the 3D view using Quartz.
  • After applying the above, the game was still running pretty slow. So I randomly avoid deciding a new action for idle actors, skipping 7 frames out of 8. At this rate, actors still respond reasonably fast.
  • Remove all NSLog calls from the run loop. Don’t even think about profiling or optimizing while generating console output.

After this, the game is running fairly smoothly, although not at a great frame rate (16fps).

I further optimized AffectMap. This complements ReflexMap – affect map determines what happens to an actor (e.g. getting hit) versus what they choose to do. I used bounds checking with a shorter range (effects are always direct). Running AffectMap is still twice as expensive as ReflexMap, because I can’t skip any frames.

Now I can push to 24fps and things look reasonably smooth. A hundred actors on stage, all live and interacting even when off-screen, doesn’t seem too bad.

Having a look at the current snapshot, it’s not difficult to guess more efforts will need to be dedicated to optimizing -this app is drawing next to nothing still.