I am using SKTilemapNode. How I can use the Pathfinding with GameplayKit properly?
Some Info: Walls are all tiles and no tiles are 'ways'
I am using SKTilemapNode. How I can use the Pathfinding with GameplayKit properly?
Some Info: Walls are all tiles and no tiles are 'ways'
This is how I implemented a GKGridGraph using the contents of one of my tile maps:
let graph = GKGridGraph(fromGridStartingAt: vector_int2(0,0), width: Int32(tileMap.numberOfColumns), height: Int32(tileMap.numberOfColumns), diagonalsAllowed: false)
var obstacles = [GKGridGraphNode]()
for column in 0..<tileMap.numberOfColumns
{
for row in 0..<tileMap.numberOfRows
{
let position = tileMap.centerOfTile(atColumn: column, row: row)
guard let definition = tileMap.tileDefinition(atColumn: column, row: row) else { continue }
guard let userData = definition.userData else { continue }
guard let isObstacle = userData["isObstacle"] else { continue }
if isObstacle
{
let wallNode = graph.node(atGridPosition: vector_int2(Int32(column),Int32(row)))!
obstacles.append(wallNode)
}
}
}
graph.remove(obstacles)
I had to set the isObstacle property in the userData field of my obstacle tile:
This way you get the resulting grid graph without the osbtacles.
The basic idea is to create a SKGridGraph
of connected nodes, then remove the nodes that can't be walked on. The GameplayKit Guide shows an example (Listing 6-1 Generating a Grid Graph). So in your case using SKTilemapNode
the graphs width and height is determined by the number of tile wide and high on the map e.g. if your map is 32 tiles wide and 18 tiles high, then initialise the graph with width of 32 and height of 18.