Search Unity

Help Wanted checking cells on an infinite grid?

Discussion in 'Scripting' started by puddleglum, Oct 21, 2021.

  1. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    213
    i have a project with an infinite grid and cells on the grid can be alive or dead. if i have a living cell. how can i tell if cells that border it are alive or dead?

    (im probobly really dumb and its super simple but i am really dumb)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,041
    However cells are addressed, modify that address to point to their neighbor cells and iterate those.
     
    Joe-Censored and exiguous like this.
  3. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,616
    When your grid is "infinite" how do you index the cells since numbers in computers can only represent a finite value?

    As for your question: A grid is defined by coordinates. So you may need a method to query the cell at a given coodinate. So when you are at 5,2 and want the cell up from this you search for 5,3.

    Edit: There was a quote from Einstein about infinity but I don't remember it ;).
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,718
    Might need more info on your infinite grid, since that is not actually possible.
     
  5. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    213
    when i say infinite grid i litteraly mean im using the coordinate/tile system unity gives you and need to tell if a tile is being ocupied

    ok ill defenitly try that :p i guess my way of doing it(which is really clunky cause i suck at coding) would to just make every cell have colliders on each side and if they are being touched trigger somthing in the script
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,616
    Don't do that. If you use colliders/physics for such stuff your grid will be even smaller than now.
    Here I have explained how I would do it in a similar usecase. When only a small part of the grid is visible to the player (and action happens only there so there is no simulation of the outer parts) I would even avoid using game objects for the whole grid but just have a data representation (non MB class) and when player moves the data around him is loaded and the tile representation is created from that.
    So in general you need a manager class which has access (or stores) all your cell data. This one is responsible for getting you information about a certain cell to check if it is "occupied" (whatever that means).

    The only way to get better is practicing ;). And maybe a bit of reading (forum, books, blogs).
     
unityunity