sql in untiy
I know most people recommend serialization for saving data in unity, but I wanted to hear about the pros/cons of using sql. My game needs to store data for 100s of procedurally generated objects (solar systems, planets, etc). I would also like the data solution to be multiplayer friendly.
The main reasons I see people not wanting to use sql is because its "complicated", but while Im new in gamedev, I have quite a lot of experience in c# and sql from a business applications perspective.
tldr: sql is not scary to me, serialization is. for saving this much data (and multiplayer), does sql make the most sense?
thanks
Posted Sep 19, 2017, 3:46 AM UTC
So, it slightly depends on what you're talking about. Are we talking about a multiplayer system, with a central server that has to handle massive amounts of concurrency, many thousands of players querying it constantly? If so, then yes, SQL servers and traditional databases would probably be a good idea. SQL servers are good at concurrency.
But if we're talking "this is a single-player game, and I procedurally generate a huge amount of data which i want to be able to save locally", then no, SQL is probably not necessary, or even the right solution. It is much simpler to use serialization (just use
JsonUtility) andPlayerPrefs, which has the advantage of working cross platform out of the box (if you really want to, I guess skip player prefs, and just write your planet files to a folder inApplication.persistentDataPath). It's probably going to be faster than SQL, and far easier to implement. Maintaining those SQL tables seems like a nightmare, every time you add a parameter you would have to add a column, and make sure everything was backwards compatible. Serialization is far less scary than that.I guess the one thing SQL would be interesting is if your data is strongly relational, and then you can take advantage of joining data together. There might be an argument for something like SQLite there, but it honestly sounds like a huge pain in the ass to me.
Also, you say "sql is not scary to me, serialization is". I have bad news. You have to learn how Unity's serialization works sooner or later. It's a core part of developing for Unity. You just have to bite that bullet. Might as well do it for this. Also, it's not scary. Just go
var data = JsonUtility.ToJson(myObject)andvar myObject = JsonUtility.FromJson<MyClass>(data). That will result in all serializable fields (public fields of a serializable type, or fields marked with[SerializeField]) being saved. That's literally all there is to it.I have two pieces of advice: first, make sure this actually is the bottleneck you think it is. It might be that this isn't a problem at all when you actually implement it, everything just works. "Premature optimization is the root of all evil" and all that. Programmers are lousy at estimating what will have performance problems, which is why profilers exist.
Second, if you want to make sure to leave your options open, make an abstraction layer with pluggable backends. That way, you can use whatever solution is easiest at first, and if you notice that it's a problem, you can make a new one and just plug it in instead.
first off, thanks for the awesome response
I do this so much for my day job that im pretty immune to how annoying it is, but its a good point.
Im curious what you would define as "strongly" relational. the basic design I have on paper is quite relational. systems having planets that are made of different elements, having different resources, different races who have relationships with other races on other planets. all able to be affected by the player
any reason you recommend json opposed to the regular unity serialization? and is it really THAT simple? I almost done believe it lol. I just mark everything as serializable and have it all come from some master "game" class and thats it? it just works?
good point and a good call out. Like you said, data access layer is going to be abstracted anyways so I should just pick something and do it, and if it sucks I can change it later.
another truth about programmers is that we are the worst testers. :P
thanks again
That is a fair point, that sounds like fairly relational data. I guess there's an argument for something like SQLite, if you specifically want to do things like "I want a list of all races that are friends with race X, and have green hair and lives on a planet that starts with the letter J".
Honestly, I'm not sure there is a way to hook into "regular unity serialization". Unity uses it internally to generate things like scenes and prefabs into either binary or YAML. But I don't actually think there's a user-facing function that does that except for JsonUtility. I might be wrong about that though. One nice thing about JSON is that it's very easy to debug.
Pretty much. Try this out:
Look at the output. Note that the only change I've done here is mark the inner class as serializable (which you have to do for custom types, either classes or structs), so that Unity recognizes it as such. Everything else happens automatically.
One thing: here I'm saving a MonoBehaviour, and MonoBehaviours are a little bit special since they're components. You have to create a MonoBehaviour manually first (via
gameObject.AddComponent<>()) and useJsonUtility.FromJsonOverwriteinstead of the other method. But if you use it for "regular" serializable classes (like theInnerTestClassI have there) it is just that easy. The tricky part is if you have a reference to another MonoBehaviour, at which point the instance id is serialized, which is not necessarily persistent across sessions. I usually avoid serializing MonoBehaviours directly, even though you can, instead storing my data in "regular" classes that I serialize.As I mentioned, even if you don't go this route, learning about Unity serialization is a really good idea. It's how scenes are stored, it's how prefabs are stored, it is how assemblies are reloaded, it's how values are exposed in the inspector (that's a good rule of thumb: if you can see a value in the inspector, it will be serialized, modulo the
[HideInInspector]attribute or custom inspectors), and on and on and on. It's worth your time understanding what is serialized and what the effects are.As I said, I'm not totally opposed to a SQLite solution, and I can see why you wanna go that way, especially if that's what you're comfortable with. It's by no means an "invalid" solution or terrible idea. But I would suggest giving Unity's serialization solution a genuine shot, it's generally speaking a fairly painless process and one that integrates very well with the larger Unity environment.
Good luck!