<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "GameplayKit",
  "identifier" : "/documentation/GameplayKit/GKGraphNode/cost(to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKGraphNode(im)costToNode:"
  },
  "title" : "cost(to:)"
}
-->

# cost(to:)

Returns the cost to travel from this node to the specified, directly connected, node.

```
func cost(to node: GKGraphNode) -> Float
```

## Parameters

`node`

A node from this node’s [`connectedNodes`](/documentation/GameplayKit/GKGraphNode/connectedNodes) list.

## Return Value

The real cost of travel along the specified connection; higher values indicate higher cost.

## Discussion

The [`GKGraph`](/documentation/GameplayKit/GKGraph) class uses this method in pathfinding between nodes—the [`findPath(from:to:)`](/documentation/GameplayKit/GKGraph/findPath(from:to:)) method finds the lowest-cost path between a specified pair of nodes. For the [`GKGraphNode`](/documentation/GameplayKit/GKGraphNode) base class, the cost of travel along any connection is 1, so the [`findPath(from:to:)`](/documentation/GameplayKit/GKGraph/findPath(from:to:)) method simply counts connections, finding the path that involves traveling through the fewest number of nodes. Because the subclasses [`GKGraphNode2D`](/documentation/GameplayKit/GKGraphNode2D) and [`GKGridGraphNode`](/documentation/GameplayKit/GKGridGraphNode) add geometry information to each node, those classes’ implementation of this method calculates the distance between nodes in 2D space, and the [`findPath(from:to:)`](/documentation/GameplayKit/GKGraph/findPath(from:to:)) method finds the path with the minimum total distance traveled.

Subclasses can implement this method to add other information to the cost. For example, a game might use higher costs to represent travel through regions of a map that are difficult for a character to traverse.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)