More on how to design the Enemy State?

Hi Guys,

Thanks for the course but i wish there can be more elaboration on enemyAi state design. like what happended if State.Busy is not set when trytakeaction is executing?

Why use the current structure? is there a pattern to this?

what if i want to change the order of action took by each enemy?

Then you have a bug that should be fixed.

Yes. The pattern is

  • Find a unit that still has enough action points to perform an action
    • If we found one
      • Perform the action,
      • Repeat from the top
    • Else, end the turn

Then you will not change the state machine, but the value of each individual action. If you want an action happen before anything else, you will increase the value of that action.


The enemy state machine merely loops through all the enemies and have them take turns performing the best action they can with the current state of the game. What that best action is is determined by the actionValue which is either fixed (as with SpinAction with a value of 0) or calculated (as with most of the other actions). The action with the highest value at the point of the game is the action that gets chosen. With this structure, you need to change how actions are valued in order to affect how they are chosen.

Let’s assume for a second that guns run out of bullets. Collecting bullets is a new action we’ve added to the game, and we want an enemy to be able to do this. Enemies may not need to collect bullets if they still have some, but at some point they may run out and then we would want them to value getting bullets over approaching an player. So we come up with 3 options:

  • A smooth increase in value: the value of the GetBulletsAction could increase as the bullet count decreases (for instance we score it like (maxBullets - currentBullets) * 10), or
  • A staggered increase in value: the value increases at certain intervals. 100% stocked, value is 0, 50% stocked, value is 50, 25% stocked, value becomes 75, Empty, value is 100.
  • A hard ‘I need bullets now’: the value would always be 0 until the bullet count is 0, and then become 100 (for instance currentBullets == 0 ? 100 : 0).

With the first and second option there will be a chance that the ‘get bullet’ value is higher than the ‘shoot player’ action and the enemy will stop to pick up bullets, first, regardless of how many bullets it still has, but with the last option an enemy would walk right past a ‘magazine pickup’ on its way to the player even if it only has 1 bullet left. Different behaviours, and all we did was change the value of the action, and how it is determined.

Real enemy AI is an incredibly broad and complex beast, which is why you will find that all the courses here implement very rudimentary AI in order to get some enemy doing something that resembles an enemy with intelligence. From what I’ve seen, this course (and the RPG course having patrolling and team mechanics) have the most complex AI of all the courses. And none of these are great. I would personally not be against a fifth RPG course that focusses solely on NPC AI. Some basic Goal Oriented Action Planning, Behaviour Trees, etc.

7 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms