Why does the SystemState interface accept World in new
and then again in get
/get_mut
?
#11724
-
SystemState's code looks like this: pub fn new(world: &mut World) -> Self {
let mut meta = SystemMeta::new::<Param>();
meta.last_run = world.change_tick().relative_to(Tick::MAX);
let param_state = Param::init_state(world, &mut meta);
Self {
meta,
param_state,
world_id: world.id(),
archetype_generation: ArchetypeGeneration::initial(),
}
}
/// Retrieve the [`SystemParam`] values. This can only be called when all parameters are read-only.
#[inline]
pub fn get<'w, 's>(&'s mut self, world: &'w World) -> SystemParamItem<'w, 's, Param>
where
Param: ReadOnlySystemParam,
{
self.validate_world(world.id());
self.update_archetypes(world);
// SAFETY: Param is read-only and doesn't allow mutable access to World.
// It also matches the World this SystemState was created with.
unsafe { self.get_unchecked_manual(world.as_unsafe_world_cell_readonly()) }
} This interface allows for the caller to provide a different world to
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
World access is required during system state initialization in order to generate all of the metadata needed to set up the system machinery. World acccess is required when using system state in order to actually get the data from the world. We can't look it up by world_id, because that's just a dumb identifier: we need some sort of reference to the world data. That said, we could improve boilerplate here by adding methods to initialize and get the system state immediately. The |
Beta Was this translation helpful? Give feedback.
World access is required during system state initialization in order to generate all of the metadata needed to set up the system machinery.
World acccess is required when using system state in order to actually get the data from the world. We can't look it up by world_id, because that's just a dumb identifier: we need some sort of reference to the world data.
That said, we could improve boilerplate here by adding methods to initialize and get the system state immediately. The
SystemState
won't be cached anywhere (which is a nice micro-optimization), but it reduces the annoyance for cold work.World::get_system_state
would probably be my preference; there's no point adding an immutable for…