Better performance for AI checking enemies are visible for AI?

I don’t use AI perception or pawn sensing because it doesn’t have good performance in my game so I made my own. For my system there is an enemy array list for two teams of AI (red and blue). Each AI knows the locations of every enemy but that’s okay and part of the game. Until now AI simply had a behavior tree task to loop through all the enemies to find the closest one to set as its target but this meant the AI would ignore enemy AI they see but aren’t closest to while pursuing the one closest to them.

So I made a task to check which enemies are visible to the AI and sets the target to the closest one. The issue is that I only knew of one method to do so - firing a line trace toward every single enemy AI on that’s in the “enemy array”, which can go upwards to dozens of actors. This happens maybe every 5-10 seconds PER AI, you can see how this might cause a problem with performance. Using the task, I lose about 10 FPS.

Anyone have any suggestions on a more efficient system for checking what enemies are visible?

Thanks!

im assuming that your using something like an array when checking for the closest (ordering array based on proximity). if thats the case then why not just check visibility based on whos closest. basically go down your list: is index 1 visible no then move on, is index 2 visible yes ok hes our target. that method will at least limit how many traces you need to do. if you base the vis checks on whos close then you will be checking the enemies most likely to be visible the most often.

That’s solid, I’ll probably do that for now. I was actually thinking the same thing a few minutes after I posted haha.

Still I’m wondering if there are any completely different methods out there (mostly just to learn how others would do it), seeing as checking enemy visibility is a common task in most combat games.