@@ -112,6 +112,15 @@ pub(crate) fn event_handler_internal<L: IntoCallbackLabel, P: IntoScriptPluginPa
112
112
let entity_scripts = entities
113
113
. iter ( )
114
114
. map ( |( e, s) | ( e, s. 0 . clone ( ) ) )
115
+ . chain (
116
+ // on top of script components we also want to run static scripts
117
+ // semantically these are just scripts with no entity, in our case we use an invalid entity index 0
118
+ res_ctxt
119
+ . static_scripts
120
+ . scripts
121
+ . iter ( )
122
+ . map ( |s| ( Entity :: from_raw ( 0 ) , vec ! [ s. clone( ) ] ) ) ,
123
+ )
115
124
. collect :: < Vec < _ > > ( ) ;
116
125
117
126
for event in events
@@ -246,7 +255,7 @@ mod test {
246
255
event:: { CallbackLabel , IntoCallbackLabel , ScriptCallbackEvent , ScriptErrorEvent } ,
247
256
handler:: HandlerFn ,
248
257
runtime:: RuntimeContainer ,
249
- script:: { Script , ScriptComponent , ScriptId , Scripts } ,
258
+ script:: { Script , ScriptComponent , ScriptId , Scripts , StaticScripts } ,
250
259
} ;
251
260
252
261
use super :: * ;
@@ -298,6 +307,7 @@ mod test {
298
307
app. insert_resource :: < Scripts > ( Scripts { scripts } ) ;
299
308
app. insert_non_send_resource ( RuntimeContainer :: < P > { runtime } ) ;
300
309
app. insert_non_send_resource ( ScriptContexts :: < P > { contexts } ) ;
310
+ app. init_resource :: < StaticScripts > ( ) ;
301
311
app. insert_resource ( ContextLoadingSettings :: < P > {
302
312
loader : ContextBuilder {
303
313
load : |_, _, _, _, _| todo ! ( ) ,
@@ -484,4 +494,73 @@ mod test {
484
494
]
485
495
) ;
486
496
}
497
+
498
+ #[ test]
499
+ fn test_handler_called_for_static_scripts ( ) {
500
+ let test_script_id = Cow :: Borrowed ( "test_script" ) ;
501
+ let test_ctxt_id = 0 ;
502
+
503
+ let scripts = HashMap :: from_iter ( vec ! [ (
504
+ test_script_id. clone( ) ,
505
+ Script {
506
+ id: test_script_id. clone( ) ,
507
+ asset: None ,
508
+ context_id: test_ctxt_id,
509
+ } ,
510
+ ) ] ) ;
511
+ let contexts = HashMap :: from_iter ( vec ! [ (
512
+ test_ctxt_id,
513
+ TestContext {
514
+ invocations: vec![ ] ,
515
+ } ,
516
+ ) ] ) ;
517
+ let runtime = TestRuntime {
518
+ invocations : vec ! [ ] ,
519
+ } ;
520
+ let mut app = setup_app :: < OnTestCallback , TestPlugin > (
521
+ |args, entity, script, _, ctxt, _, runtime| {
522
+ ctxt. invocations . extend ( args) ;
523
+ runtime. invocations . push ( ( entity, script. clone ( ) ) ) ;
524
+ Ok ( ScriptValue :: Unit )
525
+ } ,
526
+ runtime,
527
+ contexts,
528
+ scripts,
529
+ ) ;
530
+
531
+ app. world_mut ( ) . insert_resource ( StaticScripts {
532
+ scripts : vec ! [ test_script_id. clone( ) ] . into_iter ( ) . collect ( ) ,
533
+ } ) ;
534
+
535
+ app. world_mut ( ) . send_event ( ScriptCallbackEvent :: new (
536
+ OnTestCallback :: into_callback_label ( ) ,
537
+ vec ! [ ScriptValue :: String ( "test_args_script" . into( ) ) ] ,
538
+ crate :: event:: Recipients :: All ,
539
+ ) ) ;
540
+
541
+ app. world_mut ( ) . send_event ( ScriptCallbackEvent :: new (
542
+ OnTestCallback :: into_callback_label ( ) ,
543
+ vec ! [ ScriptValue :: String ( "test_script_id" . into( ) ) ] ,
544
+ crate :: event:: Recipients :: Script ( test_script_id. clone ( ) ) ,
545
+ ) ) ;
546
+
547
+ app. update ( ) ;
548
+
549
+ let test_context = app
550
+ . world ( )
551
+ . get_non_send_resource :: < ScriptContexts < TestPlugin > > ( )
552
+ . unwrap ( ) ;
553
+
554
+ assert_eq ! (
555
+ test_context
556
+ . contexts
557
+ . get( & test_ctxt_id)
558
+ . unwrap( )
559
+ . invocations,
560
+ vec![
561
+ ScriptValue :: String ( "test_args_script" . into( ) ) ,
562
+ ScriptValue :: String ( "test_script_id" . into( ) )
563
+ ]
564
+ ) ;
565
+ }
487
566
}
0 commit comments