FMOD - Unreal Callbacks in C++

, ,

Hello everyone.

I am trying to implement a simple callback in my C++ class. I already implemented the callback in Blueprints, but I want to translate the same system in CPP. I managed to load an FMOD Component and created and bound a delegate function too. The callback works, but it triggers the function on every beat.

How do I get a reference to the bar, beat, position, etc.? Getting these ints and floats is easy in Blueprints by calling the component event “On Timeline Beat”:

BP_TImelineCallback

The closest function I found in the UFMODAudioComponent::OnTimelineBeat class is .Broadcast(), but it asks for inputs instead of giving me references to these numbers.

This is how my constructor looks:

AMyCPPActorClass::AMyCPPActorClass()
{
 	
	PrimaryActorTick.bCanEverTick = true;

	StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
	
	FmodAudioComponent = CreateDefaultSubobject<UFMODAudioComponent>(TEXT("FMOD Audio Component"));
	FmodAudioComponent->SetEvent(myEvent);
	FmodAudioComponent->SetAutoActivate(false);
	FmodAudioComponent->OnTimelineBeat.Add(Callback);
	FmodAudioComponent->bEnableTimelineCallbacks = true;
	
	Callback.BindUFunction(this, TEXT("CppRotate"));
	
}

Maybe, I am implementing this system in the wrong way.
Thank you for reading my thread and for any advice.
Best!

In your callback when the type is FMOD_STUDIO_EVENT_CALLBACK_TIMELINE_BEAT, parameters will be pointing to a FMOD_STUDIO_TIMELINE_BEAT_PROPERTIES struct. To access the beat properties you just need to cast the parameters pointer, e.g

FMOD_RESULT F_CALLBACK Callback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type,  FMOD_STUDIO_EVENTINSTANCE *event,  void *parameters)
{
    switch(type)
    {
        case FMOD_STUDIO_EVENT_CALLBACK_TIMELINE_BEAT:
        {
            FMOD_STUDIO_TIMELINE_BEAT_PROPERTIES *beatProperties = (FMOD_STUDIO_TIMELINE_BEAT_PROPERTIES *)parameters;
            int beat = beatProperties->beat;
            int bar = beatProperties->bar;
            float tempo = beatProperties->tempo;
            break;
        }
    }
    return FMOD_OK;
}