【问题标题】:Data loss in Unity with Updates and IEnumeratorsUnity 中的数据丢失与更新和 IEnumerators
【发布时间】:2017-11-20 13:52:34
【问题描述】:

所以我的代码不完整......但它也很简单......我认为如果你能回答,你也足够了解能够使用 Visual Studios Intellisense 并自动填充一些东西......对不起,度过了一个漫长的夜晚。

我有三个主要脚本...这是链中的主要链接,它传递它接收到的数据。及以后.. 但是,当它过滤它用于 AddToStack() 的方法时;然而输出是......

正在发送...1 (x91) 正在接收...1 (x9)

现在我已经对其进行了调整,让它从 90 到 10 流动,但仍然......我不明白为什么 90% 的数据正在下降......我是在搞砸 Unity Engine 的流动还是什么? XD 我有点不喜欢 Unity。

脚本如下:

1.) Primary - 如果 dataStrength 小于阈值,则延迟发送数据,如果大于或等于阈值,则不延迟发送数据。 2.) 使用更新方法检查公共输入是否大于 0,如果是,则启动将输入保存到列表的方法,然后将输入重置为 0 以进行下一次输入。然后当按下“A”时,它会遍历列表并输出总和。

脚本一(有条件地发送数据)

private float totalStrength = 0;
public float activationThreshold;
public float signalStrength;
public bool showPackets = true;
public bool showFullDebug = true;
public Stack<float> signals = new Stack<float>();


void Start ()
{
    nodeGrowthThreshold = 60; //Just temporary arbitrary numeric values...
    activationThreshold = 20; //The default values if not set.
    path = GetComponent<Path>();
}

private void Update()
{
    if (signalStrength > 0)
    {
        if (numRequests.Equals(nodeGrowthThreshold)) { StartCoroutine(NodeGrow()); StopCoroutine(NodeGrow()); }

        if (totalStrength >= activationThreshold)
        {
            StartCoroutine(AddToStack());
            StopCoroutine(AddToStack());
            signalStrength = 0;
            if (showPackets || showFullDebug) { StartCoroutine(ReadStack()); StopCoroutine(ReadStack()); }
            StartCoroutine(WriteStack());
            StopCoroutine(WriteStack());

            totalStrength -= activationThreshold;
            signalStrength = 0;
            signals.Clear();
        }
        else if (totalStrength < activationThreshold)
        {
            StartCoroutine(AddToStack());
            StopCoroutine(AddToStack());
            signalStrength = 0;
        }


    }
    if (showFullDebug) { print("Node receieved: " + numRequests + " requests."); }
}

private IEnumerator AddToStack()
{
    if (!showFullDebug) { print("Node added signal: " + signalStrength + " to storage stack"); }
    signals.Push(signalStrength);
    numRequests++;
    totalStrength += signalStrength;
    yield return null;
}



private IEnumerator WriteStack()
{
    if (showFullDebug) { print("Node is passing stack of stack size..." + signals.Count); }
    if (signals.Count > 0)
    {
        foreach (float signal in signals.ToArray()) { path.input = signal;  if (showFullDebug) { print(signal); } print("Sending... " + signal); /**NOT CAPTURING DATA FAST ENOUGH. DATA IS SENDING FAST ENOUGH THOUGH.**/} }
    yield return null;
}

脚本二(接收者)

public class Out : MonoBehaviour
{
    public float input = 0;
    private float savedOutput = 0;
    public bool showFullDebug = true;
    public List<float> savedInput = new List<float>();
    public Queue<float> output = new Queue<float>();

    private void Update()
    {
        if (input > 0) {
            //StartCoroutine(Save());
            //StopCoroutine(Save());\
            Save();
            input = 0;
        }
        if (Input.GetKeyDown(KeyCode.A)) { print(savedOutput); }
    }

    private void Save()
    {
        savedInput.Add(input);
        output.Enqueue(input);
        foreach (float num in savedInput.ToArray()) { savedOutput += num; }
        //yield return null;
    }
}

感谢任何可以提供帮助的人。

注意事项: - 我使用的数据显然只是一个 1 用于通过 100 次模拟的浮点数。 - 正如你所看到的,我已经尝试过 IEnumerators 和通常的方法......但不确定为什么数据会被丢弃..

【问题讨论】:

  • 顶一下,因为我发的晚了,可能已经丢在一堆了。

标签: c# oop unity3d


【解决方案1】:

事实证明,我的答案的最佳解决方案是在我的接收类中使用 Setter 并设置它,这样它就可以在任何时候更改它时执行所有必要的操作,而不仅仅是检查它是否经常更改。

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 2018-05-05
    • 2018-03-16
    • 2021-12-10
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多