- Home /
Saving player stats for each level, static class or not ?
I'm creating a LevelStats class that I will use to save information about each level, for example, the best time, the number of chests collected, number of stars won etc...
But I am not sure what the best way to build it is, as a static class with static functions, or as a normal class which I create new instances of for each level? The way I am saving values is: ProfileName.LevelName.StatName
If static, then I could save any stat from anywhere with:
LevelStats.SaveLevelBestLap( Application.loadedLevelName, laptime );
if not static, then I would need to create an instance of it in any class that will need to save stats:
public LevelStats levelStats = new LevelStats( Application.loadedLevelName );
levelStats.SaveLevelBestLap( laptime );
I'm looking for a few tips from you guys with experience with this type of stuff. Is using a static class a better fit?
Thanks guys!
Stephane
Answer by Tarlius · Feb 28, 2013 at 08:33 AM
I think you want to google "Singleton" and have a look at the PlayerPrefs class.
A singleton setup basically looks something like this:
public class SaveData : MonoBehaviour {
// This is the singleton bit
SaveData instance = null;
public SaveData Instance {
get {return instance;}
}
void Awake() {
if(instance != null) Debug.LogWarning("There is more than one instance of SaveData in the scene!!");
instance = this;
}
public void DoSomething() {}
}
Then call with: SaveData.Instance.DoSomething();
You could also add a static wrapper method to the class like:
public static void DoSomethingStaticVersion() { Instance.DoSomething(); } // Note the name can't be the same
Or make a shortcut class and put all the statics in there
public static class SD {
public static DoSomething() { SaveData.DoSomething(); } // Different class, so can share name
}
Hi Tarlius, and thank you for your time. I didn't want to use a Singleton because in theory, each level has its own LevelStats, so I was thinking about creating a new instance of the LevelStats() class for each level...but now that I think about it, there will never be 2 instances of LevelStats per level, so I guess a singleton would make sense...
$$anonymous$$y reasoning behind making it a static class is that I need to save different stats from a few different classes, and I don't want to create a new LevelStats() instance within each script that needs to save stats data, so making it a static class makes it easier to save stats data from any script. I do like your suggestion about making a shortcut class though, interesting idea!
And yes I am using PlayerPrefs to save the data I need.
Thanks, Stephane
You could build the key using the name of the level, perhaps?
Level1.isFinished Level2.isFinished Level3.checkpoint
Yes I am building the key using the name of the level, just like you mentioned. I ended up making the class static, that way I can easily save stats through static functions that accept the level name + stat name.
Thanks for your help
Your answer
![](https://koobas.hobune.stream/wayback/20220613093423im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Saving problem unity project 5 Answers
Deliberately Forcing the Game to Crash 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can I transfer the PlayerPrefs.plist file to the iPhone? 1 Answer
Save scene 1 Answer