So I am trying to take a set of predefined Phrases and transfer them over to a dictionary at the start of a scene. It seems to add the phrases just fine. However whenever I say the predefined word i get this error:
IndexOutOfRangeException: Array index is out of range.
Recognition+<Start>c__AnonStorey0.<>m__1 () (at Assets/Recognition.cs:40)
Recognition.PhraseRecog (PhraseRecognizedEventArgs args) (at Assets/Recognition.cs:75)
UnityEngine.Windows.Speech.PhraseRecognizer.InvokePhraseRecognizedEvent (System.String text,
ConfidenceLevel confidence, UnityEngine.Windows.Speech.SemanticMeaning[] semanticMeanings,
Int64 phraseStartFileTime, Int64 phraseDurationTicks) (at
C:/buildslave/unity/build/Runtime/Export/Windows/Speech.cs:166)
My code is as follows
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Windows.Speech;
using UnityEngine;
public class Recognition : MonoBehaviour
{
KeywordRecognizer keywordRecognizer;
Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
public bool savana_Initialized;
public string[] DataPaths;
public string[] Phrases;
void Start()
{
savana_Initialized = true;
Debug.Log(savana_Initialized);
keywords.Add("Hey, Savannah", () =>
{
savana_Initialized = true;
});
int pathIndex = 0;
foreach (string phrase in Phrases)
{
Debug.Log(pathIndex);
if (DataPaths[pathIndex] != null || DataPaths[pathIndex] != "" || pathIndex < DataPaths.Length)
{
keywords.Add(phrase, () =>
{
ExecuteFunction(DataPaths[pathIndex]);
});
//Debug.Log("Added " + phrase + " with path " + DataPaths[pathIndex]);
pathIndex = pathIndex + 1;
}
else
{
return;
}
}
foreach(KeyValuePair<string, System.Action> kvp in keywords)
{
Debug.Log(string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value.ToString()));
}
keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());
keywordRecognizer.OnPhraseRecognized += PhraseRecog;
keywordRecognizer.Start();
}
void PhraseRecog(PhraseRecognizedEventArgs args)
{
System.Action keywordAction;
if(keywords.TryGetValue(args.text, out keywordAction))
{
keywordAction.Invoke();
}
}
void ExecuteFunction(string dataPath)
{
if( savana_Initialized == true)
{
System.Diagnostics.Process.Start(dataPath);
}
else{
return;
}
}
}
Is it even possable to achieve this feat dynamically adding System.Actions to a Dictionary? Any help would be greatly appreciated!
-Ledge