Skip to content

Commit 6ec1f4d

Browse files
committed
more tests and cleanup
1 parent 6a95cf5 commit 6ec1f4d

File tree

8 files changed

+52
-32
lines changed

8 files changed

+52
-32
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local NewComponent = world.register_new_component("ScriptComponentA")
2+
local entity = world.spawn()
3+
4+
assert(world.has_component(entity, NewComponent) == false, "Entity should not have component")
5+
world.add_default_component(entity, NewComponent)
6+
assert(world.has_component(entity, NewComponent) == true, "Entity should have component")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let NewComponent = world.register_new_component.call("ScriptComponentA");
2+
let entity = world.spawn_.call();
3+
4+
assert(world.has_component.call(entity, NewComponent) == false, "Entity should not have component");
5+
world.add_default_component.call(entity, NewComponent);
6+
assert(world.has_component.call(entity, NewComponent) == true, "Entity should have component");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local NewComponent = world.register_new_component("ScriptComponentA")
2+
local new_entity = world.spawn()
3+
world.add_default_component(new_entity, NewComponent)
4+
5+
local component_instance = world.get_component(new_entity, NewComponent)
6+
assert(component_instance ~= nil, "unexpected value: " .. tostring(component_instance.data))
7+
8+
world.remove_component(new_entity, NewComponent)
9+
local component_instance = world.get_component(new_entity, NewComponent)
10+
11+
assert(component_instance == nil, "unexpected value: " .. tostring(component_instance))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let NewComponent = world.register_new_component.call("ScriptComponentA");
2+
let new_entity = world.spawn_.call();
3+
world.add_default_component.call(new_entity, NewComponent);
4+
5+
let component_instance = world.get_component.call(new_entity, NewComponent);
6+
assert(type_of(component_instance) != "()", "unexpected value: " + component_instance.data);
7+
8+
world.remove_component.call(new_entity, NewComponent);
9+
let component_instance_after = world.get_component.call(new_entity, NewComponent);
10+
11+
assert(type_of(component_instance_after) == "()", "unexpected value: " + component_instance_after);

crates/bevy_mod_scripting_core/src/bindings/query.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ impl ScriptComponentRegistration {
129129
self.registration
130130
}
131131

132+
/// Removes an instance of this component from the given entity
133+
pub fn remove_from_entity(
134+
&self,
135+
world: WorldGuard,
136+
entity: Entity,
137+
) -> Result<(), InteropError> {
138+
world.with_global_access(|world| {
139+
let mut entity = world
140+
.get_entity_mut(entity)
141+
.map_err(|_| InteropError::missing_entity(entity))?;
142+
entity.remove_by_id(self.component_id);
143+
Ok(())
144+
})?
145+
}
146+
132147
/// Inserts an instance of this component into the given entity
133148
///
134149
/// Requires whole world access

crates/bevy_mod_scripting_core/src/bindings/script_component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use bevy::{
1414
use parking_lot::RwLock;
1515
use std::{alloc::Layout, mem::needs_drop, sync::Arc};
1616

17-
/// A dynamic script component, with script set
17+
/// A dynamic script component
1818
#[derive(Reflect, Clone, Default)]
1919
#[reflect(Default)]
2020
pub struct ScriptComponent {

crates/bevy_mod_scripting_core/src/bindings/world.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use bevy::{
3434
ecs::{
3535
component::{Component, ComponentId},
3636
entity::Entity,
37-
reflect::{AppTypeRegistry, ReflectComponent, ReflectFromWorld, ReflectResource},
37+
reflect::{AppTypeRegistry, ReflectFromWorld, ReflectResource},
3838
system::{Commands, Resource},
3939
world::{unsafe_world_cell::UnsafeWorldCell, CommandQueue, Mut, World},
4040
},
@@ -934,17 +934,14 @@ impl WorldAccessGuard<'_> {
934934
.type_registration()
935935
.data::<ReflectDefault>()
936936
{
937-
bevy::log::debug!("found default data");
938937
default_td.default()
939938
} else if let Some(from_world_td) = registration
940939
.type_registration()
941940
.type_registration()
942941
.data::<ReflectFromWorld>()
943942
{
944-
bevy::log::debug!("found reflect from world");
945943
self.with_global_access(|world| from_world_td.from_world(world))?
946944
} else {
947-
bevy::log::debug!("found neither");
948945
return Err(InteropError::missing_type_data(
949946
registration.registration.type_id(),
950947
"ReflectDefault or ReflectFromWorld".to_owned(),
@@ -988,18 +985,11 @@ impl WorldAccessGuard<'_> {
988985
.get_entity(entity)
989986
.ok_or_else(|| InteropError::missing_entity(entity))?;
990987

991-
bevy::log::debug!("Retrieving component with component id: {:?}", component_id);
992-
993988
let component_info = cell
994989
.components()
995990
.get_info(component_id)
996991
.ok_or_else(|| InteropError::invalid_component(component_id))?;
997992

998-
bevy::log::debug!(
999-
"Retrieved component with component info: {:?}",
1000-
component_info
1001-
);
1002-
1003993
if entity.contains_id(component_id) {
1004994
let type_id = component_info.type_id().or_else(|| {
1005995
// check its a script component
@@ -1049,25 +1039,7 @@ impl WorldAccessGuard<'_> {
10491039
entity: Entity,
10501040
registration: ScriptComponentRegistration,
10511041
) -> Result<(), InteropError> {
1052-
let component_data = registration
1053-
.type_registration()
1054-
.type_registration()
1055-
.data::<ReflectComponent>()
1056-
.ok_or_else(|| {
1057-
InteropError::missing_type_data(
1058-
registration.registration.type_id(),
1059-
"ReflectComponent".to_owned(),
1060-
)
1061-
})?;
1062-
1063-
// TODO: this shouldn't need entire world access it feels
1064-
self.with_global_access(|world| {
1065-
let mut entity = world
1066-
.get_entity_mut(entity)
1067-
.map_err(|_| InteropError::missing_entity(entity))?;
1068-
component_data.remove(&mut entity);
1069-
Ok(())
1070-
})?
1042+
registration.remove_from_entity(self.clone(), entity)
10711043
}
10721044

10731045
/// get the given resource

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ fn once_per_app_finalize(app: &mut App) {
252252
if app.world().contains_resource::<BMSFinalized>() {
253253
return;
254254
}
255-
println!("Running init");
256255
app.insert_resource(BMSFinalized);
257256

258257
// read extensions from asset settings

0 commit comments

Comments
 (0)