【问题标题】:C# Unity Queue emptying itselfC# Unity 队列清空自身
【发布时间】:2019-09-26 16:40:21
【问题描述】:

我无法找出错误。我的游戏初始化了几个队列来存储游戏提示,在“开始”按钮中,函数StartDialogue() 填充每个队列并转到DisplayNext()DisplayNext() 函数应该从某些队列中取出第一个提示并将它们放入对话框中。之后,玩家选择正确的按钮进行回答,并调用DisplayNext()进入下一个提示。

但是,我的问题是,当正确的按钮调用 DisplayNext() 时,我的 sen_queue 计数将自身设置为 0。当按下“开始”时,调试输出首先显示 4,但当按下正确的按钮时,它显示 0。第一次按下正确的按钮时,它会转到 EndDialogue() 函数。我在下面附上了我的代码。我不知道如何解决这个问题,所以任何帮助将不胜感激!非常感谢您! 但是,我的问题是当正确的按钮调用DisplayNext() 时,我的sen_queue 计数将自身设置为0。当按下“开始”时,调试输出首先显示 4,但当按下正确的按钮时,它显示 0。第一次按下正确的按钮时,它将转到 EndDialogue() 函数。我在下面附上了我的代码。我不知道如何解决这个问题,所以任何帮助将不胜感激!提前非常感谢您!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class QuestionManager : MonoBehaviour
{
    public Text dialogueText;
    public Text topText;
    public Text botText;

    public Queue<string> sen_queue;
    public Queue<string> top_queue;
    public Queue<string> bot_queue;
    public Queue<string> wrong_queue;

    private bool prev_wrong;

    public GameObject finalsaltwater;
    public GameObject water;
    public GameObject sand;
    public GameObject salt;
    public GameObject filtersand;
    public GameObject platewater;


    void Start()
    {
        Screen.SetResolution(1600, 900, true);

        sen_queue = new Queue<string>();
        top_queue = new Queue<string>();
        bot_queue = new Queue<string>();
        wrong_queue = new Queue<string>();
        prev_wrong = false;

        finalsaltwater.SetActive(false);
        filtersand.SetActive(false);
        platewater.SetActive(false);
    }


    public void StartDialogue(Question question, Question topprompt, Question botprompt, Question wrong)
    {
        sen_queue.Clear();
        top_queue.Clear();
        bot_queue.Clear();
        wrong_queue.Clear();

        foreach (string sentence in question.sentences)
        {
            sen_queue.Enqueue(sentence);
        }
        foreach (string sentence in topprompt.sentences)
        {
            top_queue.Enqueue(sentence);
        }
        foreach (string sentence in botprompt.sentences)
        {
            bot_queue.Enqueue(sentence);
        }
        foreach (string sentence in wrong.sentences)
        {
            wrong_queue.Enqueue(sentence);
        }

        DisplayNext();
    }


    IEnumerator ToggleFalse(GameObject thing)
    {
        yield return new WaitForSeconds(1.5f);
        thing.SetActive(false);
    }


    IEnumerator ToggleTrue(GameObject thing)
    {
        yield return new WaitForSeconds(1.5f);
        thing.SetActive(true);
    }


    public void DisplayNext()
    {
        Debug.Log("IN DISPLAY " + sen_queue.Count);

        if (sen_queue.Count == 0)
        {
            EndDialogue();
            return;
        }

        if (top_queue.Count == 2)
        {
            platewater.SetActive(true);
            water.SetActive(false);
            StartCoroutine(ToggleFalse(salt));
        } else if (top_queue.Count == 1)
        {
            filtersand.SetActive(true);
            sand.SetActive(false);
            platewater.SetActive(false);
            StartCoroutine(ToggleTrue(finalsaltwater));
        } else if (top_queue.Count == 0)
        {
            platewater.SetActive(true);
            StartCoroutine(ToggleTrue(salt));
            StartCoroutine(ToggleFalse(platewater));
            finalsaltwater.SetActive(false);
        }

        string sentence = sen_queue.Dequeue();

        if (top_queue.Count != 0)
        {
            string topbutton = top_queue.Dequeue();
            topText.text = topbutton;
        }
        if (bot_queue.Count != 0)
        {
            string botbutton = bot_queue.Dequeue();
            botText.text = botbutton;
        }
        prev_wrong = false;
        dialogueText.text = sentence;
    }


    public void IncorrectDisplay()
    {
        if (wrong_queue.Count == 0)
        {
            return;
        }

        if (prev_wrong == false)
        {
            string wrong_sen = wrong_queue.Dequeue();
            dialogueText.text = wrong_sen;
        }
        prev_wrong = true;
    }


    public void EndDialogue()
    {
        dialogueText.text = "Good job! Refresh the page if you want to restart.";
        return;
    }

}

【问题讨论】:

  • 你试过debugging吗?
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码. 没有明确问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Reproducible Example.

标签: c# unity3d queue 2d


【解决方案1】:

我有点晚了,但我遇到了同样的问题。即使你已经解决了问题,也许我将来可以帮助其他人! 它与调用顺序有关。在我的代码中,我在 2 个不同的类中有 2 个 Start() 方法。

在 DialogueTrigger 类中:

public Dialogue dialogue;

public void Start()
{
    triggerDialogue();
}

public void triggerDialogue()
{
    FindObjectOfType<DialogueManager>().startDialogue(dialogue);
}

在 DialogueManager 类中:

public Queue<string> sentences;
private bool dialogueStarted;

// Start is called before the first frame update
void Start()
{
    sentences = new Queue<string>();

}

private void Update()
{
    if(dialogueStarted && Input.GetKeyDown(GameObject.Find("Player").GetComponent<Controls>().confirm))
    {
        displayNextSentence();
    }
}

public void startDialogue(Dialogue dialogue)
{

    print("start:" + dialogue.name);
    sentences.Clear();

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

    displayNextSentence();
    dialogueStarted = true;
}

public void displayNextSentence()
{
    if (sentences.Count == 0)
    {
        endDialogue();
        return;
    }

    string sentence = sentences.Dequeue();

    Debug.Log(sentence);
}


private void endDialogue()
{
    Debug.Log("End of dialogue");
    dialogueStarted = false;
}

当按下一个键(空格键)时,我让下一个字符串进入,而不是单击一个按钮,但它应该工作相同。 问题是 DialogueManager 的 Start() 在 startDialogue() 方法(在 DialogueTrigger 的 Start() 中调用)之后被调用,第二个被调用。这将重置整个队列,使其为空,从而将长度设置为 0。这意味着按下键不会重置队列,而是自动发生! 在您的代码中,由于某种原因,在按下按钮后会调用 Start() 方法。 解决方案是将以下代码放在 StartDialogue() 方法的顶部:

if (sentences == null)
{
    sentences = new Queue<string>();
}

您也可以省略 if 语句,然后使用此代码代替 Clear() 方法。我没有测试过这个,但结果应该是一样的。如果要使用 Clear(),最好使用 if 语句。如果您仍然保留它,您将重置队列两次,这是不必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2016-10-07
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多