Optimizing Mob Pathfinding. Improving Game Performance for Smooth Gameplay

Optimizing Mob Pathfinding. Improving Game Performance for Smooth Gameplay

During this week of development, I focused on improving the performance of my game Escape The Wave

During this week of development, I focused on improving the performance of my game Escape The Wave when a lot of mobs are searching for the player. This was a challenging task, but I found some effective solutions.

First, I updated the mob pathfinding by using a NavigationAgent instead of the get_simple_path function of a Navigation node. This new approach allowed my game to have many more mobs on a small map. Previously, in the demo map, I was limited to around 70 mobs, or else the game would become laggy. But with this new technique, I can have more than 500 mobs without any lag on a small map.

However, this technique alone did not reduce lagging on a large map. To reduce lag on maps larger than a certain size, I had to make some compromises. Not all mobs can constantly search for the player on the map, as this significantly affects the game's performance. To address this, I used a VisibilityNotifier to detect if a mob is displayed on the screen or not. If a mob is displayed on the screen, it will try to reach the player's position. Once the mob is out of the screen, it will keep its current direction without searching for the player. When a mob spawns, it initially does not have any direction to go, which can cause mobs to stack on a single point, creating performance issues when the player is nearby. To prevent this issue, I simply define a random direction for the mob to go in until it crosses the player's path.

Using these techniques, my game no longer lags when many mobs are searching for the player. Only the displayed enemies try to attack the player, while the others go in a random direction. However, in the game, it appears as if all the mobs are trying to find and attack the player. Sometimes the game may experience slight FPS drops, but they are minimal and do not disrupt gameplay.

In conclusion, improving game performance can be a challenging task, especially when dealing with multiple mobs searching for the player. However, by using a NavigationAgent and a VisibilityNotifier, it is possible to significantly increase the number of mobs on a small map without causing lag. Additionally, by defining a random direction for mobs to go in when they spawn, it is possible to prevent stacking and performance issues when the player is nearby. These techniques have helped to make the game smoother and more enjoyable for players.