-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best practice for borrowed resources in systems? #757
Comments
I think I'm missing some context here, but if you just need mutable access to something from a system, that shouldn't be a problem, you can just use Some games / game engines even use a second |
Definitely lacks context, my fault. I will try to get a minimum-viable example (I tried https://play.rust-lang.org but The problem is not access from the system, but insertion into the world. pub struct MyGame {
world: World,
dispatcher: Dispatcher<'static, 'static>,
}
// This trait is from my framework
//
// Notice that `GameState` requires `Self: 'static` which is why my dispatcher is `<'static, 'static>`
impl GameState for MyGame {
fn tick(&mut self, ctx: &mut Foo) {
// Here `ctx` would escape out of the function
self.world.insert(ctx);
self.dispatcher.dispatch(&mut self.world);
self.world.maintain();
self.world.remove::<&mut Foo>();
}
} I think I can fix this by building the dispatcher once per tick and just storing the |
Oh are you forced to use the If you cannot get ownership of pub struct MyGame {
world: World,
dispatcher: Dispatcher<'static, 'static>,
}
// This trait is from my framework
//
// Notice that `GameState` requires `Self: 'static` which is why my dispatcher is `<'static, 'static>`
impl GameState for MyGame {
fn tick(&mut self, ctx: &mut Foo) {
self.dispatcher.dispatch(&mut self.world);
self.world.exec(|components: ReadStorage<Component>, other: WriteStorage<Other>| {
ctx.do_something();
});
self.world.maintain();
}
} or, if you want to put your rendering systems into the dispatcher, serialize required input from However, I recommend you check if you can get ownership of |
@torkleyy I instead used draw batch feature of bracket lib to do the console printing from the system. I just made a rendering dispatcher and run it within my draw method of whichever state I am in. https://github.com/lecoqjacob/blood_oath/blob/main/src/ecs/systems/render/renderer.rs |
I'm halfway through the Roguelike tutorial and I would like to move my render logic into a
System
and use aDispatcher
instead of manually running systems. In this casebracket-lib
has atick
where you mutably borrow aBTerm
instance, but it feels like this is a common pattern where some kind of context is mutably borrowed.What's the best practice here? Should we just build a dispatcher per tick and pass the context in the system struct?
The text was updated successfully, but these errors were encountered: