Skip to content

Instantly share code, notes, and snippets.

@torkleyy
Created August 7, 2017 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torkleyy/4cb4c65f5efa01ba682958151dae55d1 to your computer and use it in GitHub Desktop.
Save torkleyy/4cb4c65f5efa01ba682958151dae55d1 to your computer and use it in GitHub Desktop.
Creating entities in Specs
impl<'a> System<'a> for EntitiyCreator {
type SystemData = (Entities<'a>, WriteStorage<'a, Pos>);
fn run(&mut self, (entities, pos): Self::SystemData) {
let a = entities.create();
let b = entities.create();
pos.insert(a, Pos::new(2, 4));
pos.insert(b, Pos::new(3, 5));
}
}
// Now, if you want to insert many components for these new entities,
// it might be better to use `LazyUpdate` in order to avoid fetching many
// storages mutably:
impl<'a> System<'a> for LazyCreator {
type SytemData = (Entities<'a>, Fetch<'a, LazyUpdate>);
fn run(&mut self, (entities, lazy): Self::SystemData) {
let a = entities.create();
// These updates will be applied at the end of this frame
lazy.insert(a, Pos::new(2, 5));
lazy.insert(a, Vel::new(0, 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment