Undefined AI Behavior when there are Multiple Instances


By Will Fox

3/3/2023

Hey everyone!

Today, I wanted to discuss a problem I ran into regarding AI senses. More specifically, AI perception in Unreal. Since my primary role in the group is to create enemy AI, dealing with sensing the player is a common occurrence. However, recently, my group pointed out to me that the AI acts a little funky when there are multiple instances of them in the same level. Seeing this, I instantly got to debugging since this would be a big issue if it weren't resolved. After all, the entire point of a dungeon crawler / rogue-lite game is to fend off multiple enemies at once. If this issue is left unresolved, a big chunk of our game gets flushed down the drain.


First possible issue: Instance Synced

At first, I thought that the issue was mainly based on the accidental sharing of variables within my AI blackboard. When I initially searched for this problem on Google, most of the answers pointed toward a variable held within the blackboard called "Instance Synced." If Instance Syned is checked, it makes that variable globally accessible and can mess up the way other instances of that AI act. Unfortunately, all of the variables within my blackboard already had Instance Synced off. This meant that my problem was elsewhere.


Second possible issue: Accidental switching of a variable

The next place I checked for the bug was inside my AI controller. More specifically, at a variable called "Has Line of Site?" After taking a closer look at the strange behavior of my AI, I noticed that they would see the player, move towards them for 3 seconds, and then lose interest and wander off. Everything about this behavior was correct except for the very first part. The behavior expected from the AI is for it to notice the player, and if it has a line of site, then it should begin shooting at them. However, the AI didn't shoot and instead walked toward the player. This meant that the AI was attempting to regain its line of site so it could attack the player again. This is why I suspected the "Has Line of Site?" variable to have relevance to this issue. However, I was confused as to how this variable could be getting flipped. Obviously, the AI sees the player for a split second since it walks toward the player's location to regain its line of site. But what was making the AI lose its line of site to begin with? All variables inside a controller class is local to that controller and that controller only. Even if there are thousands of instances of the same controller, every controller has its own personal variables. This means there is no possible way for the controllers to change each other's variables. So, that meant there was a flaw in my logic.


The reason the AI didn't work

The screenshot above is the entire reason AI didn't work as expected. The expected behavior of these nodes was for the AI controller to see if it successfully sensed something, and if it did, it should check the tag of that perceived object. If that sensed object has the tag "player," then it should turn on the "Has Line of Site?" variable and begin shooting at the player - or pursuing them if it's a melee enemy. However, there was one small issue. There was nothing that checked if the perceived thing wasn't the player. See, the nodes above worked perfectly fine if there was only one single AI in the level. It was only the AI and the player. That was it. But, since there are multiple AI's running around, that means that they can run in front of their companions - and thus, their companions will perceive them and find that they are NOT the player. Since there is no logic check for what to do if the thing they perceive isn't the player, the AI will execute the "false" section of the branch node and turn off the "Has Line of Site?" node. Here is what I did to fix it.


Yep! This was the entire fix. Two entire nodes to fix a rather significant issue. The logic behind this was that if the thing the AI perceived wasn't the player, then it shouldn't do anything. The player is the main focus and is the ONLY actor that should be able to change the behavior of the AI. AI shouldn't change the behavior of AI - not yet, at least. So yeah! I know it's pretty anti-climactic, but if you have been running into some undefined AI behavior due to multiple instances, you might need to add an extra check to ensure the AI only acts upon perceiving the player.

Leave a comment

Log in with itch.io to leave a comment.