3

I've written a few event-driven iOS apps, but this has not fully prepared me for what I am working on now and I need some advice.

I have a simple strategy game design that I have prototyped using views, animation blocks and gesture recognizers. With board, moves and basic UI in place, I am just starting to create AI players and have realized that I'm not at all clear on the best way to proceed. I need to generate the AI move events and resulting animations without blocking the main thread. Years ago I might have used a classic gameloop pattern, but I'd like to avoid doing it that way in iOS as it doesn't strike me as a technique I can use in non-game apps, and one of my goals is to extend my understanding of how to structure iOS apps.

I've read about GCD and my sense is that it would be possible to do a lot of my work off the main thread with GCD, but it is not clear how I would coordinate the various parts. For example, an AI needs to evaluate the board, but every time an actor changes the board the current evaluation becomes invalid. Do I set up a queue to manage the board state? Do I put each AI player in one queue? What is the best way to interrupt an AI when it is choosing the best move?

Does anyone have a sketch of the kind of architecture my app need to support this? I'm looking for patterns that I can use in general, not just in games. Some working sample code that I could experiment with would be perfect, if anyone knows of such a thing. Thanks!

Fran K.
  • 866
  • 10
  • 23
  • This may help you. In that answer there's also a couple helpful links: http://stackoverflow.com/questions/7693203/performselectoronmainthread-with-multiple-parameter/7693262#7693262 – bryanmac Jul 28 '12 at 16:44
  • Specifically, there's some sample code in the GCD part of the answer here: http://stackoverflow.com/questions/7290931/gcd-threads-program-flow-and-ui-updating/7291056#7291056 – bryanmac Jul 28 '12 at 16:56
  • Thanks for the pointer, I looked at dozens of postings and missed this one, it is the clearest written one I've seen. It is not exactly what I was seeking but it did jog something in my mind and I think that half of my problem can be solved by putting my data model in one background queue and sending all human and AI input there, then have that queue send UI updates to the main queue. I feel like I'll need more than this, but this idea is worth some quick prototyping, thanks! – Fran K. Jul 28 '12 at 21:20
  • I don't have a full solution as of yet, but wanted to update the question with what I have discovered so far. First, UIView animation blocks are easy but limited, so I've started transitioning to CALayer animations and am convinced this is the way to go. Second, since I have yet to convince myself that I must go the CADisplayLink route, I've used a timer via GCD and that has been working well. GCD is pretty good, and I've been rearranging my code to run outside the main thread and to pass blocks to the main queue for UI updates as needed. I'm not there yet, but am making progress. – Fran K. Aug 06 '12 at 19:58

0 Answers0