@@ -27,7 +27,7 @@ use crate::{
2727 commands:: { CreateOrUpdateScript , DeleteScript } ,
2828 error:: ScriptError ,
2929 event:: ScriptEvent ,
30- script:: { ContextKey , DisplayProxy , ScriptAttachment } ,
30+ script:: { ContextKey , DisplayProxy , ScriptAttachment , ScriptContext } ,
3131} ;
3232
3333/// Represents a scripting language. Languages which compile into another language should use the target language as their language.
@@ -218,6 +218,7 @@ fn handle_script_events<P: IntoScriptPluginParams>(
218218 scripts : Query < ( Entity , & ScriptComponent ) > ,
219219 asset_server : Res < AssetServer > ,
220220 mut script_queue : Local < ScriptQueue > ,
221+ script_contexts : Res < ScriptContext < P > > ,
221222 mut commands : Commands ,
222223 world_id : WorldId ,
223224) {
@@ -233,8 +234,6 @@ fn handle_script_events<P: IntoScriptPluginParams>(
233234 // associated with. That could be static scripts, script
234235 // components.
235236
236- let mut found_entity = false ;
237-
238237 for ( entity, script_component) in & scripts {
239238 if let Some ( handle) =
240239 script_component. 0 . iter ( ) . find ( |handle| handle. id ( ) == * id)
@@ -246,17 +245,20 @@ fn handle_script_events<P: IntoScriptPluginParams>(
246245 ) )
247246 . with_responses ( P :: readonly_configuration ( world_id) . emit_responses ) ,
248247 ) ;
249- found_entity = true ;
250248 }
251249 }
252250
253- if !found_entity {
254- let handle = Handle :: Weak ( * id) ;
251+ let handle = Handle :: Weak ( * id) ;
252+ let attachment = ScriptAttachment :: StaticScript ( handle. clone ( ) ) ;
253+ for ( resident, _) in script_contexts
254+ . residents ( & attachment)
255+ . filter ( |( r, _) | r. script ( ) == handle && r. is_static ( ) )
256+ {
255257 // if the script does not have any associated entity it's static.
256- commands. queue (
257- CreateOrUpdateScript :: < P > :: new ( ScriptAttachment :: StaticScript ( handle ) )
258- . with_responses ( P :: readonly_configuration ( world_id) . emit_responses ) ,
259- ) ;
258+ commands
259+ . queue ( CreateOrUpdateScript :: < P > :: new ( resident ) . with_responses (
260+ P :: readonly_configuration ( world_id) . emit_responses ,
261+ ) ) ;
260262 }
261263 }
262264 }
0 commit comments