Continue button and playing audio

Announcements, support questions, and discussion for the Dialogue System.
devalus
Posts: 52
Joined: Tue Feb 20, 2018 5:09 pm

Continue button and playing audio

Post by devalus »

Hi Tony,

Hope you are doing well!

I'm having some trouble with how the continue button interacts with the Audio() sequence.

Using Textline with typewriter effects & a continue button.

I want to play a sound effect at the end of the dialogue:
{{default}}->Message(playsound);
Audio(audioName)@Message(playsound);

This works fine until the player clicks the continue button while typewriter is still running. First click stops the typewriter effect, but doesn't immediately move to play the audio. And second click loads the next dialogue entry, so the audio doesn't get played.

Is there a way to have the audio play immediately after the first continue button is clicked?

Cheers,
Amy
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Continue button and playing audio

Post by Tony Li »

Hi Amy,

I recommend setting it up like this:

1. Add this script to the subtitle text GameObject:
SequencerMessageSender.cs

Code: Select all

using UnityEngine;
public class SequencerMessageSender : MonoBehaviour
{
    public void SendSequencerMessage(string message)
    {
        PixelCrushers.DialogueSystem.Sequencer.Message(message);
    }
}
2. Configure the typewriter effect's OnEnd() method to call the script's SequencerMessageSender.SendSequencerMessage. In the text field, enter "DoneTyping".

3. Set the dialogue entry's Sequence to:

Code: Select all

{{default}};
Audio(audioName)@Message(DoneTyping)
Note: The Audio() command starts the audio and then forgets about it. If you want the audio to stop when the player continues the conversation, use AudioWait().
devalus
Posts: 52
Joined: Tue Feb 20, 2018 5:09 pm

Re: Continue button and playing audio

Post by devalus »

This worked perfectly, thanks Tony!

A follow up question (just curious not a necessity), is there a way to trigger audio or other sequences when the dialogue reaches a certain word? Instead of at the start, end, or with a set delay.

Also was the issue with Enhanced Scroller resolved? Haven't checked back in a while :)
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Continue button and playing audio

Post by Tony Li »

devalus wrote: Thu Jul 11, 2019 7:58 amA follow up question (just curious not a necessity), is there a way to trigger audio or other sequences when the dialogue reaches a certain word? Instead of at the start, end, or with a set delay.
Nothing is built in for that. You'd have to do some custom coding.
devalus wrote: Thu Jul 11, 2019 7:58 amAlso was the issue with Enhanced Scroller resolved? Haven't checked back in a while :)
Not yet. I'm traveling this week and not able to do much development work until Monday.
devalus
Posts: 52
Joined: Tue Feb 20, 2018 5:09 pm

Re: Continue button and playing audio

Post by devalus »

Hi Tony!

So I've encountered a problem with this solution from a while back:
Tony Li wrote: Wed Jul 10, 2019 2:28 pm Hi Amy,

I recommend setting it up like this:

1. Add this script to the subtitle text GameObject:
SequencerMessageSender.cs

Code: Select all

using UnityEngine;
public class SequencerMessageSender : MonoBehaviour
{
    public void SendSequencerMessage(string message)
    {
        PixelCrushers.DialogueSystem.Sequencer.Message(message);
    }
}
2. Configure the typewriter effect's OnEnd() method to call the script's SequencerMessageSender.SendSequencerMessage. In the text field, enter "DoneTyping".

3. Set the dialogue entry's Sequence to:

Code: Select all

{{default}};
Audio(audioName)@Message(DoneTyping)
Note: The Audio() command starts the audio and then forgets about it. If you want the audio to stop when the player continues the conversation, use AudioWait().
Most of the time it works well, however some cases my audio will play at the start of the node instead of at the end of typewriter effect when DoneTyping message is sent.

I noticed this happens when:
1. Subtitle Settings > Continue Button is set to Optional (conversation is auto-playing)
2. The node directly before that has no sequence in it and perhaps it's getting triggered by the DoneTyping message from that previous node.

At the moment I don't have any idea how to resolve this, so hoping for your input :)

Cheers,
Amy
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Continue button and playing audio

Post by Tony Li »

Hi Amy,

Do you want to always play the same audio clip every time the typewriter finishes?

If so, then instead of the sequence you could try this:

1. Add an Audio Source to the dialogue UI. Untick Loop and Play On Awake.

2. Configure the typewriter effect's OnEnd() event to call the Audio Source's AudioSource.PlayOneShot method. Assign the audio clip.
devalus
Posts: 52
Joined: Tue Feb 20, 2018 5:09 pm

Re: Continue button and playing audio

Post by devalus »

No unfortunately I'm calling different audioclips with dialogue sequence. So sometimes some nodes won't be playing any audio.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Continue button and playing audio

Post by Tony Li »

Okay, I'll give this a little thought and reply back later today.
devalus
Posts: 52
Joined: Tue Feb 20, 2018 5:09 pm

Re: Continue button and playing audio

Post by devalus »

Thank you Tony :)
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Continue button and playing audio

Post by Tony Li »

The previous node's typewriter effect ends on the same frame that the new node starts. So it's possible that the new node will receive the "DoneTyping" message as you've described. Let's modify the approach.

Here's an example scene: AudioAtTypewriterEndExample_2019-08-07.unitypackage

It uses two small scripts:
SequencerCommandSetTypeEndAudio.cs

Code: Select all

using UnityEngine;
using System.Collections;

namespace PixelCrushers.DialogueSystem.SequencerCommands
{

    public class SequencerCommandSetTypeEndAudio : SequencerCommand
    {
        private IEnumerator Start()
        {
            yield return null; // Wait 1 frame for previous node's typewriter to finish.
            var audioClip = DialogueManager.LoadAsset(GetParameter(0), typeof(AudioClip)) as AudioClip;
            TypeEndAudio.audioClip = audioClip;
            if (audioClip == null) Debug.LogWarning("Dialogue System: Sequencer: TypeEndAudio(" + GetParameters() + "): Audio clip not found in Resources or asset bundle.");
            Stop();
        }
    }
}
TypeEndAudio.cs

Code: Select all

using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class TypeEndAudio : MonoBehaviour
{
    public static AudioClip audioClip = null;

    public void PlayAudioClip()
    {
        if (audioClip != null)
        {
            GetComponent<AudioSource>().PlayOneShot(audioClip);
            audioClip = null;
        }
    }
}
It does not use the scripts in previous posts of this thread. You can remove those scripts from your project if you want.

Instead of this:

Code: Select all

{{default}};
Audio(audioName)@Message(DoneTyping)
Use this:

Code: Select all

{{default}};
SetTypeEndAudio(audioName)
The new sequencer command SetTypeEndAudio() queues an audio clip to play when the typewriter is done. It waits one frame to avoid the timing issue of the previous solution.

Then add the other script (TypeEndAudio) to the subtitle text GameObject, and configure the typewriter effect's OnEnd() to call TypeEndAudio.PlayAudioClip.
Post Reply