Simple agents with behaviors crashing

My code is here: https://gist.github.com/erica/e9dd8ca96888b74344be


I'm trying to boil down the agent paradigm to the simplest possible scenario and I cannot figure out why when I add a behavior my system crashes. I'm trying to mimic all the settings and updates I see in the sample code and cannot find any meaningful point of correlation where I fail to do something the sample does.


Thanks in advance for any insight.

Replies

This is what I came up with, it does work but wander (as has been mentioned elsewhere I think) seems to only result in movement on the x-axis with no sign of change in rotation or movement on the y-axis.


import SpriteKit
import GameplayKit
class CritterAgent: GKAgent2D, GKAgentDelegate {
    var scene: SKScene!
    let sprite = SKSpriteNode(imageNamed: "AgentSprite_002")

    convenience init(gameScene: SKScene, position: CGPoint) {
        self.init()
        self.scene = gameScene
        self.sprite.position = position
        self.scene.addChild(sprite)
  
        self.delegate = self
        self.radius = 30.0
        self.maxSpeed = 100.0
        self.maxAcceleration = 50.0
  
        let wanderGoal = GKGoal(toWander: 1.0)
        let behavior = GKBehavior(goal: wanderGoal, weight: 1.0)
        self.behavior = behavior
    }

    // DELEGATE

    func agentWillUpdate(agent: GKAgent) {
        // Place critter to match sprite
        let xPos = Float(self.sprite.position.x)
        let yPos = Float(self.sprite.position.y)
        self.position = float2(xPos, yPos)
        self.rotation = Float(self.sprite.zRotation)
    }

    func agentDidUpdate(agent: GKAgent) {
        // Update sprite to match updated critter
        self.sprite.position.x = CGFloat(self.position.x)
        self.sprite.position.y = CGFloat(self.position.y)
        self.sprite.zRotation = CGFloat(self.rotation)
        print("SPRITE X: \(self.sprite.position.x) Y: \(self.sprite.position.y) VEL: \(self.velocity)")
        print()
    }

    // UPDATED FROM: SKScene > update() > CritterAgent.updateWithDeltaTime()
}


EDIT: Tried replacing the wanderGoal with a seekGoal (just a simple GKAgent with its position set) positioned at the top of the screen and that seems to work well. My critter heads straight for the goal and loops back as it passes like you would expect. I still can't get wander to do anything (other than move right) so seek might be a good choice for testing as it does seem to work.