swiftsprite-kitnskeyedunarchiver

Load SpriteKit levels from filename


For a game I'm creating, I want to be able to create a lot of custom levels that can be loaded easily. Each level should have a .sks interface file and its own SKScene subclass .swift file. XCode

Right now, this is working:

extension SKScene {
    class func unarchiveFromFile(file : NSString, theClass : AnyClass!) -> SKScene? {
        if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
            var sceneData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil)
            var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

            archiver.setClass(theClass, forClassName: "SKScene")
            let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKScene
            archiver.finishDecoding()
            return scene
        } else {
            return nil
        }
    }
}

Where I create the scene like this:

if let scene = SKScene.unarchiveFromFile("Level1", theClass: Level1.classForKeyedUnarchiver())

But this doesn't work for large collections because I can't iterate over each level in, say, a folder of them. I need something like this

if let scene = SKScene.unarchiveFromFile("Level1", className:"Level1")

I tried using this answer to get the class from the string like this, but the scene loaded as if it were just an SKScene, not a Level1 object.

let classFromString: AnyClass! = NSObject.swiftClassFromString("Level1")
    if let scene = SKScene.unarchiveFromFile("Level1", theClass: classFromString)

For now, I'll work with this, but I don't think this is the best approach:

let levels = [
    ("Level1", Level1.classForKeyedUnarchiver()),
    ("Level2", Level2.classForKeyedUnarchiver())]

let level1 = levels[0]
let (fileName : String, theClass: AnyClass!) = classes[0]
if let scene = SKScene.unarchiveFromFile(fileName, theClass: theClass)

I just want to be able to make a large collection of easily loadable SKScene subclasses each with their own interface file.


Solution

  • This can be done using NSClassFromString:

    For Swift 2:

    extension SKScene {
        static func sceneWithClassNamed(className: String, fileNamed fileName: String) -> SKScene? {
            guard let SceneClass = NSClassFromString("Your_App_Name.\(className)") as? SKScene.Type,
                  let scene = SceneClass.init(fileNamed: fileName) else { 
                return nil 
            }
    
            return scene
        }
    }
    

    Or Swift 1:

    extension SKScene {
        static func sceneWithClassNamed(className: String, fileNamed fileName: String) -> SKScene? {
            if let SceneClass = NSClassFromString("Your_App_Name.\(className)") as? SKScene.Type,
               let scene = SceneClass(fileNamed: fileName) {
                return scene
            }
    
            return nil
        }
    }
    

    Usage:

    if let scene = SKScene.sceneWithClassNamed("MyScene", fileNamed: "MyScene") {
        view.presentScene(scene)
    }
    

    Its important to note that for this to work correctly your SKScene subclass must implement init(coder aDecoder: NSCoder), for example:

    required init?(coder aDecoder: NSCoder) {
        // ...
        super.init(coder: aDecoder)
    }