0

I have SCNScene subclass with a camera setup, that I want to use in all the subclasses.

let scene01 = TheSubclassScene()
let scene02 = TheSubclassScene(named:"art.scnassets/testScene.scn")!
self.sceneArray.addObject(scene01)
self.sceneArray.addObject(scene02)

I want to change the scenes at runtime. This works when I create the scene in code but not with a scene from the SceneKit Editor. So scene01 is working but scene02 isn't. In the debugger I can see the two scenes in the array. One is of type SCNSceneSubclass but the other is of type SCNScene.

Is there any way to get this working?

Update: This is my scene subclass

class TheSubclassScene: SCNScene
{
    let cameraNode = CameraNode()

    override init()
    {
        super.init()
        self.rootNode.addChildNode(self.cameraNode)
    }

    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }
}
Nico S.
  • 3,056
  • 1
  • 30
  • 64

3 Answers3

2

I have a workaround for that problem. Not nice but it works. I instantiate a SCNScene(named: "art.scnassets/testScene.scn") then I instantiate a TheSubclassScene() and I clone the rootNode of the scene and add it as a child node to the subclass scene.

let testScene = SCNScene(named:"art.scnassets/testScene.scn")!
let subclassScene = TheSubclassScene()
subclassScene.rootNode.addChildNode(testScene.rootNode.clone())
Nico S.
  • 3,056
  • 1
  • 30
  • 64
1

.scn files are just SCNScene instances archived and written to disk using NSKeyedArchiver. What + sceneNamed: does is that it simply unarchives the scene with a NSKeyedUnarchiver.

What you could do then, is to try to use - setClass:forClassName: to instantiate a subclass instead of a SCNScene.

That being said, SCNScene is not really meant to be subclassed. Instead you could implement your logic in the view controller or a game controller (possibly a direct subclass of NSObject). This controller will also likely conform to SCNSceneRendererDelegate to implement your game logic.

mnuages
  • 13,049
  • 2
  • 23
  • 40
0

Note: don't give your custom subclass a name starting with "SCN"; that prefix is for Apple.

The only difference I can see is calling SCNSceneSubclass(named:) vs SCNSceneSubclass(). Without seeing your code, my hunch is that your implementation of SCNSceneSubclass(named:) does not call through to your subclass's initialization, and thus misses some key steps.

Can we see a bit of your subclass's source code?

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • SCNSceneSubclass is just in example. In my code it has a name without SCN prefix. The subclass is just adding a camera node at initialisation. – Nico S. Nov 23 '15 at 23:49
  • I provided the subclass code. Nothing wrong there in my opinion. Is there any way to override the convenience init(named name: String) ? Maybe the problem is there. – Nico S. Nov 24 '15 at 00:22
  • You don't have to call it that. But I don't think that's the problem. What do we know about testScene.scn? Does it have any nodes that you can see in the debugger? Can you load it into a straight SCNScene (not your subclass)? Can you see it using Quicklook or Preview? What happens if you load a different file into your subclass? – Hal Mueller Nov 24 '15 at 03:56
  • testScene is fine as well, just a box inside the scene and I can load it in an SCNScene. Quicklook works as well. Different file, same problem. – Nico S. Nov 24 '15 at 11:48