0

I have multiple scenes that are stored in Global/BaseScene

Each SceneType: is stored as enum: set to an Integer similar to this present scene

First objective was to get the score to populate in each scene, there is seven of them. DONE thanks to stack overflow, and experimentation

I had to create default keys for each individual scene, to calculate the highScore, so I have seven unique "highScore" "keys" for each scene.

In the GameScene:

var highScoreA1: Int = 0  
    var score: Int = 0 {
         didSet { 
             a1Score.text = "Score: \(score)"
         }
      }

//above called before override func didMoveToView(view: SKView) { 

//Called in GameOver Method 
let scoreDefault = NSUserDefaults.standardUserDefaults()
scoreDefault.setInteger(score, forKey: "score")

if (score > highScoreA1){

let highScoreA1Default = NSUserDefaults.standardUserDefaults() 

highScoreA1Default.setInteger(score, forKey: "highScoreA1")
//highscoreA1Default.synchronize()         

There are six more keys similar to this.. My objective is to populate a "totalScoreKey" in two different scenes a Hud Scene and another scene (possibly game over scene)

I was thinking a function to add these keys together to populate the total score. Taking into consideration all these scenes are subclasses (of Global BaseScene, and each scene has sub classes (for the nodes operation, probably not relevant yet thought it might be useful)

I have tried: Moving all score data into a Class and using NSCoding/NSObject the required init, and optional binding, became a serious pain, and honestly I am trying to keep things simple for version one.

import Foundation

    class GameState {
        var score:Int = 0
        var highScore:Int()
        var totalScore:Int()

        class var sharedInstance: GameState {
            struct Singleton {
                static let instance = GameState()
            }

            return Singleton.instance
        }
    }
    init() {
        // Init
        score.type = 0
        highScore = 0
        totalScore = 0

        // Load game state
        let defaults = NSUserDefaults.standardUserDefaults()

        highScore = defaults.integerForKey("highScore")

    }

    func saveState() {
        // Update highScore if the current score is greater
        highScore = max(score, highScore)

        // Store in user defaults
        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setInteger(highScore, forKey: "highScore")
        defaults.setInteger(totalScore, forKey: "totalScore")
        NSUserDefaults.standardUserDefaults().synchronize()
    }

Various functions that have not worked they all default to zero, that is until I figure out how to retrieve data properly.

let total score = "totalScoreKey"

similar to this post exactly like this post actually except I had to do different configurations because of my on personal set up..

total Score example I tried to implement

outside of class and referring to that in the scene I needed to populate that data. NO Go defaults to zero.

How do I simply add the value of those keys together? For which I can display similar to the other scenes, I already have implemented. later on down the road I may want to assign a key chain value, right now I am just trying to get it show up for posting in GameCenter. (which also has key "GameCenterHighScore")

Setting them all to the same key "highScore" does not work.... just to be clear, I tried multiple times. Thanks in advance.

EDIT if I try to add all the defaults together to get the total, it throws the following error:

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions [Swift compound arithmetic operation ERROR3

Community
  • 1
  • 1
upallnight
  • 89
  • 12
  • It's not clear what your problem is, or what you mean by "add these keys together". Of course if you save a value with key of "highScore" multiple times to NSUserDefaults, they will all overwrite each other. If you have multiple high scores, why not store them in a Dictionary, and save that to NSUserDefaults? – Michael Mar 01 '16 at 04:23
  • I want total the high score values of each scene,so I can populate the total scores in the scenes that need them. Is my conclusion I have come too. – upallnight Mar 01 '16 at 04:23
  • each highScore has its on individual key (as it relates to the proper scene), crossed that bridge already... and you are right sir @Michael – upallnight Mar 01 '16 at 04:26
  • So what's your problem? And the link you have provided is broken. – Michael Mar 01 '16 at 04:28
  • roger that Ill fix the link... my problem is how to gather those "individual highScore keys" and display a total score. – upallnight Mar 01 '16 at 04:30
  • Why can't you just read them from NSUserDefaults and add them together? – Michael Mar 01 '16 at 04:30
  • thats my question my man, exactly what I am trying to do... very simple idea I would assume, yet for some reason, its not clicking... – upallnight Mar 01 '16 at 04:32
  • You haven't shown any of that code or what's going wrong with it. I assume somewhere you have `defaults.integerForKey("highScoreA1")+defaults.integerForKey("highScoreA2")...` – Michael Mar 01 '16 at 05:16
  • Its more on an approach question, and outline of things I have tried, I mean I could post the 30 or so links to stack overflow post I have referenced, at the moment I am trying simple addition as you suggested, and I am going to post those results, as I mentioned, I was able to populate the highScore that are in the individual scenes. Just not the total of the collective scores, but I am trying just what you suggested right now as I type this actually. – upallnight Mar 01 '16 at 05:28
  • 1
    "Approach" questions get closed as being too broad or opinion based. It's better to go with an approach and put up a question if you're having specific problems with it. – Michael Mar 01 '16 at 05:36
  • I listed pretty detailed what I have tried Ill be more specific and post all the links I referenced. And the code, I just did not want it to be mile long .. .. nor disclose the intimate details of my project, yet if I have to do that, to get a answer I will.. Currently I tried your approach and I have it down to one error – upallnight Mar 01 '16 at 06:31
  • @LegacyEternal That is because you are probably trying to do something like `defaults.integerForKey("highScoreA1")+defaults.doubleForKey("highScoreA2")` Try to use only one type (I guess that would be Int). – Whirlwind Mar 01 '16 at 10:19
  • @whirlwind they are all converted into integers, apparently, there are just some calculations that will not compile if too long. I figured since they were all converted to integers no problem adding. My new approach to is to calculate it in the game scenes itself, and see if I can extract that single key, and see if it populates. correctly, Since it will be universal. += does not seem to work in the game scenes either, nor does + operator. So I had to use >= – upallnight Mar 01 '16 at 16:24
  • @LegacyEternal What Michael pointed should work. I can't see how that can trigger the error you are talking about. But anyways, if you've found a workaround, it doesn't really matter. Otherwise, you could update your question (or ask another one) to show which expression compiler considers as too complex... – Whirlwind Mar 01 '16 at 16:28
  • to be clear I made a new if statement in my game scene.. with a new key that defaults to total score with a "total score" set to an integer. – upallnight Mar 01 '16 at 16:55
  • @ Whirlwind, I follow you btw, because there is not a lot of SpriteKit gurus on here that provide the amount of adequate coding examples, especially as noob it is much appreciated. I provided a link to a similar error. They provided a work around which I will attempt now, if it does not work, I will post a new question with the new error. – upallnight Mar 01 '16 at 17:06
  • http://stackoverflow.com/questions/35729564/defaults-too-complex-to-compile-with-basic-operator – upallnight Mar 01 '16 at 17:35

0 Answers0