Page 1 of 1

Dialogue System: Ending conversation on OnClick

Posted: Sat Feb 25, 2023 3:11 pm
by kenlem
I resurrected some old speech recognition code I had that used to work. Now, when I run it, the OnClick ends the conversation instead of selecting the item.

I'm using Private Hart from the demo database. When an entry is recognized the OnClick in the my code below is fired. The selections go away and the debug Info of the Dialogue is showing that the conversation has ended.

If I select selections without using speech recognition, audio plays when I select an option then it moves on to the rest of the conversation. I must be doing something wrong. Any suggestions on where to look?

Thanks.

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class SpeechDialogueUI : StandardDialogueUI
{
	public Response[] responses;
	
	SpeechController speechController;
	
	public override void ShowResponses(Subtitle subtitle, Response[] responses, float timeout)
	{
		Debug.Log("Show Responses");
		Debug.Log("Found " + responses.Length + " responses");
		if (speechController == null) {
			speechController = FindObjectOfType<SpeechController>();
		}
		
		//if (speechController != null) {
		//	speechController.StartRecognition();
		//}
		
		this.responses = responses;
		base.ShowResponses(subtitle, responses, timeout);
	}

	public override void HideResponses()
	{
		Debug.Log("HideResponses");
		base.HideResponses();
		
		if (speechController != null) {
			speechController.EndRecognition();
		}	
	}
	
	public void PartialResultReceived(string result)
	{
		Debug.Log("UI: partial: " + result);
	}
	
	public void ResultReceived(string result)
	{
		int selectedResponse = -1;
		float maxScore = 0.0f;

		Debug.Log("UI: result: " + result);
		
		if (result != null && result.Length > 0 && responses != null) {
			// Loop through responses and see which one scores highest.		
			string[] words = result.Split(' ');
			for (int i=0; i<responses.Length; i++) {
				Debug.Log("UI: " + i);
				float score = scoreResponse(words, responses[i].destinationEntry.DialogueText);
				if (score > maxScore) {
					selectedResponse = i;
					maxScore = i;
				}
				
				Debug.Log("UI: Score: " + score + " " + responses[i].destinationEntry.DialogueText);
			}

			if (selectedResponse > -1) {
				Debug.Log("Selected " + responses[selectedResponse].destinationEntry.DialogueText);
				Debug.Log("Selected " + selectedResponse);
				OnClick(selectedResponse);
			} else {
				Debug.Log("No response selected");
			}
		}
	}

	private static float scoreResponse(string[] src, string test)
	{
		float score = 0.0f;
		float matchCnt = 0.0f;
		string[] dst = test.Split(' ');
		
		int len = Mathf.Min(src.Length, dst.Length);
		
		for (int i=0; i<len; i++) {
			if (FuzzyMatch.LevenshteinDistance(src[i], dst[i]) <= 1) {
				matchCnt += 1.0f;
			}
		}
		
		if (matchCnt > 0.0f) {
			score = matchCnt / (float)Mathf.Max(src.Length, dst.Length);
		}

		return score;	
	}
}


Re: Dialogue System: Ending conversation on OnClick

Posted: Sat Feb 25, 2023 4:14 pm
by Tony Li
Hi,

The StandardDialogueUI.OnClick() method should receive a Response object, not an int. Try changing line 65 from:

Code: Select all

OnClick(selectedResponse);
to:

Code: Select all

OnClick(responses[selectedResponse]);