diff --git a/assets/scripts/multiple_events_rhai.rhai b/assets/scripts/multiple_events_rhai.rhai index a544c464..bc7c6680 100644 --- a/assets/scripts/multiple_events_rhai.rhai +++ b/assets/scripts/multiple_events_rhai.rhai @@ -1,5 +1,12 @@ fn on_init(name) { print(`Hello World! From "${name}" in Init`); + + let parent = world.get_parent(entity); + if parent == () { + print(`Parent doesn't exist`); + } else { + print(`Parent exists`); + } } fn on_update(name, delta) { diff --git a/bevy_mod_scripting_core/src/hosts.rs b/bevy_mod_scripting_core/src/hosts.rs index 773de01f..9e8ff585 100644 --- a/bevy_mod_scripting_core/src/hosts.rs +++ b/bevy_mod_scripting_core/src/hosts.rs @@ -360,22 +360,25 @@ impl Script { event_writer: &mut EventWriter, ) { debug!("reloading script {}", script.id); - // retrieve owning entity - let entity = contexts.script_owner(script.id()).unwrap(); - - // remove old context - contexts.remove_context(script.id()); - // insert new re-loaded context - Self::insert_new_script_context::( - host, - script, - entity, - script_assets, - providers, - contexts, - event_writer, - ); + // retrieve owning entity + if let Some(entity) = contexts.script_owner(script.id()) { + // remove old context + contexts.remove_context(script.id()); + // insert new re-loaded context + Self::insert_new_script_context::( + host, + script, + entity, + script_assets, + providers, + contexts, + event_writer, + ); + } else { + // remove old context + contexts.remove_context(script.id()); + } } /// checks if a script has loaded, and if so loads (`ScriptHost::load_script`), diff --git a/bevy_script_api/src/common/std.rs b/bevy_script_api/src/common/std.rs index b22ea73c..cbb0abda 100644 --- a/bevy_script_api/src/common/std.rs +++ b/bevy_script_api/src/common/std.rs @@ -102,7 +102,7 @@ impl From> for ScriptRef { pub struct ScriptVecIterator { current: usize, - end: usize, + len: usize, base: ScriptVec, } @@ -110,7 +110,7 @@ impl Iterator for ScriptVecIterator { type Item = ScriptRef; fn next(&mut self) -> Option { - let nxt = (self.current <= self.end).then(|| self.base.index(self.current)); + let nxt = (self.current < self.len).then(|| self.base.index(self.current)); self.current += 1; nxt } @@ -131,10 +131,8 @@ impl IntoIterator for ScriptVec { // TODO?: end used to be an Option, and this check moved into the next method but // I am not sure if this will ever realistically fail, so if you do get this exception happening // hit me with an issue - end: self - .len() - .map(|v| v - 1) - .expect("Could not access length when creating iterator"), + // if len > 0, subtract 1, otherwise set to 0 + len: self.len().expect("Failed to get length of ScriptVec"), base: self, } } diff --git a/bevy_script_api/src/rhai/bevy/mod.rs b/bevy_script_api/src/rhai/bevy/mod.rs index b75209e7..4ab540e1 100644 --- a/bevy_script_api/src/rhai/bevy/mod.rs +++ b/bevy_script_api/src/rhai/bevy/mod.rs @@ -40,13 +40,6 @@ impl CustomType for ScriptWorld { .map(Dynamic::from) .unwrap_or_default() }) - .with_fn("get_children", |self_: ScriptWorld, parent: Entity| { - self_ - .get_children(parent) - .into_iter() - .map(Dynamic::from) - .collect::>() - }) .with_fn( "add_default_component", |self_: ScriptWorld, entity: Entity, type_registration: ScriptTypeRegistration| { @@ -138,15 +131,19 @@ impl CustomType for ScriptWorld { }) }, ) - .with_fn("get_children", |self_: &ScriptWorld, parent: Entity| { + .with_fn("get_parent", |self_: ScriptWorld, entity: Entity| { + if let Some(parent) = self_.get_parent(entity) { + Dynamic::from(parent) + } else { + Dynamic::UNIT + } + }) + .with_fn("get_children", |self_: ScriptWorld, parent: Entity| { self_ .get_children(parent) .into_iter() .map(Dynamic::from) - .collect::>() - }) - .with_fn("get_parent", |self_: &ScriptWorld, entity: Entity| { - self_.get_parent(entity) + .collect::>() }) .with_fn( "push_child", diff --git a/examples/rhai/multiple_events_rhai.rs b/examples/rhai/multiple_events_rhai.rs index 9ba4beda..6ecf7c55 100644 --- a/examples/rhai/multiple_events_rhai.rs +++ b/examples/rhai/multiple_events_rhai.rs @@ -60,7 +60,20 @@ impl FuncArgs for ScriptArgs { fn setup_entities(mut commands: Commands, asset_server: Res) { let script_path = "scripts/multiple_events_rhai.rhai"; - for i in 0..10 { + commands.spawn_empty().with_children(|parent| { + parent.spawn(( + NewlyAddedEntityCallInit, + Name::from("Test Entity 0"), + ScriptCollection:: { + scripts: vec![Script::new( + script_path.to_owned(), + asset_server.load(script_path), + )], + }, + )); + }); + + for i in 1..10 { let entity_name = format!("Test Entity {}", i); commands.spawn(( NewlyAddedEntityCallInit,