首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#/Unity: NPC跳过对话行

C#/Unity: NPC跳过对话行
EN

Stack Overflow用户
提问于 2018-12-11 23:41:43
回答 1查看 320关注 0票数 0

我试图在我的游戏中使用一个对话管理器来处理全国人民代表大会的发言,但我遇到了一些麻烦。

我有两个NPC;如果我只和其中一个说话,它会遍历它们的所有行,没有问题(每个NPC有五行代码,由PC走向它们并按下空格键触发)。但是,如果我先与NPC1通信,然后再与NPC2通信,那么即使我返回到NPC1,它也只会显示npc2的语音。

NPC.cs

代码语言:javascript
复制
public class NPC : Character {
    private bool charInRange;
    public Dialogue dialogue;
    public bool talkedTo = false;

    // Use this for initialization
    void Start () {
        charInRange = false;
    }

    // Update is called once per frame
    void Update () {
        //if player is in range and presses space, triggers NPC dialogue
        if (charInRange && Input.GetKeyDown(KeyCode.Space))
        {
            TriggerDialogue();
        }
    }

    //if Player gameObject is in NPC collider, player is in range
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.tag == "Player")
        {
            charInRange = true;
        }
    }

    //if player exits NPC collider, player is not in range
    void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            charInRange = false;
        }
    }

    //if NPC has been talked to before, displays next sentence; if not, loads dialogue and displays first sentence
    private void TriggerDialogue()
    {
        if (!talkedTo)
        {
            talkedTo = true;
            FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
        }
        else
        {
            FindObjectOfType<DialogueManager>().DisplayNextSentence();
        }
    }
}

DialogueManager.cs

代码语言:javascript
复制
public class DialogueManager : MonoBehaviour {

private Queue<string> sentences;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

//loads a queue with lines from Dialogue and displays first sentence
public void StartDialogue(Dialogue dialogue)
{
    sentences = new Queue<string>();

    foreach (string sentence in dialogue.sentences)
    {
        sentences.Enqueue(sentence);
    }

    DisplayNextSentence();
}

//displays next sentence in the queue
public void DisplayNextSentence()
{
    Debug.Log(sentences.Count);

    //if no more sentences in the queue, end the dialogue
    if(sentences.Count == 0)
    {
        EndDialogue();
        return;
    }

    string sentence = sentences.Dequeue();
    Debug.Log(sentence);
}

//ends dialogue
private void EndDialogue()
{
    Debug.Log("CONVERSATION OVER");
}
}

我知道问题是什么:一旦与NPC2交谈,talkedTo在NPC.cs中翻转,np2的对话开始,np1的对话队列被清除。然后,它简单地显示NPC2的下一句话,而不管玩家在哪个对撞机中。我的问题是,我不确定如何解决这个问题。我应该在特定的NPC中存储特定的对话吗?如果是这样,我该怎么做呢?

任何和所有的帮助都是感激的。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-12-12 00:45:07

@Eddge在评论中的回答:

是的,你可以在每个NPC上存储队列,当你与NP交谈时,让对话系统显示队列中的下一条消息,而不是让NPC在对话系统上调用start,然后让对话系统存储NPC发送的对话的队列。

谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53727575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档