//! A minimal example setting up bevy_rosc without the plugin extern crate bevy_rosc; use bevy::prelude::*; use bevy_rosc::OscMethod; use bevy_rosc::{method_dispatcher_system, MultiAddressOscMethod, OscDispatchEvent}; use bevy_rosc::{OscDispatcher, OscUdpServer}; fn startup(mut commands: Commands) { println!("** Startup"); commands.spawn(MultiAddressOscMethod::new(vec!["/test/address".into()]).unwrap()); // Spawn UDP server that can receive OSC packets on port 31337 commands.spawn(OscUdpServer::new("0.0.0.0:31337").unwrap()); } /// System that listens for any `MultiAddressOscMethod` that has changed and then prints out the received OscMessage fn print_received_osc_packets( mut query: Query<&mut MultiAddressOscMethod, Changed>, ) { for mut osc_method in query.iter_mut() { let new_msg = osc_method.get_message(); if let Some(msg) = new_msg { println!( "Method {:?} received: {:?}", osc_method.get_addresses()[0], msg ) } } } /// System that receives messages via UDP and then forwards them to the dispatcher fn osc_receive_system( mut osc_dispatcher: ResMut, mut query: Query<&mut OscUdpServer>, event_writer: EventWriter, ) { let mut osc_packets = vec![]; for osc_udp_server in query.iter_mut() { loop { if let Ok(o) = osc_udp_server.recv() { match o { Some(p) => osc_packets.push(p), None => break, } } } } osc_dispatcher.dispatch(osc_packets, event_writer); } fn main() { App::new() .add_plugins(MinimalPlugins) // Add dispatcher resource .insert_resource(OscDispatcher::default()) // Event sent by the dispatcher .add_event::() // System that received the dispatch event and attempts to match received messages with all `MultiAddressOscMethod` components .add_systems( PreUpdate, ( osc_receive_system, method_dispatcher_system::.after(osc_receive_system), ), ) .add_systems(Startup, startup) .add_systems(Update, print_received_osc_packets) .run(); }