Skip to content

Commit 5a5f89b

Browse files
committed
wait until scripts loaded fully in benchmarks
1 parent d28d4aa commit 5a5f89b

File tree

3 files changed

+34
-3
lines changed
  • crates

3 files changed

+34
-3
lines changed

crates/bevy_mod_scripting_core/src/pipeline/machines.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ impl<P: IntoScriptPluginParams> ActiveMachines<P> {
178178
internal_state: MachineExecutionState::Initialized(Box::new(state)),
179179
});
180180
}
181+
182+
/// Returns the amount of active machines
183+
pub fn active_machines(&self) -> usize {
184+
self.machines.len()
185+
}
181186
}
182187

183188
/// A machine, which combines the inputs to its states with the state of the world and generates state transitions,

crates/bevy_mod_scripting_core/src/pipeline/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,39 @@ impl<P> Default for RequestProcessingPipelineRun<P> {
317317
}
318318
}
319319

320-
/// A system which runs [`RunProcessingPipelineOnce`] command for the plugin only if [`RequestProcessingPipelineRun`] resource had [`RequestProcessingPipelineRun::request_run`] called on it
320+
/// A system which runs [`RunProcessingPipelineOnce`] command for the plugin only if [`RequestProcessingPipelineRun`] resource had [`RequestProcessingPipelineRun::request_run`] called on it,
321+
/// and or if there are active machines
321322
pub fn automatic_pipeline_runner<P: IntoScriptPluginParams>(world: &mut World) {
322323
let mut res = world.get_resource_or_init::<RequestProcessingPipelineRun<P>>();
323-
if res.get_and_unset() {
324+
if res.get_and_unset()
325+
|| world
326+
.get_resource::<ActiveMachines<P>>()
327+
.is_some_and(|machines| machines.active_machines() > 0)
328+
{
324329
RunProcessingPipelineOnce::<P>::new().apply(world);
325330
}
326331
}
327332

333+
/// A helper trait for App implementations
334+
pub trait PipelineRun {
335+
/// Runs update's until all scripts are processed and no more machines are running
336+
fn update_until_all_scripts_processed<P: IntoScriptPluginParams>(&mut self);
337+
}
338+
339+
impl PipelineRun for App {
340+
fn update_until_all_scripts_processed<P: IntoScriptPluginParams>(&mut self) {
341+
loop {
342+
let world = self.world_mut();
343+
let machines = world.get_resource::<ActiveMachines<P>>();
344+
let has_active = machines.is_some_and(|machines| machines.active_machines() > 0);
345+
if !has_active {
346+
break;
347+
}
348+
self.update();
349+
}
350+
}
351+
}
352+
328353
#[cfg(test)]
329354
mod test {
330355
use bevy_asset::{AssetApp, AssetId, AssetPlugin};

crates/testing_crates/script_integration_test_harness/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use bevy_mod_scripting_core::{
3232
BMSScriptingInfrastructurePlugin, IntoScriptPluginParams,
3333
commands::AttachScript,
3434
error::ScriptError,
35+
pipeline::PipelineRun,
3536
script::{DisplayProxy, ScriptAttachment, ScriptComponent, ScriptContext},
3637
};
3738
use bevy_mod_scripting_functions::ScriptFunctionsPlugin;
@@ -309,7 +310,7 @@ where
309310
}
310311
}
311312

312-
app.update();
313+
app.update_until_all_scripts_processed::<P>();
313314

314315
let script_contexts = app
315316
.world_mut()

0 commit comments

Comments
 (0)