Skip to content

SystemBuilder: better handling of system creation with non-default kind while deferred #207

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 26 additions & 29 deletions flecs_ecs/src/addons/system/system_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ where
pub(crate) desc: sys::ecs_system_desc_t,
term_builder: TermBuilder,
world: WorldRef<'a>,
/// Skip setting default phase (`OnUpdate`) if `kind` was set,
/// or an existing entity was passed in (via [`Self::new_from_desc`])
kind_set: bool,
_phantom: core::marker::PhantomData<&'a T>,
}

Expand All @@ -26,23 +29,14 @@ where
desc: Default::default(),
term_builder: TermBuilder::default(),
world: world.into(),
kind_set: false,
_phantom: core::marker::PhantomData,
};

obj.desc.entity = unsafe { sys::ecs_entity_init(obj.world_ptr_mut(), &Default::default()) };

T::populate(&mut obj);

#[cfg(feature = "flecs_pipeline")]
unsafe {
sys::ecs_add_id(
world.world_ptr_mut(),
obj.desc.entity,
ecs_dependson(ECS_ON_UPDATE),
);
sys::ecs_add_id(world.world_ptr_mut(), obj.desc.entity, ECS_ON_UPDATE);
}

obj
}

Expand All @@ -51,26 +45,20 @@ where
desc,
term_builder: TermBuilder::default(),
world: world.into(),
kind_set: false,
_phantom: core::marker::PhantomData,
};

if obj.desc.entity == 0 {
obj.desc.entity =
unsafe { sys::ecs_entity_init(obj.world_ptr_mut(), &Default::default()) };
} else {
// Can't make assumptions about the kind on an existing entity.
obj.kind_set = true;
}

T::populate(&mut obj);

#[cfg(feature = "flecs_pipeline")]
unsafe {
sys::ecs_add_id(
world.world_ptr_mut(),
obj.desc.entity,
ecs_dependson(ECS_ON_UPDATE),
);
sys::ecs_add_id(world.world_ptr_mut(), obj.desc.entity, ECS_ON_UPDATE);
}

obj
}

Expand All @@ -82,6 +70,7 @@ where
desc: Default::default(),
term_builder: TermBuilder::default(),
world: world.into(),
kind_set: false,
_phantom: core::marker::PhantomData,
};

Expand All @@ -95,15 +84,6 @@ where

T::populate(&mut obj);

#[cfg(feature = "flecs_pipeline")]
unsafe {
sys::ecs_add_id(
world.world_ptr_mut(),
obj.desc.entity,
ecs_dependson(ECS_ON_UPDATE),
);
sys::ecs_add_id(world.world_ptr_mut(), obj.desc.entity, ECS_ON_UPDATE);
}
obj
}

Expand Down Expand Up @@ -134,6 +114,7 @@ where
if phase != 0 {
sys::ecs_add_id(self.world_ptr_mut(), self.desc.entity, ecs_dependson(phase));
sys::ecs_add_id(self.world_ptr_mut(), self.desc.entity, phase);
self.kind_set = true;
}
};
self
Expand Down Expand Up @@ -249,6 +230,22 @@ where
/// * C++ API: `node_builder::build`
#[doc(alias = "node_builder::build")]
fn build(&mut self) -> Self::BuiltType {
#[cfg(feature = "flecs_pipeline")]
if !self.kind_set {
unsafe {
sys::ecs_add_id(
self.world().world_ptr_mut(),
self.desc.entity,
ecs_dependson(ECS_ON_UPDATE),
);
sys::ecs_add_id(
self.world().world_ptr_mut(),
self.desc.entity,
ECS_ON_UPDATE,
);
}
}

let system = System::new(self.world(), self.desc);
for s in self.term_builder.str_ptrs_to_free.iter_mut() {
unsafe { core::mem::ManuallyDrop::drop(s) };
Expand Down
20 changes: 20 additions & 0 deletions flecs_ecs/tests/flecs/system_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,26 @@ fn system_custom_pipeline_w_kind() {
});
}

#[test]
fn system_kind_while_deferred() {
let world = World::new();

let sys = world.defer(|| {
world
.system::<()>()
.kind_id(flecs::pipeline::OnValidate::ID)
.run(|_| {})
});

world.progress();

let sys = sys.entity_view(&world);
assert!(sys.has_id(flecs::pipeline::OnValidate::ID));
assert!(sys.has_first::<flecs::DependsOn>(flecs::pipeline::OnValidate::ID));
assert!(!sys.has_id(flecs::pipeline::OnUpdate::ID));
assert!(!sys.has_first::<flecs::DependsOn>(flecs::pipeline::OnUpdate::ID));
}

#[test]
fn system_create_w_no_template_args() {
let world = World::new();
Expand Down