From f0ed3fa6e9e4239ed3ade14b8a49fc5f397ea179 Mon Sep 17 00:00:00 2001 From: Drake Date: Sat, 4 Oct 2025 17:23:57 -0400 Subject: [PATCH 1/5] Replaced Event with Message; try_ctx to ctx --- src/console.rs | 42 ++++++++++++++++++++++++++---------------- src/log.rs | 4 ++-- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/console.rs b/src/console.rs index 81c5674..a724032 100644 --- a/src/console.rs +++ b/src/console.rs @@ -1,3 +1,4 @@ +use bevy::ecs::query::FilteredAccessSet; use bevy::ecs::resource::Resource; use bevy::ecs::{ component::Tick, @@ -27,9 +28,9 @@ use crate::{ ConsoleSet, }; -type ConsoleCommandEnteredReaderSystemParam = EventReader<'static, 'static, ConsoleCommandEntered>; +type ConsoleCommandEnteredReaderSystemParam = MessageReader<'static, 'static, ConsoleCommandEntered>; -type PrintConsoleLineWriterSystemParam = EventWriter<'static, PrintConsoleLine>; +type PrintConsoleLineWriterSystemParam = MessageWriter<'static, PrintConsoleLine>; /// A super-trait for command like structures pub trait Command: NamedCommand + CommandFactory + FromArgMatches + Sized + Resource {} @@ -69,10 +70,11 @@ pub trait NamedCommand { /// ``` pub struct ConsoleCommand<'w, T> { command: Option>, - console_line: EventWriter<'w, PrintConsoleLine>, + console_line: MessageWriter<'w, PrintConsoleLine>, } impl ConsoleCommand<'_, T> { + /// /// Returns Some(T) if the command was executed and arguments were valid. /// /// This method should only be called once. @@ -128,9 +130,9 @@ unsafe impl SystemParam for ConsoleCommand<'_, T> { type State = ConsoleCommandState; type Item<'w, 's> = ConsoleCommand<'w, T>; - fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { - let event_reader = ConsoleCommandEnteredReaderSystemParam::init_state(world, system_meta); - let console_line = PrintConsoleLineWriterSystemParam::init_state(world, system_meta); + fn init_state(world: &mut World) -> Self::State { + let event_reader = ConsoleCommandEnteredReaderSystemParam::init_state(world); + let console_line = PrintConsoleLineWriterSystemParam::init_state(world); ConsoleCommandState { event_reader, console_line, @@ -138,6 +140,14 @@ unsafe impl SystemParam for ConsoleCommand<'_, T> { } } + fn init_access( + state: &Self::State, + system_meta: &mut SystemMeta, + component_access_set: &mut FilteredAccessSet, + world: &mut World, + ) { + } + #[inline] unsafe fn get_param<'w, 's>( state: &'s mut Self::State, @@ -189,7 +199,7 @@ unsafe impl SystemParam for ConsoleCommand<'_, T> { } } /// Parsed raw console command into `command` and `args`. -#[derive(Clone, Debug, Event)] +#[derive(Clone, Debug, Event, Message)] pub struct ConsoleCommandEntered { /// the command definition pub command_name: String, @@ -198,7 +208,7 @@ pub struct ConsoleCommandEntered { } /// Events to print to the console. -#[derive(Clone, Debug, Eq, Event, PartialEq)] +#[derive(Clone, Debug, Eq, Event, PartialEq, Message)] pub struct PrintConsoleLine { /// Console line pub line: String, @@ -240,7 +250,7 @@ pub struct ConsoleConfiguration { pub moveable: bool, /// show the title bar or not pub show_title_bar: bool, - /// Background color of console window + /// Background color of console window pub background_color: Color32, /// Foreground (text) color pub foreground_color: Color32, @@ -488,15 +498,15 @@ pub(crate) fn console_ui( mut egui_context: EguiContexts, config: Res, mut cache: ResMut, - mut keyboard_input_events: EventReader, + mut keyboard_input_events: MessageReader, mut state: ResMut, - command_entered: EventWriter, + command_entered: MessageWriter, mut console_open: ResMut, ) { let keyboard_input_events = keyboard_input_events.read().collect::>(); // If there is no egui context, return, this can happen when exiting the app - let ctx = if let Some(ctxt) = egui_context.try_ctx_mut() { + let ctx = if let Ok(ctxt) = egui_context.ctx_mut() { ctxt } else { return; @@ -669,7 +679,7 @@ fn handle_enter( config: Res<'_, ConsoleConfiguration>, cache: &ResMut<'_, ConsoleCache>, state: &mut ResMut<'_, ConsoleState>, - mut command_entered: EventWriter<'_, ConsoleCommandEntered>, + mut command_entered: MessageWriter<'_, ConsoleCommandEntered>, ui: &mut egui::Ui, text_edit_response: &egui::Response, ) { @@ -725,7 +735,7 @@ fn handle_enter( pub(crate) fn receive_console_line( mut console_state: ResMut, - mut events: EventReader, + mut events: MessageReader, ) { for event in events.read() { let event: &PrintConsoleLine = event; @@ -765,7 +775,7 @@ pub fn block_mouse_input( return; } - let Some(context) = contexts.try_ctx_mut() else { + let Ok(context) = contexts.ctx_mut() else { return; }; @@ -783,7 +793,7 @@ pub fn block_keyboard_input( return; } - let Some(context) = contexts.try_ctx_mut() else { + let Ok(context) = contexts.ctx_mut() else { return; }; diff --git a/src/log.rs b/src/log.rs index 567fece..28611c4 100644 --- a/src/log.rs +++ b/src/log.rs @@ -6,7 +6,7 @@ use std::{ use bevy::{ app::{App, Update}, log::tracing_subscriber::{self, EnvFilter, Layer, Registry}, - prelude::{EventWriter, IntoScheduleConfigs, ResMut, Resource}, + prelude::{MessageWriter, IntoScheduleConfigs, ResMut, Resource}, }; use crate::{ConsoleSet, PrintConsoleLine}; @@ -45,7 +45,7 @@ impl Write for BevyLogBufferWriter { /// Flushes the log buffer and sends its content to the console pub fn send_log_buffer_to_console( buffer: ResMut, - mut console_lines: EventWriter, + mut console_lines: MessageWriter, ) { let mut buffer = buffer.0.lock().unwrap(); // read and clean buffer From f679dec0c0258f95c4fd4a7544ad6758e90766ad Mon Sep 17 00:00:00 2001 From: Drake Date: Sat, 4 Oct 2025 17:24:21 -0400 Subject: [PATCH 2/5] Egui pass update --- src/lib.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 99d6e42..2c16fe6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ use bevy::prelude::*; pub use bevy_console_derive::ConsoleCommand; -use bevy_egui::{EguiContextPass, EguiPlugin, EguiPreUpdateSet}; +use bevy_egui::{ EguiPrimaryContextPass, EguiPlugin, EguiPreUpdateSet}; use console::{block_keyboard_input, block_mouse_input, ConsoleCache}; use trie_rs::TrieBuilder; @@ -48,12 +48,13 @@ pub enum ConsoleSet { } /// Run condition which does not run any command systems if no command was entered -fn have_commands(commands: EventReader) -> bool { +fn have_commands(commands: MessageReader) -> bool { !commands.is_empty() } /// builds the predictive search engine for completions fn init(config: Res, mut cache: ResMut) { + println!("lib.rs:init"); let mut trie_builder = TrieBuilder::new(); for cmd in config.commands.keys() { trie_builder.push(cmd); @@ -72,8 +73,8 @@ impl Plugin for ConsolePlugin { .init_resource::() .init_resource::() .init_resource::() - .add_event::() - .add_event::() + .add_message::() + .add_message::() .add_console_command::(clear_command) .add_console_command::(exit_command) .add_console_command::(help_command) @@ -86,14 +87,14 @@ impl Plugin for ConsolePlugin { .before(EguiPreUpdateSet::BeginPass), ) .add_systems( - EguiContextPass, + EguiPrimaryContextPass, ( console_ui.in_set(ConsoleSet::ConsoleUI), receive_console_line.in_set(ConsoleSet::PostCommands), ), ) .configure_sets( - EguiContextPass, + EguiPrimaryContextPass, ( ConsoleSet::Commands .after(ConsoleSet::ConsoleUI) @@ -105,9 +106,7 @@ impl Plugin for ConsolePlugin { // Don't initialize an egui plugin if one already exists. // This can happen if another plugin is using egui and was installed before us. if !app.is_plugin_added::() { - app.add_plugins(EguiPlugin { - enable_multipass_for_primary_context: true, - }); + app.add_plugins(EguiPlugin::default()); } } } From bbec4bb2b5fc535947d2964130b565d625d84305 Mon Sep 17 00:00:00 2001 From: Drake Date: Sat, 4 Oct 2025 17:25:03 -0400 Subject: [PATCH 3/5] bevy_egui needs a 2D camera to be created first for these to work properly --- examples/capture_bevy_logs.rs | 6 ++++++ examples/change_console_key.rs | 5 +++++ examples/completions.rs | 5 +++++ examples/log_command.rs | 5 +++++ examples/raw_commands.rs | 7 ++++++- examples/write_to_console.rs | 7 ++++++- 6 files changed, 33 insertions(+), 2 deletions(-) diff --git a/examples/capture_bevy_logs.rs b/examples/capture_bevy_logs.rs index 2f78392..815f7d2 100644 --- a/examples/capture_bevy_logs.rs +++ b/examples/capture_bevy_logs.rs @@ -9,9 +9,11 @@ fn main() { level: log::Level::INFO, filter: "error,capture_bevy_logs=info".to_owned(), custom_layer: make_layer, + fmt_layer: |_| None, }), ConsolePlugin, )) + .add_systems(Startup, setup_camera_system) .add_systems(Startup, || { log::info!("Hi!"); log::warn!("This is a warning!"); @@ -21,3 +23,7 @@ fn main() { }) .run(); } + +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} diff --git a/examples/change_console_key.rs b/examples/change_console_key.rs index 2218790..8710982 100644 --- a/examples/change_console_key.rs +++ b/examples/change_console_key.rs @@ -5,6 +5,7 @@ use bevy_console::{ConsoleConfiguration, ConsolePlugin}; fn main() { App::new() .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .insert_resource(ConsoleConfiguration { keys: vec![ // Console key on a swedish keyboard @@ -21,3 +22,7 @@ fn main() { }) .run(); } + +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} diff --git a/examples/completions.rs b/examples/completions.rs index bdf5c3a..88662f2 100644 --- a/examples/completions.rs +++ b/examples/completions.rs @@ -6,6 +6,7 @@ fn main() { App::new() // set background to red .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .insert_resource(ConsoleConfiguration { arg_completions: vec![ vec!["custom".into(), "foo".into()], @@ -18,6 +19,10 @@ fn main() { .run(); } +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + #[derive(Clone, Copy, ValueEnum)] pub enum Variant { Foo, diff --git a/examples/log_command.rs b/examples/log_command.rs index 8638942..9029f7a 100644 --- a/examples/log_command.rs +++ b/examples/log_command.rs @@ -6,10 +6,15 @@ fn main() { App::new() // set background to red .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .add_console_command::(log_command) .run(); } +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + /// Prints given arguments to the console #[derive(Parser, ConsoleCommand)] #[command(name = "log")] diff --git a/examples/raw_commands.rs b/examples/raw_commands.rs index 00a047d..bb84a41 100644 --- a/examples/raw_commands.rs +++ b/examples/raw_commands.rs @@ -4,11 +4,16 @@ use bevy_console::{ConsoleCommandEntered, ConsolePlugin, ConsoleSet}; fn main() { App::new() .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .add_systems(Update, raw_commands.in_set(ConsoleSet::Commands)) .run(); } -fn raw_commands(mut console_commands: EventReader) { +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + +fn raw_commands(mut console_commands: MessageReader) { for ConsoleCommandEntered { command_name, args } in console_commands.read() { println!(r#"Entered command "{command_name}" with args {:#?}"#, args); } diff --git a/examples/write_to_console.rs b/examples/write_to_console.rs index 5b37630..1bf8d31 100644 --- a/examples/write_to_console.rs +++ b/examples/write_to_console.rs @@ -4,6 +4,7 @@ use bevy_console::{ConsolePlugin, ConsoleSet, PrintConsoleLine}; fn main() { App::new() .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) // NOTE: this wouldn't work for this particular case, // systems in the [`ConsoleSet::Commands`] do not run if there are no console commands entered // .add_systems(Update, write_to_console.in_set(ConsoleSet::Commands)) @@ -12,6 +13,10 @@ fn main() { .run(); } -fn write_to_console(mut console_line: EventWriter) { +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + +fn write_to_console(mut console_line: MessageWriter) { console_line.write(PrintConsoleLine::new("Hello".into())); } From e04aaa0f6cf4354a51019a5aa5e999c570b4adbd Mon Sep 17 00:00:00 2001 From: Drake Date: Sat, 4 Oct 2025 17:25:27 -0400 Subject: [PATCH 4/5] Event to Message --- src/commands/exit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/exit.rs b/src/commands/exit.rs index 413b99c..7b87767 100644 --- a/src/commands/exit.rs +++ b/src/commands/exit.rs @@ -12,7 +12,7 @@ pub(crate) struct ExitCommand; pub(crate) fn exit_command( mut exit: ConsoleCommand, - mut exit_writer: EventWriter, + mut exit_writer: MessageWriter, ) { if let Some(Ok(_)) = exit.take() { exit_writer.write(AppExit::Success); From ebb0c4fea7e3b5e4ab0e5b4c975f2cb827b08877 Mon Sep 17 00:00:00 2001 From: Drake Date: Sat, 4 Oct 2025 17:25:45 -0400 Subject: [PATCH 5/5] bevy and bevy_egui updated to latest --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ac192f3..d9e172b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,13 +10,13 @@ license = "MIT" readme = "README.md" [dependencies] -bevy = { version = "0.16", default-features = false, features = [ +bevy = { version = "0.17", default-features = false, features = [ "std", "bevy_log", ] } clap = { version = "4.5", features = ["derive"] } bevy_console_derive = { path = "./bevy_console_derive", version = "0.5.0" } -bevy_egui = { version = "0.34", default-features = false, features = [ +bevy_egui = { version = "0.37", default-features = false, features = [ "render", "default_fonts", ] } @@ -26,7 +26,7 @@ strip-ansi-escapes = "0.2" trie-rs = "0.2" [dev-dependencies] -bevy = { version = "0.16", features = ["std", "bevy_log"] } +bevy = { version = "0.17", features = ["std", "bevy_log"] } color-print = { version = "0.3" }