English 中文(简体)
• 为我的团结游戏制定分管算法,但遇到外围观察: 背 景
原标题:Tried creating dijkstras algorithm for my unity game but encountering OutOfMemoryException: Out of memory Error
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using TMPro;
using Unity.Profiling.Editor;
using Unity.VisualScripting;
using UnityEngine;

public class Djikstras : MonoBehaviour
{
    public GameObject A;
    public GameObject B;
    public GameObject C;
    public GameObject D;
    public GameObject E;
    public GameObject F;
    public GameObject G;
    public GameObject startingNode;
    public GameObject finalNode;
    public bool isTravelling = false;
    public void djikstras()
    {
        isTravelling = true;
        GameObject[] a = { B, C };
        GameObject[] b = { A, C, D };
        GameObject[] c = { A, B, C };
        GameObject[] d = { B, C, E, F };
        GameObject[] e = { D, F, G };
        GameObject[] f = { E, D, G };
        GameObject[] g = { F, E, };
        GameObject[][] nodesRelations = { a, b, c, d, e, f, g };
        GameObject[] nodes = { A, B, C, D, E, F, G };
        float[] shortdist = { 0, 0, 0, 0, 0, 0, 0 };
        GameObject[] prevNodes = { A, B, C, D, E, F, G };
        List<GameObject> visitedNodes = new List<GameObject>();
        GameObject currentNode;
        float dist = 0;
        GameObject lowestNode = A;
        currentNode = startingNode;
        while (visitedNodes.Count < 7)
        {
            while (currentNode != finalNode)
            {
                GameObject[] currentArray = a;
                int count = 0;
                for (int i = 0; i < nodes.Length; i++)
                {
                    if (currentNode == nodes[i]){}
                    else
                    {
                        count = count + 1;
                    }
                }
                currentArray = nodesRelations[count];
                nodes.RemoveAt(0);
                float lowestnodedis = 1000;
                for (int i = 0; i < currentArray.Length; i++)
                {
                    float dis = Mathf.Abs(Mathf.Sqrt(Mathf.Pow((currentNode.transform.position.x - currentArray[i].transform.position.x), 2) + Mathf.Pow(currentNode.transform.position.y - currentArray[i].transform.position.y, 2)));
                    if (dis < lowestnodedis)
                    {
                        lowestnodedis = dis;
                        lowestNode = currentArray[i];
                    }
                    dis = dis + dist;
                    int index = 0;
                    for (int j = 0; j < nodes.Length; j++)
                    {
                        if (nodes[j] == currentArray[i]){}
                        else
                        {
                            index = index + 1;
                        }
                    }
                    if (dis < shortdist[index])
                    {
                        shortdist[index] = dis;
                        prevNodes[index] = currentNode;
                    }
                }
                visitedNodes.Add(currentNode);
                currentNode = lowestNode;
            }
        }
        Stack<GameObject> shortestPath = new Stack<GameObject>();
        GameObject followthroughnode = finalNode;
        while (followthroughnode != startingNode)
        {
            int i = 0;
            for (int z = 0; z < nodes.Length; z++)
            {
                if (nodes[z] == followthroughnode){}
                else
                {
                    i = i + 1;
                }
            }
            shortestPath.Push(followthroughnode);
            followthroughnode = prevNodes[i];
        }
        enemyMovement(shortestPath, finalNode);
    }
    void enemyMovement(Stack<GameObject> stack, GameObject final)
    {
        float speed = 1f;
        while(transform.position != final.transform.position)
        {
            GameObject nextNode = stack.Pop();
            Vector3 nextPosition = new Vector3(nextNode.transform.position.x, nextNode.transform.position.y, -1);
            transform.position = Vector3.MoveTowards(transform.position,nextPosition, speed*Time.deltaTime);
        }
        isTravelling = false;
    }
    void Update()
    {
        while(isTravelling == false)
        {
            djikstras();
        }
    }
}

这里的错误信息是:

OutOfMemoryException: Out of memory
System.Collections.Generic.List`1[T].set_Capacity (System.Int32 value) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
System.Collections.Generic.List`1[T].EnsureCapacity (System.Int32 min) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
System.Collections.Generic.List`1[T].AddWithResize (T item) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
Djikstras.djikstras () (at Assets/Scripts/Djikstras.cs:109)
Djikstras.Update () (at Assets/Scripts/Djikstras.cs:156)

我是一位学生,目前正在制作一个团结的录像。 这是我为敌人在我的游戏中使用的一种文字,这种游戏是建立在最低道路算法的基础之上的。

算法应当从目前敌对特性的起始点和最后需要达到的终点。 算法通过使用阵列对 no进行模拟和模拟,每一阵列均符合节点,并持有与其相关的节点。 实际游戏物体可与作为成分的敌方特性(绿地)一起看待。

( https://i.stack.imgur.com/abxF4.png)

之后,该方案采用分界线算法,在两条点之间找到最短的道路,将订单推向一个分点,然后将分界线移至移动功能。 之后,该运动的功能冲淡了每一条 no子,把敌人的特性移至那个 no子,直到它到达最后的终点。

我确信,这一错误的产生是由于正在创建的猎物阵阵阵阵列的数量,但并不肯定如何接手。

问题回答

我在执行A*算法时也曾遇到过类似的问题。

记忆离开空间的原因是,你法典中的漏洞之一从未停止过。

考虑到所有频率都在单一框架内运行。 Debug. Carlo()赢得了笔工作,阵列将迎来你们的记忆,然后团结会让你们放弃游戏。

我看不出该守则有错之处,但我建议使用《最新情况》(最新情况)的漏洞,直到你说明你从来就永远不会结束。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签