Skip to content

Commit 2636295

Browse files
committed
bug: Fix rhea and static script unloading.
1 parent 324f9f3 commit 2636295

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

crates/bevy_mod_scripting_core/src/asset.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,13 @@ pub(crate) fn eval_script<P: IntoScriptPluginParams>(
384384
// }) {
385385
if let Some(script_id) = script_queue.pop_front() {
386386
if let Some(asset) = script_assets.get(script_id) {
387-
commands.queue(CreateOrUpdateScript::<P>::new(
388-
script_id,
389-
asset.content.clone(),
390-
Some(Handle::Weak(script_id)),
391-
));
387+
if asset.language == P::LANGUAGE {
388+
commands.queue(CreateOrUpdateScript::<P>::new(
389+
script_id,
390+
asset.content.clone(),
391+
Some(Handle::Weak(script_id)),
392+
));
393+
}
392394
} else {
393395
// This is probably a load failure. What to do? We've already
394396
// provided a warning on failure. Doing nothing is fine then we

crates/bevy_mod_scripting_core/src/commands.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ impl<P: IntoScriptPluginParams> CreateOrUpdateScript<P> {
8484
}
8585
}
8686

87+
fn script_name(&self) -> String {
88+
self.asset.as_ref().and_then(|handle| handle.path().map(|p| p.to_string())).unwrap_or_else(|| self.id.to_string())
89+
}
90+
8791
fn reload_context(
8892
&self,
8993
guard: WorldGuard,
@@ -215,7 +219,7 @@ impl<P: IntoScriptPluginParams> Command for CreateOrUpdateScript<P> {
215219
handle_script_errors(
216220
guard,
217221
vec![err
218-
.with_script(self.id.clone())
222+
.with_script(self.script_name())
219223
.with_context(P::LANGUAGE)
220224
.with_context(phrase)]
221225
.into_iter(),

examples/game_of_life.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ fn run_script_cmd(
5757
asset_server: Res<AssetServer>,
5858
mut script_handle: Local<Option<Handle<ScriptAsset>>>,
5959
script_comps: Query<Entity, With<ScriptComponent>>,
60-
mut static_scripts_created: Local<Vec<ScriptId>>,
60+
mut static_lua_scripts: Local<Vec<ScriptId>>,
61+
mut static_rhai_scripts: Local<Vec<ScriptId>>,
6162
) {
6263
if let Some(Ok(command)) = log.take() {
6364
match command {
@@ -75,7 +76,11 @@ fn run_script_cmd(
7576
} else {
7677
bevy::log::info!("Using static script instead of spawning an entity");
7778
let handle = asset_server.load(script_path);
78-
static_scripts_created.push(handle.id());
79+
if language == "lua" {
80+
static_lua_scripts.push(handle.id());
81+
} else {
82+
static_rhai_scripts.push(handle.id());
83+
}
7984
commands.queue(AddStaticScript::new(handle))
8085
}
8186
}
@@ -88,13 +93,17 @@ fn run_script_cmd(
8893
commands.entity(id).despawn();
8994
}
9095

91-
// you could also do
92-
for script_id in static_scripts_created.drain(..) {
96+
for script_id in static_lua_scripts.drain(..) {
9397
commands.queue(DeleteScript::<LuaScriptingPlugin>::new(
9498
script_id
9599
));
96100
}
97-
// as this will retain your script asset and handle
101+
102+
for script_id in static_rhai_scripts.drain(..) {
103+
commands.queue(DeleteScript::<RhaiScriptingPlugin>::new(
104+
script_id
105+
));
106+
}
98107
}
99108
}
100109
}

0 commit comments

Comments
 (0)