hey guys!
im new to fmod unity and i have a question.
i have a general_speaker script used for my npcs that have dialog that plays their dialog.
is there a way for me to check if any other dialog is playing when the script tries to play a new dialog, so that we can either return if we cant interrupt that dialog, or stop the event instance of that previous dialog if we are allowed to interrupt?
here is my code as of now, which seems to work but after i added this unity crashes sometimes:
void PlayDialog(AI_Dialog targetDialog, bool forceInterruption = false)
{
//--CHECK IF THERE ARE ANY ECENTS PLAYING AND HANDLE INTERRUPTION OF THOSE EVENTS--
//check if an event is playing
if (PlayingInstance.isValid())
{
PlayingInstance.getPlaybackState(out PLAYBACK_STATE state);
if (state != PLAYBACK_STATE.STOPPED)
{
//but if we CAN interrupt. stop any playing voice lines
if (forceInterruption)
PlayingInstance.stop(STOP_MODE.IMMEDIATE);
else
return;
}
}
//----------------------------------------------------------------------------------
FMOD.Studio.EVENT_CALLBACK dialogueCallback = new FMOD.Studio.EVENT_CALLBACK(SentenceEventCallback);
PlayingInstance = FMODUnity.RuntimeManager.CreateInstance(targetDialog.audio);
FMODUnity.RuntimeManager.AttachInstanceToGameObject(PlayingInstance, audioTransform);
PlayingInstance.setCallback(dialogueCallback);
//Start the sound
PlayingInstance.start();
PlayingInstance.release();
}
[AOT.MonoPInvokeCallback(typeof(EVENT_CALLBACK))]
FMOD.RESULT SentenceEventCallback(EVENT_CALLBACK_TYPE type, System.IntPtr instancePtr, System.IntPtr parameterPtr)
{
try
{
//var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
FMOD.Sound sound = new FMOD.Sound(parameterPtr);
sound.getName(out string eventName, 1024);
switch (type)
{
case EVENT_CALLBACK_TYPE.STARTED:
{
playingASound = true;
break;
}
case EVENT_CALLBACK_TYPE.STOPPED:
{
playingASound = false;
break;
}
}
return FMOD.RESULT.OK;
}
catch (System.Exception)
{
throw;
}
}