Search code examples
delegatesviewcontrollerskscene

Can't set delegate between ViewController and SKScene


I want to set delegate between my GameViewController and SKScene file but facing error. My goal is to call method from SKScene file.

Error is : Value of type 'GameScene' has no member 'gameViewControllerDelegate'

What am i doing wrong ?

Thanks in advance

MainSC.swift

import UIKit
import SpriteKit
import Firebase
import StoreKit
import GoogleMobileAds

class MainVC: UIViewController, GameViewControllerDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

     if let view = self.view as! SKView? {

            if let scene = SKScene(fileNamed: "GameScene") {
                let gameScene = scene as! GameScene

                //THIS GUY THROWING ERROR
                 gameScene.gameViewControllerDelegate = self 

                gameScene.scaleMode = .aspectFill
                gameScene.size.width = 414
                gameScene.size.height = 736
                view.presentScene(gameScene)

            }

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
            view.showsPhysics = true

        }
    }
func callMethod(inputProperty:String) {
        print("inputProperty is: ",inputProperty)
    }
}

MainSC.swift

import UIKit
import SpriteKit
import GameplayKit
import Firebase
import GoogleMobileAds

protocol GameViewControllerDelegate : class {
    func callMethod(inputProperty:String)
}

class MainSC: SKScene {

    weak var gameViewControllerDelegate : GameViewControllerDelegate?

override func didMove(to view: SKView) {

       ...

       gameViewControllerDelegate?.callMethod(inputProperty: "call game view controller method")

    }

}

Solution

  • You didn't set the gameViewControllerDelegate delegate as self in MainVC so how is it possible to call the delegate of that SKscene . So set as self in MainVC