@@ -19,7 +19,7 @@ use bevy::{
19
19
system:: { Local , Resource , SystemState } ,
20
20
world:: { Mut , World } ,
21
21
} ,
22
- log:: trace_once,
22
+ log:: { self , trace_once} ,
23
23
prelude:: { Events , Ref } ,
24
24
} ;
25
25
@@ -108,13 +108,11 @@ macro_rules! push_err_and_continue {
108
108
/// If any of the resources required for the handler are missing, the system will log this issue and do nothing.
109
109
pub fn event_handler < L : IntoCallbackLabel , P : IntoScriptPluginParams > (
110
110
world : & mut World ,
111
- mut state : Local <
112
- SystemState < (
113
- Local < QueryState < ( Entity , Ref < ScriptComponent > ) > > ,
114
- EventReaderScope < ScriptCallbackEvent > ,
115
- WithWorldGuard < HandlerContext < P > > ,
116
- ) > ,
117
- > ,
111
+ state : & mut SystemState < (
112
+ Local < QueryState < ( Entity , Ref < ScriptComponent > ) > > ,
113
+ EventReaderScope < ScriptCallbackEvent > ,
114
+ WithWorldGuard < HandlerContext < P > > ,
115
+ ) > ,
118
116
) {
119
117
// we wrap the inner event handler, so that we can immediately re-insert all the resources back.
120
118
// otherwise this would happen in the next schedule
@@ -132,6 +130,12 @@ fn event_handler_inner<L: IntoCallbackLabel, P: IntoScriptPluginParams>(
132
130
mut handler_ctxt : WithWorldGuard < HandlerContext < P > > ,
133
131
) {
134
132
let ( guard, handler_ctxt) = handler_ctxt. get_mut ( ) ;
133
+ // bevy::log::debug!(
134
+ // "{}: scripts:{} contexts:{}",
135
+ // P::LANGUAGE,
136
+ // handler_ctxt.scripts.scripts.len(),
137
+ // handler_ctxt.script_contexts.contexts.len()
138
+ // );
135
139
let mut errors = Vec :: default ( ) ;
136
140
137
141
let events = script_events. read ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ;
@@ -183,15 +187,17 @@ fn event_handler_inner<L: IntoCallbackLabel, P: IntoScriptPluginParams>(
183
187
{
184
188
continue
185
189
}
186
- _ => ( ) ,
190
+ _ => { }
187
191
}
188
192
189
- match handler_ctxt. call :: < L > (
193
+ let call_result = handler_ctxt. call :: < L > (
190
194
script_id. clone ( ) ,
191
195
* entity,
192
196
event. args . clone ( ) ,
193
197
guard. clone ( ) ,
194
- ) {
198
+ ) ;
199
+
200
+ match call_result {
195
201
Ok ( _) => { }
196
202
Err ( e) => {
197
203
match e. downcast_interop_inner ( ) {
@@ -249,7 +255,12 @@ pub fn handle_script_errors<I: Iterator<Item = ScriptError> + Clone>(world: Worl
249
255
mod test {
250
256
use std:: { borrow:: Cow , collections:: HashMap } ;
251
257
252
- use bevy:: app:: { App , Update } ;
258
+ use bevy:: {
259
+ app:: { App , Update } ,
260
+ asset:: AssetPlugin ,
261
+ diagnostic:: DiagnosticsPlugin ,
262
+ ecs:: world:: FromWorld ,
263
+ } ;
253
264
use test_utils:: make_test_plugin;
254
265
255
266
use crate :: {
@@ -545,4 +556,33 @@ mod test {
545
556
]
546
557
) ;
547
558
}
559
+
560
+ #[ test]
561
+ fn event_handler_reinserts_resources ( ) {
562
+ let mut app = App :: new ( ) ;
563
+ app. add_plugins ( (
564
+ AssetPlugin :: default ( ) ,
565
+ DiagnosticsPlugin ,
566
+ TestPlugin :: default ( ) ,
567
+ ) ) ;
568
+
569
+ assert ! ( app
570
+ . world( )
571
+ . get_resource:: <Events <ScriptCallbackEvent >>( )
572
+ . is_some( ) ) ;
573
+
574
+ let mut local = SystemState :: from_world ( app. world_mut ( ) ) ;
575
+
576
+ assert ! ( !app
577
+ . world( )
578
+ . get_resource:: <Events <ScriptCallbackEvent >>( )
579
+ . is_some( ) ) ;
580
+
581
+ event_handler :: < OnTestCallback , TestPlugin > ( app. world_mut ( ) , & mut local) ;
582
+
583
+ assert ! ( app
584
+ . world( )
585
+ . get_resource:: <Events <ScriptCallbackEvent >>( )
586
+ . is_some( ) ) ;
587
+ }
548
588
}
0 commit comments