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

I'm making a game in Flixel and I need to program an NPC. It's rapidly turning more complex than I expected. I was wondering if there are any best practices, tutorials or examples that you can refer me to, to see how this is done.

I can probably hack it together, which is what I always do, but it would be nice if I can make it maintanable and can add stuff later on.

Here's screenshot to give you an idea: The butler will be an NPC that will follow you, or guide you, and talk to you the whole time.

enter image description here

EDIT: More specifically: What I have now is a long list of IF statements in the update loop of the butler (about 8 different cases), and all I have covered is his walking behavior. I want him to comment on things and sometimes switch his main behavior to be more aggresive or distant,... Is there any way to keep track of this, or is complex code with many many nested if statements the way to go?

share|improve this question
This is a pretty broad question, do you have a specific issue you're dealing with? – Byte56 Sep 23 '12 at 23:09
You are right, I tried again to be a bit more specific, but feel free to flag the question if you think it is inappropriate. – joon Sep 23 '12 at 23:17
Don't have time to post a proper answer, but check out the slides from the GDC talk "AI-driven Dynamic Dialog through Fuzzy Pattern Matching", it's both simple and elegant: – junkdog Sep 24 '12 at 12:35

From your description it sounds like you need some kind of state machine, maybe switchable for different rooms/zones to keep things maintainable. Take a look also at Behavior Trees:

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.