Apply side effects after running system (#5)
Deferred side effects(Commands) that were pushed in scripting exposed systems were never applied. This commit applies deferred
This commit is contained in:
parent
db3258e832
commit
6f35089693
3 changed files with 55 additions and 1 deletions
1
assets/examples/side_effects.rhai
Normal file
1
assets/examples/side_effects.rhai
Normal file
|
|
@ -0,0 +1 @@
|
|||
spawn_entity();
|
||||
51
examples/side_effects.rs
Normal file
51
examples/side_effects.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use bevy::{app::AppExit, ecs::event::ManualEventReader, prelude::*};
|
||||
use bevy_scriptum::{prelude::*, Script};
|
||||
|
||||
#[derive(Component)]
|
||||
struct Comp;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
// This is just needed for headless console app, not needed for a regular bevy game
|
||||
// that uses a winit window
|
||||
.set_runner(move |mut app: App| {
|
||||
let mut app_exit_event_reader = ManualEventReader::<AppExit>::default();
|
||||
loop {
|
||||
if let Some(app_exit_events) = app.world.get_resource_mut::<Events<AppExit>>() {
|
||||
if app_exit_event_reader
|
||||
.iter(&app_exit_events)
|
||||
.last()
|
||||
.is_some()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
app.update();
|
||||
}
|
||||
})
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(ScriptingPlugin::default())
|
||||
.add_systems(Startup, startup)
|
||||
.add_systems(Update, print_entity_names_and_quit)
|
||||
.add_script_function(String::from("spawn_entity"), spawn_entity)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn spawn_entity(mut commands: Commands) {
|
||||
commands.spawn(Name::new("SpawnedEntity"));
|
||||
}
|
||||
|
||||
fn startup(mut commands: Commands, assets_server: Res<AssetServer>) {
|
||||
commands.spawn((Script::new(
|
||||
assets_server.load("examples/side_effects.rhai"),
|
||||
),));
|
||||
}
|
||||
|
||||
fn print_entity_names_and_quit(query: Query<&Name>, mut exit: EventWriter<AppExit>) {
|
||||
if !query.is_empty() {
|
||||
for e in &query {
|
||||
println!("{}", e);
|
||||
}
|
||||
exit.send(AppExit);
|
||||
}
|
||||
}
|
||||
|
|
@ -57,7 +57,9 @@ where
|
|||
let mut inner_system = IntoSystem::into_system(self);
|
||||
inner_system.initialize(world);
|
||||
let system_fn = move |_args: In<Vec<Dynamic>>, world: &mut World| {
|
||||
Dynamic::from(inner_system.run((), world))
|
||||
let result = inner_system.run((), world);
|
||||
inner_system.apply_deferred(world);
|
||||
Dynamic::from(result)
|
||||
};
|
||||
let system = IntoSystem::into_system(system_fn);
|
||||
CallbackSystem {
|
||||
|
|
|
|||
Loading…
Reference in a new issue