We’re making a game where the player will have many units/heroes under his control. Those units will level up and evolve and their stats will change over time. We need to be able to store that in a save file of some sort and retrieve that data when required (that data will also be progression, previous choices made etc). What is the best course to follow for something like this? We’re pretty new to unity and the only other platforms we’ve played with so far are like Game Maker and RPG Maker that do all the saving themselves.
Please be a little more specific since we’re kinda new to this. We read about PlayerPrefs but it seems that people don’t like it and it is kinda slow on mobile OSes + windows doesn’t like it. We also read about using SQL but got no idea on how to go about it (if it is relevant that is). Generally I’m also looking on a general method to solve this.
Thanks in advance!
Learn from me: Do not use player prefs for an RPG! Even a smaller single player one. It gets VERY tedious VERY fast, and there’s no real easy way to clean it up, and certainly no easy way to send the saved game over the web – for debugging or allowing players to continue their game on another platform.
The other day I watched this: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
And that started me on a great new path. I’ve been putting up tutorials and unity packages as I make a new RPG: http://forum.unity3d.com/threads/1st-person-rpg-tutorial-series-infinitypbr-com.379255/
I’ve been saving binaries of the data – right now I have the “game data” in one file, and one file for each scene (dungeon etc).
I haven’t yet decided on how to save enemies and their states, or pickups, inventory etc - -but the suggestions above are pretty good.
If each Enemy Model Type has a unique ID, and you have a “enemy control” object that doesn’t move (even if the model does), then you really have unique names considering they’re in unique and non-changing positions, if you use their positions as a key-name. Like “Enemy0.2|2.2|583” (xyz position). And then follow that with all the values you’d like, comma between each, so you can break down that (Very) long string into an array. Go through the array, and if you know each enemy has maybe 50 spots, then every 1st and 51st spot would be a new enemy.
It’s tedious to set up, but if you stay organized it should be pretty straight forward.
It should also be possible to write an editor extension to manage all that in a more visual way.
Do not use PlayerPrefs. PlayerPrefs is designed more for a players prefences (hence the name) on game configurations (audio volume, screen gamma, hotkey setup, etc.). they’re also not very secure and easily hackable (which for normal player configurations is fine, but not for game data).
You’re going to want to look into Serialization, more specifically Unity’s serialzation with scriptableObjects. No, its not simple programming, but with the requirements I’m hearing that you’ve set forth in your game, this is the best course of action.
Using Serialized ScriptableObjects comes with its own advantages not normally available in homemade Serialization implementations such as:
dual-referencing (where two variable reference one object and if its edited through one reference the other updates properly)
Polymorphism normal serialization doesn’t support it
Collections Normal Serialization has problems with things like Lists and Dictionaries, forcing you to convert to and from arrays while you serialize/deserialize. while complex collections (mutidentional or jagged collection) might take some work its still easier to work with through scriptable Objects
And (speaking from personal experience) it is worth it for you to try and figure this out early as not only will it set you on you way to understanding how unity manages its data, but serialization and ScriptableObjects are also closely tied to tools making which will be a great help to your designers and artists early in the project.
Take a look at these videos for some helpful info into Unity’s Serialization
Persistence Data Saving and Loading sfbaystudios already posted this. I agree that its quite useful in getting you started with general serialization. It doesn’t go into using Unity’s Serialization however
Introduction to ScriptableObjects another useful video focusing on the basics around the ScriptableObject itself.
- **Serialization In-Depth ** This video will be highly educational in helping you harness Unity’s Serialization. comes with the added bonus with some examples in making editor tools.
Unfortunately PlayerPrefs do not work directly to store information with Lists or Arrays. Meaning any extensive use of PlayerPrefs will require rediculous amounts of tedious manual programming to retrieve/save hundreds of PlayerPrefs with custom names.
This is also a problem because using a ton of PlayerPrefs is reportedly slow for performance.
I have been dealing with a similar problem lately after I designed my player’s inventory system. I realized that having randomized loot like any respectable RPG could potentially require hundreds of playerPrefs even with only around 20 inventory slots.
This problem sounds very similar to yours if you want many individual units to have multiple stats saved.
One possible solution I found is this:
But judging from their description it sounds like using PlayerPrefs in arrays like they have programmed is only recommended for somewhat small lists like 10 entries. I am not sure if it would work effectively for complex systems that many Unity users will probably need eventually depending on their game type.
It seems that saving with standard binary IO type stuff is the only reasonable solution. But I currently know literally nothing about that and am in no rush to start.
Maybe if enough people request this kind of feature to store complex data structures permanently in a user friendly manner they might try to develope a better built-in solution for Unity.
I think it’s still possible to do with PlayerPrefs if you are extremely meticulous with your functions and planning. But the amount of time to program this compared to being able to simply retrieve playerPrefs from a list is crazy talk.
Here’s hoping for some Unity innovation to allow new programmers to save data in a reasonable fashion
Unity is a game engine, not a storage engine. If the complexity of your data is high, you will need an actual data storage solution. What that solution that might be depends on your game and your target platforms, but trying to push complex data into comma separated strings is a fools errand.
Search for an external solution that meets your needs, do not try to stretch Unity into doing something it’s not designed to do.