- Home /
Hello,I am trying to build a save mechanism for a building game in unity in which the player can save a 3d scene.should i use a database or what?
Hello everyone i am building a building game in unity in which the player can build a city i want the player to be able to save the scene that he has build so he can go back to it when he plays next time,how would i be able to build the saving mechanism.Should I use a database like mongodb to save the position,rotation,scale,etc for the objects that the player has made and placed(but i think that would be too lengthy) or should i do something else.Pls Help
Tunials answer is pretty correct and sufficient but I would add to only use a database if your game is very data centric and will need to regularly store and access lot of data in a bunch of ways.
Just persisting basic game (i.e a few properties on a few different objects) state is probably not a valid use case for a full featured database.
If you are making something like an RPG then I would definitely say its a healthy consideration.
If it does need one then there are lots of options available and different types depending on the need but maybe the easiest to implement and integrate would be something like a SQLite database. $$anonymous$$ongo could also be a good candidate but I would suggest spending some time with document style databases before investing heavily in that direction over a relational style one because it can be a lot easier to tie yourself in knots.
Oh wait you said its a building game. Yeah that potentially could be useful
Thanks @sacredgeometry I was also thinking of using a database. I think a mongodb server would be best for me as I have used mongodb earlier also.
Answer by tuinal · Dec 05, 2020 at 12:23 PM
This for me is one of the best tutorials for it.
https://www.raywenderlich.com/418-how-to-save-and-load-a-game-in-unity
The 'proper' way to save in Unity is to serialise every element of data that needs to be saved, and write it to disk. You can in theory use a text file as above, or PlayerPrefs for quick prototypes, but PlayerPrefs is designed (as the class name suggests) to hold a small amount of information on preferred settings rather than a whole savegame.
You basically need to distil the core information into classes you can serialise, which means they need to be serialisable. This means breaking the data back down towards basic types (or serialisable ones like Vector3). Using GetComponentsOfType can be a big time-saver (e.g. you can get all rigidbodies then iterate through them saving position/rotation).
Saving (properly) in general is one of the trickier things to do in Unity and it's best if you make it a priority when starting development, especially if 'save anytime' is a required feature. It's particularly complicated if you're adding/instantiating a lot of things at runtime that need to persist, since you need to handle tracking them and respawning them in the correct state when the game loads. This is because you need to consider when adding any new variable 'will I need to save this?' and update the savegame code accordingly.
Thank you @tuinal,but I want to ask which would be better, a local saving method or a database.
Answer by Llama_w_2Ls · Dec 05, 2020 at 10:10 AM
You could write to a text file containing the position, type, and transform of each block in the building game and then extract that data every time you reload the scene by reading that text file. I've created text files thousands of lines long (when converting STL files to meshes) containing data such as this and it's read almost instantly if you use the right approach.
Warning: This isn't really the way to go if you want secure, safe data saving as the files aren't encrypted, and if the files are deleted or corrupted, all your data could be lost.
If you want to use a database, that seems like a more logical, efficient, industrial approach rather than the easy way out of writing and reading a text file. It's your choice @Sidadi657
Writing to databases will always be quicker and take up less space, as thousands of man-years have gone into perfecting the transmission and storage of data in databases. Choose a database type that would be best-suited for you. For example, if you're worried about the speed of sorting, retrieving and writing to databases, try a fast database. If you're worried about storage, try an efficient database. etc.
If the data was easily readable, the player could modify it without penalty @TheWhispers
Your answer
![](https://koobas.hobune.stream/wayback/20220613021111im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UnitySerializer Delete Saved Games 0 Answers
Creating Scene Chunk in 2d game and Altering Prefab 0 Answers
What can i do when my built Scene differs from my scene in the Editor? 0 Answers
Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers
Web player for my project 0 Answers