-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..

4
  • you should try this stackoverflow.com/questions/24044108/… Commented Apr 12, 2018 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 Commented Apr 12, 2018 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. Commented Apr 12, 2018 at 21:58
  • @Quac Nguyen can you please give more detail or a link that I can look. Commented Apr 12, 2018 at 21:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You sooo much. It solved my problem. you saved me from great headache.
No problem. If it answered your question do me a favor and mark this as answer (not just upvote). Cheers!
I did. but because of my reputation is less than 15 upvote is not shown. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.