Luiz
Luiz

Reputation: 1315

Share a variable between swift files

Explanation

My question is pretty straight forward: how do I print a variable from GameScene on GameViewController?


Code

I created this code below so it's easier to get the idea.

GameScene.swift

import SpriteKit

class GameScene: SKScene {

    var variable = Int()

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        variable = 50
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        NSNotificationCenter.defaultCenter().postNotificationName("calledFromGameSceneVC", object: nil)
    }
}

GameViewController.swift

import UIKit
import SpriteKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set view size.
        let scene = GameScene(size: view.bounds.size)

        // Configure the view.
        let skView = view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .ResizeFill

        skView.presentScene(scene)

        //------------------------------//

        //Register observer
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.calledFromGameScene), name: "calledFromGameSceneVC", object: nil)
    }

    func calledFromGameScene(){

        //Print variable
        let scene = GameScene(size: view.bounds.size)
        print("My variable from GameScene is: ", scene.variable)
    }
}


Sorry for the brief explanation.

Thanks in advance,
Luiz.

Upvotes: 1

Views: 342

Answers (1)

Paulw11
Paulw11

Reputation: 114875

The type of inter-object communication you are trying to achieve is probably best addressed through a delegation pattern using a protocol:

GameScene.swift

import SpriteKit

protocol GameSceneDelegate {
    func calledFromGameScene(scene: GameScene)
}

class GameScene: SKScene {

    var variable = Int()

    var gameDelegate: GameSceneDelegate?

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        variable = 50
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {    
        gameDelegate?.calledFromGameScene(self)
    }
}

GameViewController.swift

class GameViewController: UIViewController, GameSceneDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set view size.
        let scene = GameScene(size: view.bounds.size)

        // Configure the view.
        let skView = view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .ResizeFill


        /* Set the delegate */
        scene.gameDelegate = self

        skView.presentScene(scene)
    }

    func calledFromGameScene(scene:GameScene){
        //Print variable
        print("My variable from GameScene is: ", scene.variable)
    }
}

Note that you can't use the property name delegate on your GameScene as SKScene already has a delegate property

Upvotes: 2

Related Questions