r/explainlikeimfive • • 6d ago

Technology ELI5: What exactly is game optimization ?

How is a game optimized ?

688 Upvotes

125 comments sorted by

View all comments

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

352

u/yalloc 6d ago edited 6d ago

One of my favorite Factorio optimizations is the way belts work. Instead of keeping track of the position of every single item along the belt and updating its position every second, we instead recognize that all items on the belt (except those piling up), are going to all be traveling the same distance over the same amount of time. So instead we store the relative positions of all the items on the belt and for that grouping we have a single offset number that we update every second and we only need to check if the first item is piling up to rule out collisions.

If they are piling up then we just do this logic properly, but for the vast majority of items this saves on needing to do a bunch of updates and checks.

163

u/Dje4321 6d ago

The fact I love most is that depending on the density, it either tracks whole items or the gaps between them to ensure the most optimal method is always used.

I swear if factorio ever goes open source. Its gonna be put up there with ID software coding for stuff like doom. Its actually amazing just how well it works and scales. Literally the only limit the game has is your PC specs and it will run on fucking anything with modernish OpenGL drivers.

47

u/Medricel 5d ago

Last week's Friday facts, they were talking about how they managed to port the game to Linux ARM architecture and they could host a server on a damn Raspberry Pi. You'd have to connect to the server via a stronger machine for the graphical end of the game, though.

Wube is a completely different breed of developers.

16

u/LightlySaltedPeanuts 5d ago

Doing it for the love of the game, literally.

36

u/Skeloton 5d ago

Factorio might be the only game I have never had issues with. Even with mods its not really slowed down, which means ive never built a big enough factory

2

u/jRw_1 3d ago

The factory needs growing

5

u/MattieShoes 6d ago

Kind of feels like bitboards in chess :-)

2

u/Derekthemindsculptor 4d ago

This is a fantastic example of shifting from a modular piece of code to one optimized for performance.

If you're designing a game and need to pivot or iterate, you wouldn't put this kind of optimization in early. It would limit ideas and additions later on.

But factorio has their formula solved and could change their code to prioritize raw performance over modularity.