A collection of nodes that describes the navigability of a game world and provides *pathfinding* methods to search for routes through that space.

SDKs

- iOS 9.0+
- macOS 10.11+
- tvOS 9.0+

Framework

- Gameplay
Kit

## Declaration

## Overview

Individual nodes in a graph represent discrete locations that a character or other object in your game can occupy, and the connections between adjacent nodes represent the ability of a game entity to travel from one location to another. Use the `GKGraph`

class to create a general graph, or the `GKGrid`

, `GKObstacle`

, or `GKMesh`

subclass to generate specialized graphs that contain more information about the geometry of your game world.

Each set of graph and node classes can generate graphs for different kinds of spaces:

The base classes

`GKGraph`

and`GKGraph`

contain functionality general to all graphs and nodes. You can also use these classes on their own to construct graphs that contain no geometry information. This option is useful for games where the connections between spaces are more important than their physical locations, such as board games.Node Use the

`GKGrid`

andGraph `GKGrid`

classes to describe game worlds that constrain movement to an integer grid, such as tactical role-playing games.Graph Node Use the

`GKObstacle`

orGraph `GKMesh`

class to describe 2D game worlds that allow continuous movement in open spaces that are interrupted by impassable obstacles (Graph `GKPolygon`

objects). Obstacle graphs automatically generate nodes containing 2D point information (Obstacle `GKGraph`

objects), and you can also add your own such nodes representing locations of interest.Node2D

The graphs modeled by this class are always *directed*—that is, a connection between two nodes describes one direction of travel between them. To enable travel between two nodes in either direction, you must create a connection in each direction. You can choose to connect both directions at once with the `connect`

method (for graphs) or the addConnection:bidirectional: method (for nodes).

Using a graph for pathfinding typically involves three major steps:

Create a graph once (for example, when initializing a game level class) with static information about your game world.

When you need to find a route between points, connect temporary nodes to the graph at those points. Use the

`connect`

method to connect nodes using their own geometry information, or theTo Lowest Cost Node(node: bidirectional:) `connect`

orUsing Obstacles(node:) `connect`

method to use the additional constraints of obstacle and grid graphs.To Adjacent Nodes(node:) Call the

`find`

method to find a route between locations in the graph. This method returns an array of graph nodes, starting with the requested start point of the path, and proceeding to adjacent nodes in order until it reaches the requested end point. Use the geometry information contained in each node to make use of the route—for example, in a SpriteKit game you might create a sequence of move actions to move a character from point to point along the path.Path(from: to:) The temporary nodes you created for finding a path typically have little usefulness after a path has been found. Remove those nodes before reusing the graph for future searches.

To learn more about graphs and pathfinding, see Pathfinding in GameplayKit Programming Guide.