【发布时间】:2018-05-03 18:29:58
【问题描述】:
好的,我的游戏中有一个对话系统。它的设计非常简单。每个对话都包含一个节点列表,这将是 NPC 所说的内容,并且对于每个节点,还有一个选项列表,玩家可以从中选择结束对话或继续对话。对话部分完美无缺。我想要做的是将它与一个任务系统结合起来。在我深入了解我的任务系统之前,我需要想办法将我的对话系统连接到我的任务系统或 NPC(最好是我的 NPC)。我的对话系统设置方式是单例模式,每个 NPC 只需调用其上的一个方法,根据其本地对话变量开始与玩家对话。
我一直坐在这里思考如何将一个值从我的对话管理器传递给我的 NPC,但是考虑到我的 run 方法是一个协程,我无法弄清楚退出后如何返回该值如果需要的话,协程。我觉得这应该是可能的,但是,我真的想不出办法来做到这一点。任何帮助,将不胜感激。
理想的情况是让RunDialogue 方法返回一个变量类型(bool?),但只有在EndDialogue 从run 方法中调用之后。如果它返回 true,则分配任务,否则什么也不做。
来自DialogueManager:
public void RunDialogue(Dialogue dia)
{
StartCoroutine(run(dia));
}
IEnumerator run(Dialogue dia)
{
DialoguePanel.SetActive(true);
//start the convo
int node_id = 0;
//if the node is equal to -1 end the conversation
while (node_id != -1)
{
//display the current node
DisplayNode(dia.Nodes[node_id]);
//reset the selected option
selected_option = -2;
//wait here until a selection is made by button click
while (selected_option == -2)
{
yield return new WaitForSeconds(0.25f);
}
//get the new id since it has changed
node_id = selected_option;
}
//the user exited the conversation
EndDialogue();
}
来自NPC:
public override void Interact()
{
DialogueManager.Instance.RunDialogue(dialogue);
}
【问题讨论】:
-
这样做的问题是,想要检索返回值的函数必须等待协程结束,如果它是一个正常的同步函数,它只会停止程序。