Crate bevy_rosc

Source
Expand description

This is a plugin implementing OSC for bevy using rosc.

§Basic usage

Add the BevyRoscPlugin to your app, choosing the ip address at which you want to receive at. Then add either the SingleAddressOscMethod or MultiAddressOscMethod component to your entity. bevy_rosc will automatically deliver matching messages to the component, where you can then retrieve them with the get_address method.

use bevy::prelude::*;
use bevy_rosc::OscDispatcher;
use bevy_rosc::SingleAddressOscMethod;
use bevy_rosc::OscMethod;
use bevy_rosc::BevyRoscPlugin;

fn spawn(mut commands: Commands) {
    commands
        .spawn(SingleAddressOscMethod::new("/test/address".into()).unwrap());
}

fn print_received_osc_packets(mut query: Query<&mut SingleAddressOscMethod, (Changed<SingleAddressOscMethod>)>) {
    for mut osc_method in query.iter_mut() {
        match osc_method.get_message() {
            Some(message) => println!("Method {:?} received: {:?}", osc_method.get_address(), message),
            None => {}
        }
    }
}

fn main() {
    App::new()
        .add_plugins(MinimalPlugins)
        .add_plugins(BevyRoscPlugin::new("0.0.0.0:31337").unwrap())
        .add_systems(Startup, spawn)
        .add_systems(Update, print_received_osc_packets)
        .run();
}

§Advanced usage

There is the option to add custom osc method components. Your component just has to implement OscMethod and you need to add a method_dispatcher_system for it. Now your component will receive OSC messages at it’s address(es).

extern crate bevy_rosc;
use bevy::prelude::*;
use bevy_rosc::OscMethod;
use bevy_rosc::{method_dispatcher_system, BevyRoscPlugin};
use rosc::address::OscAddress;
use rosc::OscMessage;
 
#[derive(Component)]
struct MyOscMethod {
    osc_address: OscAddress,
}
 
impl OscMethod for MyOscMethod {
    fn get_addresses(&self) -> Vec<OscAddress> {
        return vec![self.osc_address.clone()];
    }
 
    // This method is called when an OSC message was successfully matched with the method
    fn receive_message(&mut self, osc_message: OscMessage) {
        println!("MyOscMethod received: {:?}", osc_message)
    }
}
 
fn startup(mut commands: Commands) {
    commands.spawn(MyOscMethod {
        osc_address: OscAddress::new("/test/address".into()).unwrap(),
    });
}
 
fn main() {
    App::new()
        .add_plugins(MinimalPlugins)
        .add_plugins(BevyRoscPlugin::new("0.0.0.0:31337").unwrap())
        .add_systems(Update, method_dispatcher_system::<MyOscMethod>) // <-- Add dispatcher system for your method
        .add_systems(Startup, startup)
        .run();
}

Structs§

BevyRoscPlugin
Plugin implementing the default functionality for bevy_rosc
MultiAddressOscMethod
Bevy component that can receive OSC messages at multiple addresses
OscDispatchEvent
An event containing all OSC messages that were received this frame and their corresponding rosc::address::Matchers
OscDispatcher
Dispatches received OscPackets and sends the OscDispatchEvent, which ultimately delivers messages to matching OscMethods.
OscUdpClient
OscUdpServer
SingleAddressOscMethod
Bevy component that can receive OSC messages at one addresses

Traits§

OscMethod
An OSC Method is capable of receiving OSC messages at one or multiple addresses.

Functions§

method_dispatcher_system
This reads OscDispatchEvents sent by the dispatcher and forwards the incoming messages to OscMethods