Skip to content

Commit 9c830b1

Browse files
zwazelmakspll
andauthored
Small cleaning up + Fix for crash when looping through empty Vec (#87)
* dwadwa * debug prints * we ignore this ever happened * mmmmmmmmmmm i'm still a bit dumb lol * does this fix the crash? * that maybe fixes it * unncessecary get_children + fixed get_parent * rustfmt * clippy * moved wrappers example into lua and renamed it * WIP new example for rhai wrapper * WIP implementing fix mentioned in review #87 (comment) * basic one shot works * accessing variables work * accessing variables not work * Revert "accessing variables not work" This reverts commit c282ada. * empty array * go back to <= * commented out errors, setting values seem to work * expanded multiple_events example for parent testing * returning UNIT instead of int if parent couldn't be found * Update bevy_script_api/src/common/std.rs Co-authored-by: Maksymilian Mozolewski <makspl17@gmail.com> * Update bevy_script_api/src/common/std.rs Co-authored-by: Maksymilian Mozolewski <makspl17@gmail.com> * no longer problem at looping through empty vec * if/else to check if parent exists * removed rhai wrapper example, moved normal wrapper example back and renamed * rust fmt --------- Co-authored-by: Maksymilian Mozolewski <makspl17@gmail.com>
1 parent eb691a2 commit 9c830b1

File tree

5 files changed

+52
-34
lines changed

5 files changed

+52
-34
lines changed

assets/scripts/multiple_events_rhai.rhai

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
fn on_init(name) {
22
print(`Hello World! From "${name}" in Init`);
3+
4+
let parent = world.get_parent(entity);
5+
if parent == () {
6+
print(`Parent doesn't exist`);
7+
} else {
8+
print(`Parent exists`);
9+
}
310
}
411

512
fn on_update(name, delta) {

bevy_mod_scripting_core/src/hosts.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -360,22 +360,25 @@ impl<T: Asset> Script<T> {
360360
event_writer: &mut EventWriter<ScriptLoaded>,
361361
) {
362362
debug!("reloading script {}", script.id);
363-
// retrieve owning entity
364-
let entity = contexts.script_owner(script.id()).unwrap();
365-
366-
// remove old context
367-
contexts.remove_context(script.id());
368363

369-
// insert new re-loaded context
370-
Self::insert_new_script_context::<H>(
371-
host,
372-
script,
373-
entity,
374-
script_assets,
375-
providers,
376-
contexts,
377-
event_writer,
378-
);
364+
// retrieve owning entity
365+
if let Some(entity) = contexts.script_owner(script.id()) {
366+
// remove old context
367+
contexts.remove_context(script.id());
368+
// insert new re-loaded context
369+
Self::insert_new_script_context::<H>(
370+
host,
371+
script,
372+
entity,
373+
script_assets,
374+
providers,
375+
contexts,
376+
event_writer,
377+
);
378+
} else {
379+
// remove old context
380+
contexts.remove_context(script.id());
381+
}
379382
}
380383

381384
/// checks if a script has loaded, and if so loads (`ScriptHost::load_script`),

bevy_script_api/src/common/std.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ impl<T> From<ScriptVec<T>> for ScriptRef {
102102

103103
pub struct ScriptVecIterator<T> {
104104
current: usize,
105-
end: usize,
105+
len: usize,
106106
base: ScriptVec<T>,
107107
}
108108

109109
impl<T: FromReflect> Iterator for ScriptVecIterator<T> {
110110
type Item = ScriptRef;
111111

112112
fn next(&mut self) -> Option<Self::Item> {
113-
let nxt = (self.current <= self.end).then(|| self.base.index(self.current));
113+
let nxt = (self.current < self.len).then(|| self.base.index(self.current));
114114
self.current += 1;
115115
nxt
116116
}
@@ -131,10 +131,8 @@ impl<T: FromReflect + TypePath> IntoIterator for ScriptVec<T> {
131131
// TODO?: end used to be an Option, and this check moved into the next method but
132132
// I am not sure if this will ever realistically fail, so if you do get this exception happening
133133
// hit me with an issue
134-
end: self
135-
.len()
136-
.map(|v| v - 1)
137-
.expect("Could not access length when creating iterator"),
134+
// if len > 0, subtract 1, otherwise set to 0
135+
len: self.len().expect("Failed to get length of ScriptVec"),
138136
base: self,
139137
}
140138
}

bevy_script_api/src/rhai/bevy/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ impl CustomType for ScriptWorld {
4040
.map(Dynamic::from)
4141
.unwrap_or_default()
4242
})
43-
.with_fn("get_children", |self_: ScriptWorld, parent: Entity| {
44-
self_
45-
.get_children(parent)
46-
.into_iter()
47-
.map(Dynamic::from)
48-
.collect::<Vec<_>>()
49-
})
5043
.with_fn(
5144
"add_default_component",
5245
|self_: ScriptWorld, entity: Entity, type_registration: ScriptTypeRegistration| {
@@ -138,15 +131,19 @@ impl CustomType for ScriptWorld {
138131
})
139132
},
140133
)
141-
.with_fn("get_children", |self_: &ScriptWorld, parent: Entity| {
134+
.with_fn("get_parent", |self_: ScriptWorld, entity: Entity| {
135+
if let Some(parent) = self_.get_parent(entity) {
136+
Dynamic::from(parent)
137+
} else {
138+
Dynamic::UNIT
139+
}
140+
})
141+
.with_fn("get_children", |self_: ScriptWorld, parent: Entity| {
142142
self_
143143
.get_children(parent)
144144
.into_iter()
145145
.map(Dynamic::from)
146-
.collect::<Vec<Dynamic>>()
147-
})
148-
.with_fn("get_parent", |self_: &ScriptWorld, entity: Entity| {
149-
self_.get_parent(entity)
146+
.collect::<Vec<_>>()
150147
})
151148
.with_fn(
152149
"push_child",

examples/rhai/multiple_events_rhai.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,20 @@ impl FuncArgs for ScriptArgs {
6060
fn setup_entities(mut commands: Commands, asset_server: Res<AssetServer>) {
6161
let script_path = "scripts/multiple_events_rhai.rhai";
6262

63-
for i in 0..10 {
63+
commands.spawn_empty().with_children(|parent| {
64+
parent.spawn((
65+
NewlyAddedEntityCallInit,
66+
Name::from("Test Entity 0"),
67+
ScriptCollection::<RhaiFile> {
68+
scripts: vec![Script::new(
69+
script_path.to_owned(),
70+
asset_server.load(script_path),
71+
)],
72+
},
73+
));
74+
});
75+
76+
for i in 1..10 {
6477
let entity_name = format!("Test Entity {}", i);
6578
commands.spawn((
6679
NewlyAddedEntityCallInit,

0 commit comments

Comments
 (0)