You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One of the more advanced features of FMOD allows pulling information back out of FMOD at runtime.
I've been trying to figure out how to do so with bevy_fmod recently. While I can register callbacks, capturing bevy context to trigger Events for example has been proving... a bit more challenging.
It seems useful enough to me to consider adding an example for such useage (if possible at all).
Has anyone attempted to do this before with bevy? I'll share some code snippets as examples for what I mean.
basic callback example:
use bevy::prelude::*;use bevy_fmod::fmod_studio::FmodStudio;use libfmod::ffi::{FMOD_OK,FMOD_RESULT,FMOD_STUDIO_EVENTINSTANCE,FMOD_STUDIO_EVENT_CALLBACK_TYPE};// Implement a function with the expected FFI signature that calls the closureextern"C"fncallback_bridge<F>(param1:u32,param2:*mutFMOD_STUDIO_EVENTINSTANCE,param3:*mut std::ffi::c_void,) -> i32whereF:FnMut(u32,*mutFMOD_STUDIO_EVENTINSTANCE,*mut std::ffi::c_void) -> i32,{unsafe{// Get the closure from the context datalet callback_wrapper:&mutCallbackWrapper<F> = &mut*(param3 as*mutCallbackWrapper<F>);// Call the closure with the provided parameters(callback_wrapper.callback)(param1, param2, param3)}}impl<F>CallbackWrapper<F>whereF:FnMut(u32,*mutFMOD_STUDIO_EVENTINSTANCE,*mut std::ffi::c_void) -> i32,{// Function to create a new CallbackWrapperfnnew(callback:F) -> CallbackWrapper<F>{CallbackWrapper{ callback }}// Function to get the function pointer to the bridge functionfnget_callback_bridge(&self) -> libfmod::ffi::FMOD_STUDIO_EVENT_CALLBACK{Some(callback_bridge::<F>)}}pubfnsetup_fmod_callbacks_system(studio:Res<FmodStudio>){letmut my_closure = move |param1:u32,param2:*mutFMOD_STUDIO_EVENTINSTANCE,param3:*mut std::ffi::c_void| {print!("triggered by FMOD event");FMOD_OK};let callback_wrapper = CallbackWrapper::new(my_closure);let callback_bridge = callback_wrapper.get_callback_bridge();let _ = studio
.0.get_event("event:/my-event").unwrap().set_callback(callback_bridge,0xFFFF_FFFFu32);}
This reliably triggers when the appropriate FMOD event triggers, multiple times due to the catch_all mask FFFF_FFFF.
But as for relaying this information to bevy... I've tried an approach using crossbeam-channel.
The text was updated successfully, but these errors were encountered:
My bevy attempts so far ranged from bevy EventWriters constructions such as NonSend<EventWriter<FmodEvent>>, up to my most recent approach that compiles but unfortunately segfaults at runtime.
#[derive(Resource)]structFmodReceiver(Receiver<LevelEvent>);#[derive(Event)]pubstructFmodEvent;pubfnsetup_fmod_callbacks_system(studio:Res<FmodStudio>){let(sender, receiver):(Sender<FmodEvent>,Receiver<FmodEvent>) = unbounded();
commands.insert_resource(FmodReceiver(receiver));letmut my_closure = move |param1:u32,param2:*mutFMOD_STUDIO_EVENTINSTANCE,param3:*mut std::ffi::c_void| {print!("triggered by FMOD event");// Send the event to the main thread
sender.send(FmodEvent::new()).expect("crossbeams sender failed");// doesn't print, just segfaultsFMOD_OK};let callback_wrapper = CallbackWrapper::new(my_closure);let callback_bridge = callback_wrapper.get_callback_bridge();let _ = studio
.0.get_event("event:/my-event").unwrap().set_callback(callback_bridge,0xFFFF_FFFFu32);}
One of the more advanced features of FMOD allows pulling information back out of FMOD at runtime.
I've been trying to figure out how to do so with bevy_fmod recently. While I can register callbacks, capturing bevy context to trigger Events for example has been proving... a bit more challenging.
It seems useful enough to me to consider adding an example for such useage (if possible at all).
Has anyone attempted to do this before with bevy? I'll share some code snippets as examples for what I mean.
basic callback example:
This reliably triggers when the appropriate FMOD event triggers, multiple times due to the catch_all mask
FFFF_FFFF
.But as for relaying this information to bevy... I've tried an approach using crossbeam-channel.
The text was updated successfully, but these errors were encountered: