I am currently working on a tower defense game with around 200 AI enemies. These AIs are supposed to be simple, just moving towards a point and shoot something if they spot a target. The AIs have to work even when not rendered. I’ve run into a bit of a optimization issue.
For the sake of testing, I’ve turned off shadows for the AI characters, and the vertex count is around 500-ish.
Whenever I use the default character class for my AI, the framerate takes a nosedive. I have tried pawns with floating movement components didn’t quite cut it. I also tried making a custom movement component in C++, which did not work, and I will want to use the avoidance feature in the character movement component.
My question is are there any possible solutions or workaround for this?
Is each of your agents doing path finding, navmesh traversal?
if the path is pre-defined per level (bloons TD) then you could pre-cache the path, or simplify the path to cut down on nodes to traverse to find a “good path”
id the path is to be dynamically re-calculated (Fieldrunners) then calculate the new path for an arbitrary agent then propagate that path to all other agents (this could result in some agents having dumb behavior but if performace is your target right now then intelligent AI can have a smaller delay)
have you found a max number of agents before everything starts to slow down?
must you have all the agents at once, or can some of them be delayed/spaced.
maybe instead of spawning 500 separate agents could you consider 50 “clusters” of 10 or 100 clusters of 5. then when they get down to a “low enough” number swap them to individual agents.
well, it’s not really like the normal tower defense type, I think it’s more like a strategy game.
It’s not pre-defined, and I forgot to mention that the nav mesh is updating during the gameplay. Like if I placed down something, then it will update the nav mesh.
The max number of agent is like 100ish, anything more the frame will drop to a “not playable” rate, and I need all the agents at once
I’ll try dividing and spawning them into clusters, that might work. Any suggestion on how to start on that?
you could use a GroupPawn class that holds a few IndividualPawns.
the IndividualPawns would have an AI-State machine where they are concerned with avoiding each other this should avoid the potential of N(P) hard you are potentially running into), and receiving commands from the GroupPawn. as the IndividualPawns would only be concerned with avoiding within a small subset of NavMesh nodes the pathfinding traversal should be small if not just a steering behavior.
the GroupPawn is in charge of the actual path finding, and deligating down to the IndividualPawns.
in theory you could have the GroupPawn simply concerned with the Global Position, and the IndividualPawn concerned with everything else (if they are not allowed to move+shoot simultaneously then that would maybe be an edge case), if this hits similar hurdles (should still give a multiple of headroom) then formations would maybe need to be applied where IndividualPawns in a GroupPawn have no ability to move themselves, and the GroupPawn would be responsible for shifting them around (similar to in the editor when you group select actors in the level)
then when the members of the GroupPawn get “low enough” (if the group starts with 5 then maybe at 1 or 2) the IndividualPawns would break loose and be able to do actual Pathfinding themselves. this can incur some overhead on the edge-case where you start with 70 GroupPawns 10 groups are reduced to 2 members with no groups fully eliminated so they are broken off into Individuals which would bring your big pathfinding agents up to 80 which would be a higher load on performance.
For this edge case you might need to have thresholds of when to favor putting those Individuals into an existing GroupPawn that has an empty slot, or have “close enough” Individuals form a GroupPawn so given the situation I described those 20 Independent agents would end up forming int the “best case” 5 GroupPawn agents bringing your full pathfinding agents to 65 this will depend on the general behavior you desire as an Independent agent charging through 5 Groups just to fall into position with a group that has 4 might not be desired.
Are the characters using skeletal meshes?
If so then for a large amount of units you could try looking into VAT’s (Vertex Animation Textures). They are a more light weight way of animating large numbers of characters.