Introduction to AI Programming for Games

Update: We have an updated version of this tutorial for iOS 9 and GameplayKit here. When you make a game, you often have enemies for the player to combat. You want these enemies to seem intelligent and present a challenge to the player to keep the game fun and engaging. You can do this through […] By Ray Wenderlich.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Gratuitous Sounds

Wouldn't it be cool if you added a sound effect with the AI switches to a state, so the player has a hint what state the AI is currently in?

Your first idea of how to do so might be to implement a "changeState" method that you call instead of changing the state directly. Inside the changeState method, you could switch on what the new state is, and play the appropriate sound effect.

That would work, but you might notice things are starting to get a little ugly here:

  • You're starting to have code that switches on each state split into different areas - the update statement, the stateName method, the changeState method. You'd think the code for each state would be better off together!
  • That big if/else statement in update: is starting to get long and ugly. Imagine what would happen if this game was more complicated than this simple example!

So in this section you're going to refactor your code a bit to make things a bit cleaner - instead of having each state's code be separated all over the place like this, you're going to make a class for each state.

Create a new class with the iOS\Cocoa Touch\Objective-C class template, name it AIState, and make it a subclass of NSObject. Then replace AIState.h with the following:

@class AIPlayer;

@interface AIState : NSObject

- (void)enter:(AIPlayer *)player;
- (void)execute:(AIPlayer *)player;
- (void)exit:(AIPlayer *)player;
- (NSString *)name;

@end

This just defines a few method that each state can override. They will be implemented as empty - replace AIState.m as follows:

#import "AIState.h"

@implementation AIState

- (NSString *)name {
    return nil;
}

- (void)enter:(AIPlayer *)player {
}

- (void)execute:(AIPlayer *)player {
}

- (void)exit:(AIPlayer *)player {
}

@end

Next, switch to AIPlayer.m, remove the old enum and instance variable, and add a new import and instance variable for the current AIState instance:

#import "AIState.h"

/*typedef enum {
    StateMassing = 0,
    StateCountering,
    StateDefending,
    StateRushing
} State;*/

@implementation AIPlayer  {
    //State _state;
    AIState * _currentState;
}

Next replace update: and changeState: with these much simpler versions:

- (void)update:(ccTime)delta {
    [_currentState execute:self];
    [super update:delta];
}

- (NSString *)stateName {
    return _currentState.name;
}

Also, the new state classes will need a way to switch states, so add a new mehod so they can do this:

- (void)changeState:(AIState *)state {
    
    [_currentState exit:self];
    _currentState = state;
    [_currentState enter:self];
    
}

And predeclare this method in AIPlayer.h:

- (void)changeState:(AIState *)state;

Next you need to move all your state code that was in AIPlayer.m to subclasses of AIState. To save you some time, I have already done this for you, just download these resources for the tutorial and add them to your project. It also includes the extra code to play a sound effect when it enters some of the states!

Finally, inside AIPlayer.m add this import:

#import "AIStateMass.h"

And at the end of viewDidLoad, add this to set the initial state:

_currentState = [[AIStateMass alloc] init];

And that's it! Build and run, and now your state code should be a bit cleaner - and it should play some auto-tuned sound effects (made by yours truly) when the AI switches states!

The final game with some simple AI!

Where To Go From Here?

Here is an example project with all of the code from the above tutorial series.

Remember, that this tutorial just barely scratches the surface with Game AI - there is a ton more you can learn from here, and more elegant ways to do what I showed you above.

But also remember that the real goal of game AI to is to make your game fun - so don't make things more compilcated than they need to be! :]

Here are some recommended resources for learning more about Game AI:

Harken all AI programmers! Do you have a cool AI technique you would like to share with readers of this site? I would absolutely love it if you'd like to extend the sample game in this tutorial to demonstrate it and write a follow-up to this tutorial about it. Contact me if you're interested in this!

If you have any questions or comments about this tutorial, please join the forum discussion below!


This is a blog post by site administrator Ray Wenderlich, an independent software developer and gamer.

Contributors

Over 300 content creators. Join our team.