I have build a swift game with a "GameViewController.swift"
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
}
and i have a scene called "GameScene"(it is the default game-file when you open swift and create a new swift game)
import UIKit
import SpriteKit
class GameScene: SKScene{
var building = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
}
a lot of things going on there. (buildings can be placed and that trigger some actions and so on)
But all the progress is lost when you leave the app.
How to save the whole progress of this "GameScene" and reload it at the start?
You can archive your game state in the applicationDidEnterBackground(_:) event. Subscribe through
NotificationCenter
to get notified.Scene loading happens in
viewDidLoad
, by first attempting to locate a previously archived scene or creating a new scene if no archived scene exists.You'll also need to make game elements
NSCoding
compliant, as mentioned in other answers.SKScene conforms to NSCoding, so you can use this to archive your scene. Here is a tutorial that may help. https://www.hackingwithswift.com/read/12/3/fixing-project-10-nscoding
The basic idea is you take the instance of your scene, and you archive it with:
NSKeyedArchiver.archivedData(withRootObject: scene)
Then take this and save it to disk or somewhere else
You can then unarchive it later with
NSKeyedUnarchiver.unarchiveObject(with: scene) as! SKScene
Now when you make a custom scene class, you need to be sure to include any variables you create that you need to save. This is where that pesky
required init(coder aDecoder:NSCoder)
initializer comes in that people tend to not use.You basically use this initializer to decode your custom variables, like this.
To save it to the archive, you add an encode method