I am having this bug/issue where when I click on an option button, the dialogue is not being progressed to the children of the node even though the player node has child nodes.
I assume the NextDialogue() in PlayerConversant is working because when I click on the next button for NPC dialogue, it progresses. I tried a Debug log in the SelectOption() function to see if that was being called when I clicked on the button and it does not seem to be printing but I am not sure why.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace RPG.Dialogue
{
public class PlayerConversant : MonoBehaviour
{
[SerializeField] Dialogue testDialogue;
Dialogue currentDialogue;
DialogueNode currentNode = null;
bool isSelecting = false;
public event Action onDialogueUpdated;
IEnumerator Start()
{
yield return new WaitForSeconds(2f);
StartDialogue(testDialogue);
}
public void StartDialogue(Dialogue newDialogue)
{
currentDialogue = newDialogue;
currentNode = currentDialogue.GetRootNode();
onDialogueUpdated();
}
public void QuitDialogue()
{
currentDialogue = null;
currentNode = null;
isSelecting = false;
onDialogueUpdated();
}
public bool IsActive()
{
return currentDialogue != null;
}
public bool IsSelectingOption()
{
return isSelecting;
}
public string GetText()
{
if (currentNode == null)
{
return "";
}
return currentNode.GetText();
}
public IEnumerable<DialogueNode> GetOptions()
{
return currentDialogue.GetPlayerChildren(currentNode);
}
public void SelectOption(DialogueNode chosenNode)
{
Debug.Log("Option selected");
currentNode = chosenNode;
isSelecting = false;
NextDialogue();
}
public void NextDialogue()
{
// If the dialogue is by player and there is more than one response, enter selection mode in dialogue
int numOfPlayerOptions = currentDialogue.GetPlayerChildren(currentNode).Count();
if (numOfPlayerOptions > 0)
{
isSelecting = true;
onDialogueUpdated();
return;
}
DialogueNode[] childNodes = currentDialogue.GetNPCChildren(currentNode).ToArray();
int randomIndex = UnityEngine.Random.Range(0, childNodes.Count());
currentNode = childNodes[randomIndex];
onDialogueUpdated();
}
public bool HasNext()
{
// If the current node has children, return true as it can progress to next dialogue, otherwise return false
return currentDialogue.GetAllChildren(currentNode).Count() > 0;
}
}
}
private void BuildOptionsList()
{
foreach (Transform item in optionRoot)
{
Destroy(item.gameObject);
}
foreach (DialogueNode option in playerConversant.GetOptions())
{
GameObject optionButtonInstance = Instantiate(optionButtonPrefab, optionRoot);
var buttonText = optionButtonInstance.GetComponentInChildren<TextMeshProUGUI>();
buttonText.text = option.GetText();
Button button = optionButtonInstance.GetComponentInChildren<Button>();
button.onClick.AddListener(() => playerConversant.SelectOption(option)); // Function is only called when button is clicked
}
}
Ive attached my PlayerConversant.cs and the BuildOptionList() from DialogueUI.cs as that is where I attached the button listener (SelectionOption()). Any help is much appreciated!