swiftsprite-kitscenekitskscenescnscene

Is it possible to have "update(_ currentTime: TimeInterval)" in SCNScene or SCNNode the same way as SKScene?


I'm working on an iOS game that will require the use of both SceneKit & SpriteKit & was wondering about the Render/update loop function.

I will be using a SpawnController class to spawn different types of objects & they will be based on the time interval.

Right now I have a GameViewController that conforms to the SCNSceneRendererDelegate like here I've modified it so that inside the delegate update function I call my SpawnController's custom update function like so:

extension GameViewController: SCNSceneRendererDelegate {
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        mySpawnController.nextSpawn()
    }
}

I feel there must be a better way to do it such as add a delegate to an SCNScene or SCNNode.

My question:

Is it possible to have "update(_ currentTime: TimeInterval)" for SCNScene or SCNNode the same way as SKScene has? Or should I keep calling custom added update functions from the one SCNSceneRendererDelegate update function in my GameViewController?

Thanks


Solution

  • The SCNView, conforming to SCNSceneRendererDelegate calls renderer(_: updateAtTime:) on assigned delegate, and it is right place to insert game loop logic.

    Regarding your question: neither SCNScene nor SCNNode has its own update methods. If you are using both SceneKit and SpriteKit, it is sufficient (but not necessary) to have one place for game loop logic, as mentioned above, so your example looks correct.

    To be precise, there is difference, although render loop methods looks alike:

    While in SpriteKit view has SKViewDelegate's view(_:shouldRenderAtTime:) which must return true if render loop should be called. SKScene's delegate exists only for the sake of avoiding subclassing of SKScene.

    If you are looking to more precise control of update logic, I would recommend using GameplayKit which is independent from SceneKit/SpriteKit and allows easy per-entity/per-component updates as well as entity/component reuse and other goodies.