Factorio in Unity ECS

Has been a while, nearly 2+ years.
And a lot has changed. OOP in Unity was yesterday. Looking back at my previous posts I see fruitful running against the wall.

For a primer on the CPU, ECS or why certain mechanics run slow watch this:

 

The short answer is memory layout and the problem with Heap based memory allocations. The memory points the algorithm needs is, most of the time, not sequential so a lot of cache-misses are introduced which utterly destroys performance. To talk in numbers, cache misses result in 100ns per variable where a local cache hit results in 1-3ns of waiting time for the CPU.

Quite drastic if you add it up, so how can we write code that has no cache misses AND is multi threaded?

ECS to the rescue!

Other than in OOP one class doesn’t do everything. The new abstraction is thinking in jobs and systems.

So which system and jobs do we need to have several transport lines with items that constantly move and have an end point?

transportBelts

 

The obvious one:

– Process Velocity: add a vector to the item position

With that alone the bar can be set for performance. Results are pretty impressive.

On my i7 3.5Ghz with 1 million items this runs with 230ms per frame. That’s a pretty good number, wait, oh, right I need to enable Burst compiling.

3.3ms with 1 million items and minimum rendering

Praise Joachim Ante and his team! What is happening is that the code gets optimized with SIMD code. I could get these numbers before in isolated tests but now we want to go much further than just moving items without logic.

– ProcessDistance: subtracts the movement distance and adds data for
– ProcessFindNewLeader: adds data to the new leader and adds data for
– ProcessUpdateDistances: calculate distance between new and previous leading item

Which adds up to:

4.5ms with 1 million items and minimum logic and rendering

From there more jobs can be added to the system so it can support curves and intersections.

The beauty is, that edge cases that are very specific to mechanics can be built on top. It’s certainly possible to run into problems of needing data that hasn’t even been generated yet but when things run in parallel it’s not that hard to integrate unlike in other code architectures where refactoring is often necessary.

Ramble-section

The future for Unity looks very bright. Graphics can be seen but ECS and Burst can only be simulated and I’m not only impressed but also really excited what kind of crazy games will be released in the Unity engine. Games that just scale things up so drastically to the likes we have never seen before.

As this system is quite generic it could be used for many things that has the logic of following a leader. Some form of crowd simulation, worm/snake like creatures, you can certainly get creative with it. After all it’s just a rigid rule set that can be used on anything that has the required data for the system to run.

What ran really bad and was only possible by the most experienced C++ programmers is now possible for a large range of coders.

I think this will introduce us to a new era of games. Sounds hopeful? Well. The big games, like pop culture level, were based on technological marvel. Programmers made something no others thought possible.

If you look deep enough you’ll see impressive step ups in scale in 3d world size:
Doom: verticality, slopes
Quake: Full 3d
Half-Life: connected levels
Minecraft: infinite worlds
(Bonus for level design scale)
Dark Souls: Interconnected world

or multiplayer
Quake
Battlefield
World of Warcraft
Fortnite

and many more, you get the point.

Unfortunately, the bar couldn’t be any higher for games right now.
To stand out, just a little, you have to impress and the zombie kill genre has done it best with scaling up zombies so much you have completely different games and genres. Games like Left for Dead are no coincidence, hardware instancing was new and the Source engine handled it with great effect. Smooth gameplay with zombie filled rooms.

And that’s also the problem. To have any kind of zombie game now that wows you’d need fields of zombies as far as the eye reaches. Hordes you nuke with bombs, leaving such a field of devastation the player just laughs at the sheer absurdity and leaves a “11/10 – nuked 900 zombies to orbit” Steam review.

And here ECS and Burst or new compiler tech in general will help us as they are specifically made to handle scale.

Making games is fun. Fiddling around, weirdly optimizing is not and also has no future if you think about productivity.

One thought on “Factorio in Unity ECS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s