Author Topic: Get Neighbours for Group?  (Read 798 times)

manofspirit

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Get Neighbours for Group?
« on: August 16, 2019, 11:50:53 AM »
Hi there,

As you can see in attached image, i have a group 1 cells and what i need is get its Neighbours with a given distance (currently 1) as yellow cells.
An optimize function that does this'd be really great.

Thanks


Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5320
    • View Profile
Re: Get Neighbours for Group?
« Reply #1 on: August 19, 2019, 05:08:35 AM »
There're many ways to accomplish this. Since the grid is square, you could just get the neighbours of those 1-cells by getting the index using the row/column directly using CellGetIndex(row, column,true) method.
Then, you could use the cell.iteration property to annotate which cells have been already added to the result list so you ensure there're no duplicates. Ex:

Code
if (neighbour.iteration != Time.frameCount) {
  ... add this cell to result and annotate it's used:
  neighbour.iteration = Time.frameCount;
}
iteration is indeed an utility field that can be used to optimize this kind of algorithms by storing temporary data. In this case we use the Time.frameCount to ensure we don't use that neighbour more than once.



manofspirit

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Get Neighbours for Group?
« Reply #2 on: August 20, 2019, 10:16:04 AM »
Thanks , hopefully it works fast on mobile every update.