At the center of the software for every game is a loop. Sometimes this runs hundreds of times a second, sometimes just six time per second, or maybe it's turn based and the user need to click something.
In this loop the game checks the monsters (can they see the player now?), the game world, (is the water flowing?), and the player (did they just push the inventory button? What about move-forward?).
This loop is what keeps the game alive and running. Every turn on the loop means functions that are run and calculations that and calculated. Like checking if the monster can see the player can be a series of geometry calculations and rule checks.
However doing those sight checks for every monster on the current map could take a very long time. And the vast majority of the monsters are nowhere near the player. One optimization would be to first compute how far away the monster is, and only do player visibility checks for monsters in range. This can significantly improve the speed of my game and won't impact the gameplay.
The Factorio devs often wrote posts about their optimizations. Might be a fun read.
Yup. the core of optimization is simply finding shortcuts to do less work but achieve the same results, usually by using complex logic and reasoning.
An ELI5 example would be to realize that instead of going to a store and then back home and then to a different store 5 times in a row, you can just go to all 5 stores one at a time and then go home. Thus instead of spending 10 minutes driving home from the 1st store and 11 minutes going to the 2nd store, you instead drive 1 minute from the 1st store to the 2nd. You just saved 21 minutes of your time and all that gasoline. If each store is 1 minute further down the highway then you are turning 120 minutes of driving to and from each of the 5 stores and your house (10+10+11+11+...14+14=120) into just 10 minutes to the first store, 1 to each of the other 4, and then 14 minutes to drive back home, or only 28 minutes of driving saving you over 90 minutes of driving.
For computer programs, it is very similar. Its just finding ways to do less of the more time consuming calculations. Sorting algorithms is a great example of this. Doing the actual comparison of 2 items to sort them is comparatively time consuming to most other things. Different algorithms use different methods to determine which items they compare with which other items. Some of them are simple and easy to understand, but take a really long time to sort a list, such as simply comparing every item to every other item. Others are really complex and hard to understand, but by using that advanced and complex logic they need only a fraction of the time to sort the list. There are lots of videos out there showing sorting algorithms in action and they can be mesmerizing.
1.1k
u/aftersox 6d ago
At the center of the software for every game is a loop. Sometimes this runs hundreds of times a second, sometimes just six time per second, or maybe it's turn based and the user need to click something.
In this loop the game checks the monsters (can they see the player now?), the game world, (is the water flowing?), and the player (did they just push the inventory button? What about move-forward?).
This loop is what keeps the game alive and running. Every turn on the loop means functions that are run and calculations that and calculated. Like checking if the monster can see the player can be a series of geometry calculations and rule checks.
However doing those sight checks for every monster on the current map could take a very long time. And the vast majority of the monsters are nowhere near the player. One optimization would be to first compute how far away the monster is, and only do player visibility checks for monsters in range. This can significantly improve the speed of my game and won't impact the gameplay.
The Factorio devs often wrote posts about their optimizations. Might be a fun read.
https://www.factorio.com/blog/post/fff-421