Advice - Systems that talk to each other?
⚓ Rust 📅 2025-06-18 👤 surdeus 👁️ 16I often fiddle around with writing rather barebones game engines in different programming languages, and I've moved my attention back to Rust after a year or so of nearly exclusively using C++.
I began adapting some recent C++ code of mine to Rust, specifically a group of game Systems that may depend on each other:
// c++ code:
sprite::system sprites{};
animation::system animations{sprites}; // "mutable ref" to sprites system
actor::system actors{animations}; // "mutable ref" to animations system
roamer::system roamers{actors}; // "mutable ref" to actors system
// ... etc.
// later...
actors.update(); // Might update some state in Animations and Sprites.
animations.update(); // Might update some state in Sprites.
sprites.update(); // ...and so on
All these systems are rather simple - they're just structs containing a few data containers, e.g. a sprites: Vec<Sprite> in the sprites system, and some logic to update and manipulate them.
I'm finding that this code structure doesn't adapt well to rust. Namely:
- Rust is not a fan of structs holding mutable references to each other and also getting mutated in the outer scope (i.e. in update() fns).
- Iterating over one member of a system while mutating another is equally frustrating.
I've tried to get around this by making a sloppy sort of service locator. Each structure holds an immutable reference to this locator and uses locator.get::<System>().borrow_mut().whatever as needed.
None of this feels like the "right" way to structure this sort of application in rust, though. I feel like trying to write a group of interconnected systems has me fighting against rust's strict safety rules at every turn. Any thoughts on what the right path forward may be?
1 post - 1 participant
🏷️ rust_feed