-1

Unfortunately I m newbie and cant solve this problem for days. the problem is I cant success to gather score from game engine class to view controller so that I can show it with a label.

here is I want to extract data from this func to global variables.

private func nextMove() {
    current = .random(config)
    if (well.hasCollision(current)) {
        stopTimer()
        scene.showGameOver(scores)
      //extract score to another variable can be reached by another class

    } else {
        scene.show(current)
    }
}

I tried many methods, creating a global class with init method, creating struct, creating protocol..etc that already written here, but never successed. any idea will be appriciated. thanks..

Bilal Şimşek
  • 5,453
  • 2
  • 19
  • 33
  • you should try this https://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Kamran Apr 12 '18 at 21:27
  • You should create a closure variable if you want to send to only 1 viewController or push notification if you want to send to many viewcontroller at once – Quoc Nguyen Apr 12 '18 at 21:49
  • @Kamran at that link while calling function parameter is used. and I dont mean segues. in my case I just want to extract values stored in scores variable to another class. I only have one viewcontroller already. – Bilal Şimşek Apr 12 '18 at 21:58
  • @Quac Nguyen can you please give more detail or a link that I can look. – Bilal Şimşek Apr 12 '18 at 21:58

1 Answers1

2

A Singleton could be what you're looking for.

Here's an example:

class ScoresSingleton {
    static let sharedSession = ScoresSingleton()
    var scores : Int = 0
} 

You may then use globally by:

// Setting    
ScoresSingleton.sharedSession.scores = 10


// Getting
var scores = ScoresSingleton.sharedSession.scores

Hope that helps.

R Bradshaw
  • 173
  • 11