DEV Community

Cover image for Rust Entity Component Systems: ECS Libraries for Rust Game Dev
Rodney Lab
Rodney Lab

Posted on • Originally published at rodneylab.com

Rust Entity Component Systems: ECS Libraries for Rust Game Dev

🦀 Rust Entity Component Systems

Here, we will have a look at some Rust Entity Component System (ECS) libraries, and also, resources for finding out more about ECS generally and building your own ECS. I am new to Rust game dev and documenting what I learn as I progress.

In a recent post, I went through some options for Rust game development engines. In that post, we looked at Godot and Bevy, which are both build around ECSs. We look at the Bevy ECS later, here, as you can use it with other game engines, independently of Bevy. Most of the other engines considered, though, did not have an ECS built in, so we look at some alternatives here.

🧩 What is an ECS?

Before looking at whether you need an Entity Component System for your game, let’s first see what an ECS is!

Components

Entity component systems provide structures and operations for managing game elements. The three main elements are entities, components, and systems. We’ll look at components first. Components, in an ECS, hold data on some actor (or game object). The actor might be a player or non-player character (NPC), or even some inanimate object, like a door.

You might want each of those actors to be able to interact with the game world in different ways. Both the player and the door might have a collision box represented as data components, used to help us update game state when the player goes through the door. The player and NPC might have velocity components holding data on their current speed and direction, so we can move them by the right amount, over each iteration of the game loop.

Entities

Entities are data too; they are collections of logically associated component instances. A player entity might have position, velocity, and collision components. Then, the door might just need position and collision components. From the ECS perspective, an entity is essentially an ID or handle on a game object, linking its components.

Systems

Systems can be thought of as functions that manipulate component instance data to update them. As an example, a movement system could take current position and velocity data to update actor positions. That system can loop through all entities that have a velocity, and position component, but ignore any entities that do not have both of these.

Resources and Schedulers

It doesn’t end with entities, components, and systems! Typically, we will have a game world, resources (for managing global properties, such as frame timing) and schedulers too. You might use schedulers, to help ensure that systems run in a certain, logical order, or after some, particular, change in the game state.

Resources for Learning more about ECS

That is just a quick introduction to ECS, and if you are new to them, you will probably want to explore some of these resources to learn more:

  • The Specs Book takes you through adding an ECS to your game, and includes a good introduction to ECS in general.
  • Using Rust For Game Development keynote speech by Catherine West with a longer look at how the ECS approach benefits versus an object-oriented approach, and also some alternatives to an ECS for a smaller game.
  • ECS FAQ deep dive into ECS from the creator of the C/C++ flecs ECS.

😕 Why use an ECS?

The main reason for opting for an ECS approach over object-oriented is probably code maintainability. The ECS philosophy is composition over inheritance, and the approach makes it easier to add extra components later, efficiently. The ECS approach can also save on code duplication for similar objects. Finally, there can be runtime performance benefits, for games with large numbers of entities and types, working in some languages.

🧱 Building your own Rust ECS

Entity Component Systems can be performance critical elements of your game, and they are not easy to write, so why try? In the previous, Rust game engine post, I mentioned that you probably don’t want to build a game and a game engine! Similarly, if you are building a game, you probably don’t either want to build an ECS for it. That said, you might want to run through a quick tutorial on building an ECS, just to get a better understanding of how they work.

You might also want a basic ECS for your small game, rather than integrating one of the libraries mentioned below.

In either case, see the Writing a tiny Entity Component System in Rust tutorial, by Ian Kettlewell to get going!

Rust Entity Component System Libraries

Here, we look at game development ECSs written in Rust. In this section:

  1. Bevy ECS;
  2. hecs;
  3. Shipyard; and
  4. Specs;

Rust ECS: Bevy ECS

Performance and ergonomics focussed ECS

Bevy is probably the most popular Rust game development engine. It takes a data-driven approach, centred on the ECS. As mentioned, earlier, you can use Bevy’s ECS with other game engines. Bevy ECS aims to be ergonomic, using normal Rust data types, and no need or complex lifetimes, traits or builder patterns. It has Entities, Components, Worlds, Resources, Schedules, and Systems.

Learn more about Bevy here:

Rust ECS: hecs

Minimalist Rust ECS

hecs is a minimalist ECS, considered more a library than a framework.

Learn more about hecs here:

Rust ECS: Shipyard

Rust ECS for highly parallel processes

Shipyard focusses on usability and speed. Shipyard’s data structure is inspired by the C++ EnTT ECS library’s core data-structure. There is a book and fun, little online game (links to book and game code below) to help you get going quickly.

Learn more about Shipyard here:

Rust ECS: Specs

Early Rust ECS, still popular

Specs has a classic ECS design and was responsible for popularizing the ECS pattern in Rust.

Rust ECS: Legion

Legion was intended as a replacement for specs in the amethyst game engine, though neither amethyst nor legion are currently maintained. See the link to the detailed look at Specs and Legion above for more on Legion.

Rust Bindings for C++ ECSs: Flecs

There are Rust bindings for the popular C++ ECS flecs.

🙌🏽 Rust Entity Component Systems: Wrapping Up

In this Rust Entity Component Systems post, we got an introduction to ECS and, more specifically, Rust ECS. In particular, we saw:

  • an overview of the ECS pattern;
  • some ECS learning resources; and
  • four actively maintained Rust ECS libraries.

I would love to hear from you, if you are also new to Rust game development. Do you have alternative resources you found useful? Is there something missing here that you would have added. Equally, if you are a veteran of the ecosystem and have some pointers, I would also like to hear from you. Let me know if you decide not to go down the ECS route, or even if you plan to build your own.

🙏🏽 Rust Entity Component Systems: Feedback

If you have found this post useful, see links below for further related content on this site. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on X, giving me a mention, so I can see what you did. Finally, be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on X (previously Twitter) and also, join the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Game Dev as well as Rust and C++ (among other topics). Also, subscribe to the newsletter to keep up-to-date with our latest projects.

Top comments (0)