SOLVED Create an array from a grid


(TaylorAnderson) #1

Hellooo,

So I’m starting to try and implement pathfinding for enemies in my game (I know, may God have mercy on my soul) and I’m taking it one step at a time. The first thing I want to do is create an array from a Grid object, so I can do pathfinding stuff with it later. Is there an easy way to do this?

Thanks, Taylor Anderson


(TaylorAnderson) #2

Nevermind, found out :stuck_out_tongue: Turns out the double for loop I was using was correct, but I just wasn’t displaying it properly. Code:

for (r = 0; r < grid.rows; r++)
{
	pathGrid[r] = new Array();
	for (c = 0; c < grid.columns; c++)
	{
		pathGrid[r][c] = grid.getTile(c, r) ? 1 : 0
	}
}

where pathGrid is just an Array.


(Abel Toy) #3

Just so you know, 1-dimensional arrays can be used to represent a grid as well.

You can get/set members this way: array[(row * cols) + col]