How do you write callbacks?

OK, I am absolutely puzzled by this declaration:


How do I declare a callback?

// in foo.h
class Foo {
    FMOD::Studio::EventInstance eventinstance;
    FMOD_RESULT onEvent(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, void* parameters);  //????
    void bind();

// in foo.cpp
void Foo::bind() {
    eventInstance.setCallback(Foo::onEvent); //does not work ??? 

void Foo::onEvent(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, void* parameters) {
    // how do I access parameters?

Thanks for answering this newb question!

Your callback needs to have F_CALLBACK in the declaration. If it’s a class member function, it also needs to be static:

    static FMOD_RESULT F_CALLBACK onEvent(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, void* parameters);

Sorry for bumping this super old post up, I have a similar question and figured this is better than starting a completely new topic. I understand the static and F_CALLBACK part, still hope to know hot to pass parameters into the callback function. There’s nowhere in the setCallback function to pass in a void* for the callback function’s parameters argument.

You would want to use setUserData() for this, it allows you to pass arbitrary data as void* that will be sent back everytime a callback is triggered (on the instance you called setUserData() on)

1 Like