Improving my code
By Jay Marriner, 17/01/2023
Normally I'm not one for making New Years resolutions but for this year I decided to make a push to improve myself by working on personal projects throughout the year. To achieve this I'm going to be making a roadmap of what I want to achieve this year; for now the outline is to overall improve my code in various areas from optimization to learning more technical techniques and methods. Aside from that I also want to finish 1 personal project within the year. Starting this process I decided to look at optimizing my code!

What I wanted to do

For this mini-project I wanted to stress-test my code with a small, easy to setup scenario. I landed on making moving AI agents using Unity's built in NavMeshAgents system. I then would create a script that would look for nearby stations for them to walk to. At face value this seems trivial, however in the past I have made these systems and found further down the line that getting to over 100 agents causes major lag and stress on system resources unnecessarily. My goal was to see how I could reduce the stress placed on the system.

How I did it

Throughout the mini-project I attempted different methods of optimization but initially started with the basics - getting a list of all stations and checking through each to see which is the closest to the agent and ensuring the station is free (not used by another agent). Again once this gets to more than 100 stations this can cause issues. My first remedy to this was to create a box collider on each agent that would then only look at the stations found within the box collider (if any at all) and determine the closest amongst them before looking at all the other stations.

This works much better and reduces a lot of issues and may even be enough for a realistic implementation for a game as the user is unlikely to spawn vast amounts of agents within a given place. In my endeavour to improve however I further looked at checking the box collider bounds and increasing the size of the collider if no stations are found, widening the search incrementally rather than immediately taking in all stations after 1 bounds search. This works well for reducing the load on looping through the list but also increases wait time whilst iterating over the increase in box collider size for what could be a far away station. Ultimately the trade off is worth it in most circumstances for the level size a game like this would occupy. Furthering my work I looked into other areas to look into in the future such as better Lamda expressions in order to make the query faster. I also realised that using arrays over lists may have sped up my search but could be impractical as this project would have new stations being added as time goes on (something more suited to lists).

Conclusion

To conclude; I have a ways to go to be competent enough to fully optimize the code to a standard I would be happy with in a commercial game. Grasping a better understanding of how to implement these improvements is what I will be looking into next and hopefully I will have more substantial improvements going forward.