Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question maxed out array int, what to do to store more informations in array

Discussion in 'Scripting' started by qz154772610, Apr 29, 2021.

  1. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    I am working on a 2D open-world project, so I made a grid system, but it seems can't go up to 50000 * 50000 grid space, unity saying its "out of memory exception", I am assuming this is because 50000*50000 is more than int max number. works fine with 5000*5000 tho, but I want it to be bigger.

    my questions is how do i store more informations in array like this
    Code (CSharp):
    1. public GridData[,] gridData;
    and this is my whole script code
    maybe i am wrong about what i assumed, but if so please tell me how to fix it, thank you!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.U2D;
    5.  
    6. public class GridSystem
    7. {
    8.     int GridWidth;
    9.     int GridHeight;
    10.     float GridCellSize;
    11.     SpriteAtlas TerrainSpriteAtlas;
    12.  
    13.     //------------this is the important part----------
    14.     //i made a grid in other script as:
    15.     //Grid = new GridSystem(50000,50000,3, TerrainAtlas);
    16.     public GridData[,] gridData;
    17.     //------------------------------------------------
    18.  
    19.     List<GameObject> currentInSceneGridTiles = new List<GameObject>();
    20.  
    21.     //the creating method
    22.     public GridSystem(int width, int height, float cellSize,SpriteAtlas terrainAtlas)
    23.     {
    24.         GridWidth = width;
    25.         GridHeight = height;
    26.         GridCellSize = cellSize;
    27.         TerrainSpriteAtlas = terrainAtlas;
    28.  
    29.         gridData = new GridData[width,height];
    30.  
    31.         for(int x = 0; x < width; x++)
    32.         {
    33.             for (int y = 0; y < height; y++)
    34.             {
    35.                 gridData[x, y] = new GridData(x,y);
    36.             }
    37.         }
    38.     }
    39.  
    40.     public Vector2  GetGridWorldPos(int x, int y)
    41.     {
    42.         return new Vector2(x*GridCellSize,y*GridCellSize);
    43.     }
    44.     public void GetGridXYAxis(Vector2 WorldPos,out int x, out int y)
    45.     {
    46.  
    47.         x = Mathf.FloorToInt(WorldPos.x / GridCellSize);
    48.         y = Mathf.FloorToInt(WorldPos.y / GridCellSize);
    49.     }
    50.  
    51.     public void RefreshGridVisual(Vector2 CurrentCamPosition)
    52.     {
    53.         int x;
    54.         int y;
    55.  
    56.         GetGridXYAxis(CurrentCamPosition, out x, out y);
    57.  
    58.         List<GameObject> removedObj = new List<GameObject>();
    59.  
    60.         foreach (GameObject Obj in currentInSceneGridTiles)
    61.         {
    62.             int currentObjGridX;
    63.             int currentObjGridY;
    64.  
    65.             GetGridXYAxis(Obj.transform.position, out currentObjGridX, out currentObjGridY);
    66.  
    67.             if (currentObjGridX-x < -10|| currentObjGridX - x > 10 || currentObjGridY - y < -10 || currentObjGridY - y > 10)
    68.             {
    69.                 removedObj.Add(Obj);
    70.                 GameObject.Destroy(Obj);
    71.             }
    72.         }
    73.  
    74.         foreach(GameObject Obj in removedObj)
    75.         {
    76.             currentInSceneGridTiles.Remove(Obj);
    77.         }
    78.  
    79.  
    80.  
    81.         for (int gridx = x-10; gridx <= x+10; gridx++)
    82.         {
    83.  
    84.             for (int gridy = y- 10; gridy <= y+ 10; gridy++)
    85.             {
    86.  
    87.                 if (gridx >= 0 && gridx < GridWidth && gridy >= 0 && gridy < GridHeight)
    88.                 {
    89.                     if(gridData[gridx, gridy].currentGridTerrainObj == null)
    90.                     {
    91.                         GameObject currentGridTile = new GameObject("GridTile", typeof(SpriteRenderer));
    92.  
    93.                         gridData[gridx, gridy].currentGridTerrainObj = currentGridTile;
    94.                         currentInSceneGridTiles.Add(currentGridTile);
    95.                         currentGridTile.transform.position = GetGridWorldPos(gridx, gridy);
    96.                         currentGridTile.GetComponent<SpriteRenderer>().sprite = GetGridTerrainSprite(gridData[gridx, gridy]);
    97.  
    98.                     }
    99.                     else
    100.                     {
    101.  
    102.                     }
    103.                 }
    104.  
    105.             }
    106.         }
    107.  
    108.     }
    109.     public Sprite GetGridTerrainSprite(GridData gridData)
    110.     {
    111.         if (gridData.gridType == GridType.GrassLand)
    112.         {
    113.             return TerrainSpriteAtlas.GetSprite("GrassLandSprite");
    114.         }
    115.         else
    116.         {
    117.             return null;
    118.         }
    119.     }
    120.     public void test()
    121.     {
    122.         int x=5;
    123.         int y=5;
    124.  
    125.         for (int gridx = x - 5; gridx < x + 5; gridx++)
    126.         {
    127.  
    128.             for (int gridy = y - 5; gridy < y + 5; gridy++)
    129.             {
    130.                 if (gridx >= 0 && gridx < GridWidth && gridy >= 0 && gridy < GridHeight)
    131.                 {
    132.                     Debug.Log("this is working");
    133.                 }
    134.  
    135.             }
    136.         }
    137.     }
    138.  
    139. }
    140. public class GridData
    141. {
    142.     int gridXPos;
    143.     int gridYPos;
    144.  
    145.     public GameObject currentGridTerrainObj;
    146.  
    147.     public GridType gridType;
    148.  
    149.     public GridData(int x, int y)
    150.     {
    151.         gridXPos = x;
    152.         gridYPos = y;
    153.  
    154.         gridType = GridType.GrassLand;
    155.     }
    156.  
    157.  
    158. }
    159. public enum GridType
    160. {
    161.     None,
    162.     GrassLand
    163. }
     
    Last edited: Apr 29, 2021
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    That's 2,500,000,000. You could use long for the x,y indexes in that array, but this is not the problem. Just think about how much memory you're using...

    2,500,000,000 cells in your grid, that many GridData object. If we only count the x,y coordinates of the GridData (from the
    new GridData(x, y);
    expression, then you have 8 bytes per object.

    That's 20,000,000,000 bytes. 19,531,250 kilobytes. 19,073.48 megabytes. 18.62 Gigabytes.

    Do you have 18.62 gigabytes free memory just for this array? And you haven't stored anything else in it, only two coordinates...

    BTW, what do you want with a gigantic grid like this? You will never (well, question of years and deadlines, obviously) put meaningful content in a space like that.
     
  3. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    ah that sounds like i should stayed as small map?
    how do other openworld games get a map so big and smooth tho.

    for ur question i am trying to make a big world which u can teleport around to get to point fast but u can also try to just walk the road,collect some mineral or herbs on the way, free to do anything is the point

    also i am thinking make a feature that player can build their own town out side the city, like minecraft

    so i want my map to be bigger as possible
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    I don't know what kind of game you're building, but 50000^2 tilemap is gigantic.

    Just FYI:
    Civilization's huge map is 106×66.
    Stardew Valley has maps around from 1200 (~around 20x60) tiles to 3600 (around 45x80) tiles. Number of tiles!

    You can always make bigger maps, but you also need to make content for all the tiles, otherwise your game will be boring.
    If you make bigger maps, you can streaming the content and data in and out of memory as your player moves around. But it is advanced topic.
     
  5. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    well yes for civilization the tile is huge so it doesnt need much tiles, my is bit small(since i want players to build their own town every small tile matter), and for stardew valley it has different scene to load, but i dont want to have a loading screen while playing so i want to put everything in one map. that is why its a problem to me

    however 5000*5000 seem big enough to be, i am just wondering if i canmake it bigger, guess later on if i have more content i will just add other map as island or sonething

    thank you for your help! it helped alot!
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    Well, considering that you didn't understand a simple 'out of memory' exception, I highly doubt it that you are ready to code a memory-streaming application. No offense and all, but open worlds and memory streaming isn't a beginner topic.
    Make games what you are capable of. You will get better and will be able tackle more advanced topics.

    When you start to learn how to ride a bicycle, you don't start with the extreme mountain cliff biking...
     
  7. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    hate to say that ur right, but yeah i am pretty amateur on this.

    always wanna make a game have huge content like world of warcraft, guess i can not yet XD

    thank you for ur help again!
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    Don't worry, me too. :D
    But resist the urge, postpone it for now. You need a ton of time to write the content anyway, so you can work on the 'creative' parts in the mean time. Just build smaller games too so you get better.
     
    Kurt-Dekker likes this.
  9. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    There is 0 reason to use grid like this.You can use build in coordinates for things you mentioned.You also can create infinite map where new objects will be loaded based on player position for example.
     
    Joe-Censored likes this.
  10. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    What about using some chunk system like Minecraft?
     
  11. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    as you can see i am very new to gamedev, is there any reference or example for build in coordinates or such?
     
  12. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    What do you mean it is just vector 3 position
     
  13. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    i am doing some research in chunk system right now , it looked pretty simmilar to my grid system. except that i have griddata[,] with 2 coordinate, and it has 3 coordinate with chunkData[,,]. not sure how it will change anything since if i get a huge number like [5000,5000,5000] on initiating it will still blow memory out?

    i am now thinking to store small grids with like [50,50] for each grid and then only load with 9 grid from file everytime.

    for example if i am in [0,0] grid position

    my game will load those grid
    [ -1,1] [0,1][1,1]
    [ -1,0] [0,0][1,0]
    [ -1,-1] [0,-1][1,-1]

    and each grid will have [50,50] grid data

    this is only a thought, havent make it working yet
     
  14. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    if u mean only store vector3 and only use data when its on my camera? i am already doing that in my script. however my problem is when the grid number gets too big it get me error with out of memory when creating one
     
  15. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Ye i know but i dont get for what you need so big grid
     
  16. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    well u can think it as i want to create a game like minecraft, i dont need it to be actually infinity, but i want it to be big enough that players can make their own kindom while there are other npc kingdoms
     
  17. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    i get it but there is no reason to have it all in one grid do you think that big games like wow are all in one ? just separate world into locations. And i still dont get what you using grid for?You wrote it to track player position thats dumb if you can do it with siple world position coordinations


    If you really must gave grid just make one grid and divide it in some smaller and then divide them in smaller etc and if player will be in some location just use one small grid.if you was you i will do it just like object so biggest grid will have in constructor list of smaller grids etc
     

    Attached Files:

    Last edited: May 2, 2021
  18. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    using grid made me easier to editing them and easier to manipulate in-game change since i only need to change my GridDataType to change it to another one.

    For example if a player uses a hoe to make grass land to soil land. it will restore in grid and next time player come back here it will be soil land, instead of grass land as default.

    and yes i am thinking to make open-world game like WOW with no loading screen between scenes, and this is essentially why this is a problem to me.
     
  19. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    yeah i am actually having this idea and woking on it right now, hopefully it works XD
     
  20. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You can store this data no matter using grid , you just grab vector3 position of object on saving and it make same thing like with grid.If you want serialize it you must convert Vector 3 to float[] but thats all.
     
  21. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    yes ur right, however grid system does make the editing easier tho, i made a editor window to easily draw a map saving tons time
     
  22. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Just start with small grid and your big grid can be constructed from your small grid later you just use them as parametr when constructing big grid. I am using something similar in my game and it is working fine.
    Code (CSharp):
    1. namespace Assets.Extra
    2. {
    3.     public class SmallGrid
    4.     {
    5.  
    6.     }
    7.     public class MediumGrid
    8.     {
    9.         List<SmallGrid> smallGridList;
    10.         public MediumGrid(List<SmallGrid> smallGridList)
    11.         {
    12.             this.smallGrid = smallGridList;
    13.         }
    14.     }
    15.     public class BigGrid
    16.  
    17.     {
    18.  
    19.         List<MediumGrid> mediumGrid;
    20.         public BigGrid(List<MediumGrid> mediumGridList)
    21.         {
    22.             this.mediumGrid = mediumGridList;
    23.         }
    24.     }
    25. }
     
    Last edited: May 2, 2021
  23. qz154772610

    qz154772610

    Joined:
    Sep 6, 2020
    Posts:
    23
    thx for the information! that example helped alot
     
    MartinMa_ likes this.
  24. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    No problem this is good because when you "load" your big grid it will also load objects inside it you just foreach them like with standart list
     
  25. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Yep, that's a chunk system :p