How would i structure a grid module system

i want to make a turn based grid system, similar to the SD gundam games, but im not sure how what variables i should store for the grid system, nor how to plan it out, as im torn between using a table for the units, or using a table for each coordinate, and checking through the table every time for sanity checks when doing anything with the units.
i just dont really know how to structure this

1 Like

I don’t really see the advantage of using a table for each coordinate.

Assuming you don’t want two units to occupy the same spot? Instead of checking each unit, you can have local occupiedCoords = {} (containing Vector2) for each unit, they use table.insert to insert their own position into the said table, and to check if the target spot they are moving to is occupied, they would use table.find(occupiedCoords,target), not nil means occupied.

If you are worried about “ghost spots” (game thinking a spot is occupied despite it not being occupied) you can periodically check wether #occupiedCoords = #units assuming both are tables. If it is different, clear occupiedCoords then iterate through each unit and insert their position.

so move function should contain code that finds the index of the unit’s current spot using table.find, remove it using table.remove, then insert the unit’s new spot into the table.