Monday, November 14, 2011

Game AI Objective-C Chapter 2 Source

As promised here are the links to the chapter 2 examples of the Programming Game AI Example book. As mentioned earlier, I have tried my best to stay true to the original C++ code structure. Only in the third examples have I differed a little by using Objective-C properties. Couple of things to note about the third example. 

The telegram structure has been converted to a class. For the Entity Manager class I am using NSMutableDictionary to store the entities (similar to C++ std::map) and to store the telegrams I am using an NSMutableSet (similar to C++ std::set). To be honest, I myself need to take some time to go over the code I have ported to fully understand whats happening before I go to the next chapter about autonomous agents. But, regardless, the ported code works as expected!

I have not made any attempts to do any memory management (except a couple of release messages) as the current examples are to be run on your Mac and not on an iOS device. My future plans for the next set of examples for Steering behavior is to try to make an iOS version of it and not a Mac version. That should help me better learn memory management, which I found is one of the toughest concepts to learn coming to iOS development. For now here is the chapter 2 code with bonus class diagrams in the doxygen section!

Xcode 4.2 Projects
Doxygen Source Listings (including class diagrams)


6 comments:

  1. Looking good!

    One of the great things about iOS5 is ARC - no need to deal with retain/release any more :-) There's more emphasis on properties too, so it's probably worth your while adopting those in favour of hand-coded accessors. Check out the new "iOS5 By Tutorials" stuff on raywenderlich.com. I bought the book to get at the intermediate examples, but I believe they're putting all the beginner tutorials on the site for general access.

    Good luck with chapter 3!

    ReplyDelete
  2. @jawzapps
    Building iOS5 apps will exclude the older devices is that not correct? If you are targeting iOS 3 and above can we use iOS 5 ARC?
    By the way Ray Wenderlich is from Baltimore/DC area which is where is from. I was in an iOS intro class of his last year!

    ReplyDelete
  3. I've got iOS5 running fine on my 3GS, but I don't think it's supported on earlier models. ARC is iOS5-only, so you won't be able to use it for an iOS3 target. You're missing out on a hell of a lot of good APIs though if you stay with iOS3!

    It's worth upgrading to iOS5 just for the ARC, but the new "storyboard" way of creating the UI is very impressive too. Forget all that messy table cell coding and do it all in IB - definitely the way forward IMHO :)

    I was seriously considering flying over to attend Ray & Rod's recent Cocos2d session, but in the end couldn't justify the expense (I'm in the UK) - maybe next year when my game sells millions, eh ;-)

    ReplyDelete
  4. Great Work!

    Im glad someone has taken the time to do this. I look forward to seeing chapter 3. Ive been keen to try porting some of the code myself, but Im not very fluent in C++ (and Im still learing objective C)

    One question, it appears the line in main, @autoreleasepool { is causing me a parse error. If I comment out that line then everything works fine but there is a memory leak. Is there somethign special I need to use @autorelease pool such as the ARC mentioned above?

    ReplyDelete
  5. @Ross Millan
    Hi Ross,
    Thanks for the encouragement. I would say Chapter 3 is 70% complete. But then thats without a single build/run. I bet a lot of debugging will need to be done! And I am going to make it into an iPhone app. Hopefully I will be putting out an update blog on where I stand soon.
    Couple of things in my Chapter 2 code. You need to create a command line project and not an iPhone project. I am using Xcode 4.2. If you download my project file it should compile without any issues. But.. I have not paid attention to memory management since its a Mac command line project.
    By the way, I am learning C++ as and when I attempt to port to Objective-C!
    nata

    ReplyDelete
  6. Yes I did a bit of googling:
    http://stackoverflow.com/questions/8069570/autoreleasepool-make-errors

    Seems to fit my problem, because im running Xcode 4.1.

    Ill upgrade and see how I go

    ReplyDelete