@@ -267,24 +267,41 @@ pub fn machine_ticker<P: IntoScriptPluginParams>(world: &mut World) {
267267}
268268/// A command which triggers the script processing pipeline to run once,
269269/// causing outstanding attachment events to be processed
270- pub struct RunProcessingPipelineOnce < P > ( PhantomData < fn ( P ) > ) ;
270+ pub struct RunProcessingPipelineOnce < P > {
271+ override_budget : Option < Duration > ,
272+ _ph : PhantomData < fn ( P ) > ,
273+ }
271274
272275impl < P > RunProcessingPipelineOnce < P > {
273- /// Creates a new [`RunProcessingPipelineOnce`] command for the given plugin
274- pub fn new ( ) -> Self {
275- Self ( Default :: default ( ) )
276+ /// Creates a new [`RunProcessingPipelineOnce`] command for the given plugin.
277+ /// The budget override can be used to make sure all scripts in the pipeline get fully processed in one go
278+ pub fn new ( override_budget : Option < Duration > ) -> Self {
279+ Self {
280+ override_budget,
281+ _ph : PhantomData ,
282+ }
276283 }
277284}
278285
279286impl < P > Default for RunProcessingPipelineOnce < P > {
280287 fn default ( ) -> Self {
281- Self :: new ( )
288+ Self :: new ( None )
282289 }
283290}
284291
285292impl < P : IntoScriptPluginParams > Command for RunProcessingPipelineOnce < P > {
286293 fn apply ( self , world : & mut World ) {
294+ let mut last_setting = None ;
295+ if let Some ( override_budget) = self . override_budget {
296+ let mut machines = world. get_resource_or_init :: < ActiveMachines < P > > ( ) ;
297+ last_setting = Some ( machines. budget ) ;
298+ machines. budget = Some ( override_budget)
299+ }
287300 world. run_schedule ( ScriptProcessingSchedule :: < P > :: default ( ) ) ;
301+ if let Some ( last_setting) = last_setting {
302+ let mut machines = world. get_resource_or_init :: < ActiveMachines < P > > ( ) ;
303+ machines. budget = last_setting;
304+ }
288305 }
289306}
290307
@@ -326,7 +343,7 @@ pub fn automatic_pipeline_runner<P: IntoScriptPluginParams>(world: &mut World) {
326343 . get_resource :: < ActiveMachines < P > > ( )
327344 . is_some_and ( |machines| machines. active_machines ( ) > 0 )
328345 {
329- RunProcessingPipelineOnce :: < P > :: new ( ) . apply ( world) ;
346+ RunProcessingPipelineOnce :: < P > :: new ( None ) . apply ( world) ;
330347 }
331348}
332349
@@ -353,8 +370,12 @@ impl PipelineRun for App {
353370#[ cfg( test) ]
354371mod test {
355372 use bevy_asset:: { AssetApp , AssetId , AssetPlugin } ;
356- use bevy_ecs:: { system:: SystemState , world:: FromWorld } ;
373+ use bevy_ecs:: { entity :: Entity , system:: SystemState , world:: FromWorld } ;
357374 use bevy_mod_scripting_asset:: Language ;
375+ use bevy_mod_scripting_bindings:: ScriptValue ;
376+ use test_utils:: make_test_plugin;
377+
378+ use crate :: config:: { GetPluginThreadConfig , ScriptingPluginConfiguration } ;
358379
359380 use super :: * ;
360381 #[ test]
@@ -404,4 +425,25 @@ mod test {
404425 assert ! ( loaded. is_empty( ) )
405426 }
406427 }
428+
429+ make_test_plugin ! ( crate ) ;
430+
431+ #[ test]
432+ fn test_run_override_is_undid ( ) {
433+ let mut app = App :: default ( ) ;
434+ app. add_event :: < ScriptAttachedEvent > ( ) ;
435+
436+ app. add_plugins ( ( AssetPlugin :: default ( ) , TestPlugin :: default ( ) ) ) ;
437+ app. init_asset :: < ScriptAsset > ( ) ;
438+ let mut machines = ActiveMachines :: < TestPlugin > :: default ( ) ;
439+ machines. budget = None ;
440+ app. insert_resource ( machines) ;
441+ app. finish ( ) ;
442+
443+ let world = app. world_mut ( ) ;
444+ RunProcessingPipelineOnce :: < TestPlugin > :: new ( Some ( Duration :: from_secs ( 1 ) ) ) . apply ( world) ;
445+
446+ let machines = world. get_resource :: < ActiveMachines < TestPlugin > > ( ) . unwrap ( ) ;
447+ assert_eq ! ( machines. budget, None ) ;
448+ }
407449}
0 commit comments