Page 1 of 1

Accept/Abandon/Fail Quest sound

Posted: Fri Mar 20, 2020 4:23 pm
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!

Re: Accept/Abandon/Fail Quest sound

Posted: Fri Mar 20, 2020 4:56 pm
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;
        }
    }
}

Re: Accept/Abandon/Fail Quest sound

Posted: Fri Mar 20, 2020 5:13 pm
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!

Re: Accept/Abandon/Fail Quest sound

Posted: Fri Mar 20, 2020 5:50 pm
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;
        }
    }
}

Re: Accept/Abandon/Fail Quest sound

Posted: Fri Mar 20, 2020 9:57 pm
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?

Re: Accept/Abandon/Fail Quest sound

Posted: Sat Mar 21, 2020 9:19 am
by variantpolygon
Awesome, it works! Thank you so much. Great asset by the way!

Re: Accept/Abandon/Fail Quest sound

Posted: Sat Mar 21, 2020 9:46 am
by Tony Li
Thanks! Glad to help.