Luiz Luiz - 4 months ago 50
iOS Question

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.

Answer

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