I am trying to subclass GKGraphNode2D to also include different penalties for different terrains (in the costToNode method). When I create a new GKGraph with an array of my new subclass and call findPathFromNode on the GKGraph, it ignores my costToNode method entirely.
The code looks like:
public class PenaltyGraphNode: GKGraphNode2D {
public let penalty: Float
init(position: CGPoint, penalty: Float) {
self.penalty = penalty
let point = vector_float2(Float(position.x), Float(position.y))
super.init(point: point)
}
override public func costToNode(node: GKGraphNode) -> Float {
guard let penaltyNode = node as? PenaltyGraphNode else { return super.costToNode(node) }
return super.costToNode(node) * (1 + penaltyNode.penalty)
}
}
And where I am trying to use the GKGraph (node creation removed for brevity sake):
let penaltyNodes: [PenaltyGraphNode] = [penaltyNode1, penaltyNode2, ...]
let graph = GKGraph(nodes: penaltyNodes)
let path = graph.findPathFromNode(startNode, toNode: endNode) as? [PenaltyGraphNode]
The costToNode in my subclass is never getting called, I made sure that all of the nodes that I create in the graph and add to it later are all PenaltyGraphNodes, but it still won't call costToNode. When I print the graph's nodes property, it shows them as GKGraphNode2D objects only, not my subclass.
Am I missing something obvious here?
class
message. With a naïve print-out of thedescription
property (used when printed withNSLog
), it does look like the objects are vanillaGKGraphNode2D
objects, but when I interrogate them further, they are indeed my subclass, as expected. However,costToNode:
etc. are still not called. – Jigaboo