stop silly recursion. Implement init vers of question answering

This commit is contained in:
WillJeynes
2026-08-24 17:15:52 +01:00
parent f4e5de25c5
commit 7fa82ce8ec
8 changed files with 193 additions and 13 deletions
+63 -9
View File
@@ -10,7 +10,7 @@ using Utils;
public class DialogManager : Singleton<DialogManager>
{
private int currentGuard = 0;
private GuardManager currentGuard;
private JObject dialogTree;
@@ -24,7 +24,11 @@ public class DialogManager : Singleton<DialogManager>
private Transform QuestionOptionArea;
private Transform QuestionInfillArea;
private Stack<string> FullQuestionPrefixes = new Stack<string>();
private string FullQuestion;
private int recursiveDepth = 0;
public int MaxRecursiveDepth = 1;
private Stack<(string key, JObject parent, GameObject infil)> questionStack = new();
@@ -42,13 +46,15 @@ public class DialogManager : Singleton<DialogManager>
}
[ContextMenu("Debug Something")]
//start from right click
[ContextMenu("Debug Questioning")]
private void DebugSpeech()
{
StartDialog(0);
StartDialog(LevelLoader.Instance.Guards[0]);
}
public void StartDialog(int guardId)
//public interface to start interaction
public void StartDialog(GuardManager guardId)
{
dialogTree = LevelLoader.Instance.SpeechTree;
@@ -90,21 +96,22 @@ public class DialogManager : Singleton<DialogManager>
}
else if (kvp.Key == "QUESTION")
{
recursiveDepth++;
FullQuestionPrefixes.Push((kvp.Value as JValue).ToString());
currentItem = dialogTree;
PopulateQuestionOptions();
}
else
else if (!kvp.Key.StartsWith("NOREC_") || recursiveDepth < MaxRecursiveDepth)
{
PopulateSingleQuestionOption(kvp);
}
}
}
private void PopulateSingleQuestionOption(KeyValuePair<string, JToken> kvp)
{
var option = Instantiate(QuestionOptionPrefab, QuestionOptionArea);
option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key;
option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key.Replace("NOREC_", "");
option.GetComponent<Button>().onClick.AddListener(() =>
{
@@ -129,7 +136,7 @@ public class DialogManager : Singleton<DialogManager>
private void AddQuestionInfill(JObject parent, string item)
{
var infil = Instantiate(QuestionInfillPrefab, QuestionInfillArea);
infil.GetComponent<TextMeshProUGUI>().text = item;
infil.GetComponent<TextMeshProUGUI>().text = item.Replace("NOREC_", "");
questionStack.Push((item, parent, infil));
}
@@ -137,14 +144,61 @@ public class DialogManager : Singleton<DialogManager>
public void BackButtonPress()
{
AskButton.SetActive(false);
var (_, parent, infil) = questionStack.Pop();
var (item, parent, infil) = questionStack.Pop();
currentItem = parent;
Destroy(infil);
//this might be buggy, or might miraculuisly work
//if options start dissapearing randomly, look here
if (item.StartsWith("NOREC_"))
{
recursiveDepth--;
FullQuestionPrefixes.Pop();
}
PopulateQuestionOptions();
}
public void SubmitButtonPress()
{
//formulate query string in format PERPSECTIVE%ITEM#PROPERTY
//slightly wack way of doing it, hoping it is future proof
while (FullQuestionPrefixes.Count > 0)
{
FullQuestion = FullQuestionPrefixes.Pop() + FullQuestion;
}
Debug.LogError(FullQuestion);
string q = "";
while (questionStack.Count > 0)
{
var (item, _, go) = questionStack.Pop();
Destroy(go);
q += item;
if (item.StartsWith("guard "))
{
FullQuestion = FullQuestion.ReplaceLast("GS", "G" + (int.Parse(item[^1].ToString())-1));
}
if (item.StartsWith("door "))
{
FullQuestion = FullQuestion.ReplaceLast("DS", "D" + (int.Parse(item[^1].ToString())-1));
}
}
Debug.LogError(q);
Debug.LogError(FullQuestion);
//now get a response from the guard being questioned
var response = currentGuard.Answer(FullQuestion);
Debug.Log(response);
//reset for next question
currentItem = dialogTree;
recursiveDepth = 0;
PopulateQuestionOptions();
}
}