Accept/Abandon/Fail Quest sound

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
variantpolygon
Posts: 5
Joined: Fri Mar 20, 2020 4:19 pm

Accept/Abandon/Fail Quest sound

Post by variantpolygon »

Is there a way to have a sound play when you accept a quest (same sound for all quests)? I would also know if it's possible to play a sound when you fail or abandon a quest. Thanks!
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accept/Abandon/Fail Quest sound

Post by Tony Li »

Hi,

Yes. The easiest way (if you don't mind a tiny bit of scripting) is to add a script to the Dialogue Manager that has an OnQuestStateChange method. Something like:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class QuestStateSounds : MonoBehaviour
{
    // Assign the variables below in the inspector.
    public AudioSource audioSource;
    public AudioClip acceptedSound;
    public AudioClip failedSound;
    public AudioClip abandonedSound;
    
    void OnQuestStateChange(string quest)
    {
        switch (QuestLog.GetQuestState(quest))
        {
            case QuestState.Active:
                audioSource.PlayOneShot(acceptedSound);
                break;
            case QuestState.Failure:
                audioSource.PlayOneShot(failedSound);
                break;
            case QuestState.Abandoned:
                audioSource.PlayOneShot(abandonedSound);
                break;
        }
    }
}
variantpolygon
Posts: 5
Joined: Fri Mar 20, 2020 4:19 pm

Re: Accept/Abandon/Fail Quest sound

Post by variantpolygon »

Thank you so much for the quick reply and the script! However once I tried adding it to my game directory I get the following four console errors:

Assets\Scripts\QuestStateSounds.cs(17,28): error CS1003: Syntax error, ':' expected
Assets\Scripts\QuestStateSounds.cs(17,28): error CS1513: } expected
Assets\Scripts\QuestStateSounds.cs(23,28): error CS1003: Syntax error, ':' expected
Assets\Scripts\QuestStateSounds.cs(23,28): error CS1513: } expected

Any idea why that's happening, I tried replacing ';' with ':' but no luck. Thanks again!
variantpolygon
Posts: 5
Joined: Fri Mar 20, 2020 4:19 pm

Re: Accept/Abandon/Fail Quest sound

Post by variantpolygon »

Actually, I found the errors and fixed the script. But the audio doesn't seem to play when attached to the Dialogue Manager even when I add an AudioSource component.

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class QuestStateSounds : MonoBehaviour
{
    // Assign the variables below in the inspector.
    public AudioSource audioSource;
    public AudioClip acceptedSound;
    public AudioClip failedSound;
    public AudioClip abandonedSound;
    
    void OnQuestStateChanged(string quest)
    {
        switch (QuestLog.GetQuestState(quest))
        {
            case QuestState.Active:
                audioSource.PlayOneShot(acceptedSound);
                break;
            case QuestState.Failure:
                audioSource.PlayOneShot(failedSound);
                break;
            case QuestState.Abandoned:
                audioSource.PlayOneShot(abandonedSound);
                break;
        }
    }
}
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accept/Abandon/Fail Quest sound

Post by Tony Li »

Sorry, you fixed two typos (missing ':'), but the third one was more subtle. The method name is "OnQuestStateChange", not "OnQuestStateChanged". I fixed the typos in my post above.

If it still doesn't work, please check:
  • Did you assign the Audio Source field in the inspector?
  • Is the Audio Source's Spatial Blend set to 2D?
  • Are there any errors or warnings in the console when the audio should be playing?
variantpolygon
Posts: 5
Joined: Fri Mar 20, 2020 4:19 pm

Re: Accept/Abandon/Fail Quest sound

Post by variantpolygon »

Awesome, it works! Thank you so much. Great asset by the way!
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accept/Abandon/Fail Quest sound

Post by Tony Li »

Thanks! Glad to help.
Post Reply