I have an in game currency called diamonds, which are gained when a dragon runs into these diamonds, and they amount is added into a variable called diamondCount. I am using a label called diamondLabel to show the amount to the user, but I have tried alot of methods to save the diamonds when you close the game and none have worked for me. Can anyone please tell me a simple way to store diamonds(my in game currency) in gamescene.swift?
-
Check [this out](http://stackoverflow.com/questions/25269686/swift-saving-highscore-using-nsuserdefaults) – Matt Le Fleur Sep 18 '15 at 15:55
-
Thanks for the link, however im actually looking for a way to save currency, which is different from saving the highest score, because the previously saved currency must be added to the current currency that the user has earned in game. – Amir A. Sep 18 '15 at 19:15
1 Answers
I've found the easiest way to save in game currency, granted you aren't trying to link it up to a backend service such as Parse, is by storing integer values into NSUserDefaults. Here is an example of how exactly you would be able to save the "diamonds."
Firstly, make a global variable in GameScene.swift called diamonds.
import Foundation
import UIKit
import SpriteKit
var diamonds = Int()
class GameScene: SKScene
{
override func viewMoveToView(view: SKView)
{
//Your game code here
}
}
This will make that integer variable accessible in all of your ViewControllers and SKScenes
Then you need to store your diamondCount at the end of a game into NSUserDefaults.
NSUserDefaults.standardUserDefaults().setInteger(diamondCount, forKey: "totalDiamonds")
Let me explain how the code line above works, when your gave is ended and you have a final integer value for diamondCount, then call that line of code to save the integer value of diamondCount into NSUserDefaults for the data to later be accessed through the reference key: "totalDiamonds"
Now to answer your questions about saving the diamonds after the player exits the game and comes back, I have written some code that you would put into your AppDelegate.swift.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var oldDiamonds : Int = NSUserDefaults.standardUserDefaults().integerForKey("oldDiamonds");
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
diamonds += oldDiamonds
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication)
{
NSUserDefaults.standardUserDefaults().setInteger(diamonds, forKey: "oldDiamonds")
NSUserDefaults.standardUserDefaults().synchronize()
}
Hopefully you understand the end from what I've explained about NSUserDefaults. If you need any more information on it, please check Apple's Documentation on NSUserDefaults.
I hope this helps you! Comment any things I need to clarify.

- 66
- 8
-
thanks for answering with clear instructions, i have used all of the code above. however, my diamonds are showing as being 0 every new game whenever i close the game. let me explain a bit more. whenever my game character hits a diamond, it runs diamondCount++. then in a label called diamondLabel, the text is "\(diamondCount) Diamonds". – Amir A. Sep 19 '15 at 12:41
-
I tried to fix my code to fit your description and it works perfectly now. Thanks! – Amir A. Sep 19 '15 at 13:51
-
Glad I could help! Next time add the tag "sprite-kit"so more people can find it – Neel Sarwal Sep 22 '15 at 15:12