-
|
Struggling for a good title and concise explanation of this but here goes: I'm only using the There's a function I want to pass that impl CtxRef {
pub fn new() -> Self {
CtxRef {
ctx: std::ptr::null_mut(),
}
}
pub fn set(&mut self, r: *mut Rltk) {
self.ctx = r;
}
pub fn get(&mut self) -> *mut Rltk {
self.ctx
}
}
the_world_setup_function() {
let mut render_stage = SystemStage::single_threaded();
render_stage.add_system(draw_map.exclusive_system().label("map_render"));
render_stage.add_system(draw_renderables.exclusive_system().after("map_render"));
schedule.add_stage_after(RunStage::Main, RunStage::Render, render_stage);
world.insert_resource(map);
world.insert_non_send(CtxRef::new());
}
impl GameState for State {
fn tick(&mut self, ctx: &mut Rltk) {
ctx.cls(); // clear screen
player_input(self, ctx); // receive input data
let mut ctxref = self.ecs.get_non_send_resource_mut::<CtxRef>().unwrap();
ctxref.set(ctx); // todo: only needs to be set once.
self.schedule.run(&mut self.ecs);
}
}
snippet_of_a_render_sys(
mut ctx_ref: NonSendMut<CtxRef>,
positions: Query<(&Position, &Renderable)>,
viewshed: Query<&Viewshed, With<Player>>,
) {
let player_viewshed = viewshed.single().unwrap();
for (position, render) in positions.iter() {
// ... stuff omitted
let ctx = ctx_ref.get();
unsafe {
(*ctx).set(position.x, position.y, render.fg, render.bg, render.glyph);
}
}
}Using Hoping for some guidance to improve the code if possible. Much appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
We've had someone asking for this before (although it was called By the way, an alternative is to use a commands like system. Something like: struct RltkCommands(Vec<Box<dyn FnMut(&mut Rltk + Send + Sync + 'static)>>);
//.. in snippet_of_a_render_sys - assuming you have made the trivial commands
my_res_rltk_commands.push(move |ctx| ctx.set(position.x, position.y, render.fg, render.bg, render.glyph));
// in GameState:
let mut commands = self.ecs.get_resource_mut::<RltkCommands>().unwrap();
for command in commands{
(*command)(ctx)
}Obviously you might need to do some surplus clones to make your commands non-'static. |
Beta Was this translation helpful? Give feedback.
We've had someone asking for this before (although it was called
Contextin that case).Basically, your solution is safe, but we definitely could add a wrapper for it - interestingly we could make this non-
bevy_ecsexclusive using some privacy cleverness - i.e. it could be a third party crate.By the way, an alternative is to use a commands like system. Something like: