Enemy A.I, attack one at a time

I have 2 enemy A.I fighting me but, they both attack at the same time. How do i restrict their behavior to attack and not attack.

When one enemy is attacking then, the other should not attack.

For visual reference, the free flow combat in the batman arkham series has A.I in 3 basic states during combat. Idle/ Strafing/ Attacking. Enemies typically rush at player one at a time.

Hi there,
Try to setup an enemy “leader” who gets nearby enemies, and one at a time, select one and command this unit to attack. The attack has a max duration and cooldown state. So, while in cooldown, enemy moves to a location and waits, keepkng playing in focus. Then, the leader picks another enemy not in cooldown and repeats the command. From the enemy unit’s perspective, wait for attack command instead of cycle through attack attempts.

If the leader is killed, either a new leader should take charge, or enemies could simply attack player randomly (panic mode).

This enemy leader logic can also be set in your game mode, that manages the waves of enemies following the logic of getting a smaller number of enemies and coordinate its attacks. “Easier said than done”, but it’s a possible direction.

1 Like

A combat director! I haven’t implemented this yet but its a step in the right direction.

Currently my game looks like this

AI sees the player
AI moves to the player
AI attacks the player

Great for fighting one enemy but, terrible for fighting more than one.

I added a delay, so they won’t attack so often

If you move that logic to the Game Mode, add an integer index to every enemy, so every 4 seconds, the game mode will choose a random integer in range, set an index variable and send this information to all enemies. The enemy will read the index coming from the game mode and compare it against its own index. If is equal, then attack, else wait and move around player.

1 Like

Okay so I’ve tried and FAILED coming up with a combat manager.

I don’t know how to add spawned enemies to the combat manager.

I haven’t figured out how to communicate BT Tasks to GameMode.

I made variables in the [AI Attack Task] to check if one enemy is about to perform task but, that failed. (Behavior tree still selects all enemies instead of one)

I made variables in enemy AI blueprints to check if enemy is about to perform attack task too. That also FAILED!

What am I doing wrong and how do i get AI to attack one at a time!

trying to do too much at once.

simplify it to text only first.

an array of strings represents each enemy. Only one can be “active” at a time.

When one becomes active, tell all others to deactivate.

It goes like this: select index from the array, set it as “active enemy” variable.
Loop through the array. Any non-matching string calls a Deactivate function (just print a string for now, later on this would be a function in the enemy actor to call).

Same thing with active enemy, call a “Activate” function for it.

Same idea as radio buttons (google that).

If you get it working like that, you’ll understand how to take on the next level of complexity which is to expand communication to separate actors.

Like this?

No I dont think that will work.

You are probably misunderstanding what some of those nodes mean. For example, getting characters in a string means that it is selecting each letter in a word. And you don’t have anything plugged into the for each loop so its not going to do anything.

First - one rule you can pretty much just follow: dont use display name. It will only work in editor (not in a build). If you need to identify an actor somehow, you’ll want to do it some other way. You can use tags, or assign an instance editable variable, or use an == operator, for example.

Now, here is some code that is example only. It won’t work as is. You’ll need to think carefully about your own code and how you can fit a system like this in. But the point is, break down the big problem into just a few steps at a time.
Rather than trying to get a bunch of different actors communicating and doing something complicated, first just focus on the basics of working with the array like this:

If you can get something like this going such that when you press a key, you get your debug string saying that EnemyA is now active and EnemyB,C,D,E, etc are no longer active, that completes this first hurdle. That will demonstrate how you can filter through an array to toggle on and off things inside of the array.

The next step is expanding that out to the actual enemy actors but that will be much easier to understand once the array work is understood.

Here is some higher res in case its hard to read:

Cooler heads prevailed. I went back to the video above and tried again. I found a way to add spawned enemies to the attack manager using ‘add-unique’. I analyzed the AI toolkit off the marketplace and decided to use their blueprints instead. On to the next step!