@@ -16,6 +16,7 @@ use bevy::{
1616} ;
1717use bevy_console:: { AddConsoleCommand , ConsoleCommand , ConsoleOpen , ConsolePlugin , make_layer} ;
1818use bevy_mod_scripting:: { core:: bindings:: AllocatorDiagnosticPlugin , prelude:: * } ;
19+ use bevy_mod_scripting_core:: { commands:: RemoveStaticScript , script:: StaticScripts } ;
1920use clap:: Parser ;
2021
2122// CONSOLE SETUP
@@ -41,8 +42,7 @@ fn run_script_cmd(
4142 mut commands : Commands ,
4243 asset_server : Res < AssetServer > ,
4344 script_comps : Query < ( Entity , & ScriptComponent ) > ,
44- mut static_lua_scripts : Local < Vec < ScriptId > > ,
45- mut static_rhai_scripts : Local < Vec < ScriptId > > ,
45+ static_scripts : Res < StaticScripts > ,
4646) {
4747 if let Some ( Ok ( command) ) = log. take ( ) {
4848 match command {
@@ -53,58 +53,27 @@ fn run_script_cmd(
5353 // create an entity with the script component
5454 bevy:: log:: info!( "Using game of life script game_of_life.{}" , language) ;
5555
56- let script_path = format ! ( "scripts/game_of_life.{}" , language ) ;
56+ let script_path = format ! ( "scripts/game_of_life.{language}" ) ;
5757 if !use_static_script {
5858 bevy:: log:: info!( "Spawning an entity with ScriptComponent" ) ;
5959 commands. spawn ( ScriptComponent :: new ( vec ! [ asset_server. load( script_path) ] ) ) ;
6060 } else {
6161 bevy:: log:: info!( "Using static script instead of spawning an entity" ) ;
6262 let handle = asset_server. load ( script_path) ;
63- if language == "lua" {
64- static_lua_scripts. push ( handle. id ( ) ) ;
65- } else {
66- static_rhai_scripts. push ( handle. id ( ) ) ;
67- }
6863 commands. queue ( AddStaticScript :: new ( handle) )
6964 }
7065 }
7166 GameOfLifeCommand :: Stop => {
7267 // we can simply drop the handle, or manually delete, I'll just drop the handle
73- bevy:: log:: info!( "Stopping game of life by dropping the handles to all scripts" ) ;
74- for ( id, script_component) in & script_comps {
75- for script in & script_component. 0 {
76- match script
77- . path ( )
78- . and_then ( |p| p. get_full_extension ( ) )
79- . unwrap_or_default ( )
80- . as_str ( )
81- {
82- "lua" => {
83- commands
84- . entity ( id)
85- . queue ( DeleteScript :: < LuaScriptingPlugin > :: new ( script. id ( ) ) ) ;
86- }
87- "rhai" => {
88- #[ cfg( feature = "rhai" ) ]
89- commands
90- . entity ( id)
91- . queue ( DeleteScript :: < RhaiScriptingPlugin > :: new ( script. id ( ) ) ) ;
92- }
93- ext => {
94- warn ! ( "Can't delete script with extension {ext:?}." ) ;
95- }
96- }
97- }
68+ bevy:: log:: info!(
69+ "Stopping game of life by detaching each script and static script"
70+ ) ;
71+ for ( id, _) in & script_comps {
9872 commands. entity ( id) . despawn ( ) ;
9973 }
10074
101- for script in static_lua_scripts. drain ( ..) {
102- commands. queue ( DeleteScript :: < LuaScriptingPlugin > :: new ( script) ) ;
103- }
104-
105- #[ cfg( feature = "rhai" ) ]
106- for script in static_rhai_scripts. drain ( ..) {
107- commands. queue ( DeleteScript :: < RhaiScriptingPlugin > :: new ( script) ) ;
75+ for script in static_scripts. values ( ) {
76+ commands. queue ( RemoveStaticScript :: new ( script. clone ( ) ) ) ;
10877 }
10978 }
11079 }
0 commit comments