How to comapre variables in nested array

Godot Version

4.1

Question

i am quite new currently making a match 3 game that might get very complicated later on but im having issue with the matching logic. my current system is running on a nested array, where each sub array has the tiles info like the color and grid coordinates, and i was able to use .filter to narrow down the matchable types but am stuck trying to cross reference the sub arrays with each other. an abbreviated example of my current process is below.

array = []
(whenever tile is spawned):
    tile.x = origin.x
    tile.y = origin.y
    #real position
    tile.gridX = origin.gridX
    tile.gridY = origin.gridY
    #grid position ex (1,3)
    tile.ID = origin.ID
    tile.type rand(1,6)
    tile.sub_array = [gridX, gridY, type, ID]
    array.append(sub_array)
print((array).filter(matchable).filter(nearby))
func matchable(sub_array):
    return ((sub_array [2]) < 3)
func nearby(sub_array):
    #this is supposed to be comparing the filtered arrays type and position related to each other and is what im trying to figure out how to implement 

If we forget comparing arrays, what are you actually trying to do? Tile already has gridx, gridy, type and id, why are you putting them also to an array?

well my original idea was to have each currently spawned gem have its own set of info that get filtered down to check which ones are next to each other but as i’m new there’s probably a much easier and simpler way to do all of this so what do you reccomend

I haven’t made a “connect three” game but I wouldn’t store the playing field state to the gems. It would be a nightmare to keep the state updated. Make a simple grid where to store the playing field state. Keep the state handling (= game logic) and the gem’s visual appearance and possible animation separate things. First, make the game logic. When that works flawlessly, continue with the visual appearance.

I would something like this. Whenever the playing field changes, just check the whole field if there are three adjacent gems with same color (or however your game works). Do not try to do any smart partial calculations until the full field calculation works. If your playing field size is reasonable (i.e. not hundreds of gems tall or wide), you probably don’t need any partial updates.

(I haven’t tested any of this code, it may not work as it is.)

enum Gem {NONE, RED, GREEN, BLUE} 
var debug_gem: [" ", "R", "G", "B"]

var width: int
var height: int

var gems: Array[Gem]

func init_field(w: int, h: int) -> void:
    width = w
    height = h
    field.resize(width * height)
    # fill field with gems

func set_gem(pos: Vector2i, gem: Gem) -> void:
    # check that pos is valid 
    gems[pos_to_idx(pos)] = gem

func pos_to_idx(pos: Vector2i) -> int:
    return x + y * width

func recalculate_field() -> void:
    for y in height:
        for x in width:
            # check neighbours to every direction, or whatever your game needs

func debug_print() -> void:
    for y in height:
        var row: String
        for x in width:
            row += debug_gem[pos_to_idx(Vector2i(x, y))]
        print(row)