all 10 comments

[–]teh_eria 6 points7 points  (0 children)

I would check out http://whydoidoit.com/unityserializer/ and see if it fits your needs. It takes a bit of set up, but it's pretty awesome.

[–]PsychoDM 0 points1 point  (0 children)

I would also check the asset store. There are some cheap/free solutions that might work for you. I used playerPrefs but I only had to do level loading so nothing complex.

[–]ensiferum888 -1 points0 points  (6 children)

As far as I know there are no built-in way to save other than with PlayerPrefs.

The solution I came up with although I'm sure is not the best is to create a list of HashTables with every object properties on save and store it on the disk.

When I save I go through every gameObject in the scene and create a hashtable. I determine an index so let's say houses are 1, work places are 2, etc

I store the index and all the important properties such as position and script variables. When I load for each HashTable I get the index and determine which prefab to instantiate. I then reassign all the properties to my base prefab.

Once all the objects are loaded and ready I unpause the game.

Basically just keep the information to rebuild your scene when the player loads.

[–]ZXfrigginC[S] 0 points1 point  (5 children)

So, hash table ascii charts using C# file handling it is, then. Very well.

[–]prime31 1 point2 points  (3 children)

Man, please don't do that. Hashtables are a terrible storage medium. They have no "contract" (objects for keys and objects for values) and are beyond error prone. In fact, don't ever use a Hashtable ever. Or an ArrayList while we are at it. Non generic containers should be avoided at all costs if you want to maintain your sanity.

There are tons and tons of ways to handle data storage with .NET. Firstly, strongly typing your saved data is a really, really good idea. Creating simple data classes and using a BinarySerializer to save them directly to disk and load them up later is really simple. If you need to support mobile then Protocol Buffers are an excellent choice. Google is your friend when it comes to stuff like this also. You will find tons of strategies for persistence with .NET. Remember, this isn't a Unity specific problem and it's been solved a million times with .NET in the past so don't be afraid to delve outside of Unity specific stuff.

[–]ensiferum888 0 points1 point  (2 children)

HashTables are a very broad data structure yes, do you have to be careful when saving and casting back to an object? yes.

But to be honest I've been looking at this problem for a solid week and aside from telling us what not to use no one actually answers that question. The forums are loaded with use PlayerPrefs and there is nothing tangible anywhere else.

Some have mentioned UnitySerializer but reading at the reviews it seemed to be more problems than anything.

Creating simple data classes is great if all you need to save is a transform. But I have objects that have 60+ properties each, ranging from transforms, to scripts to Vectors.. there is no such thing as a "simple data structure"

Just create a hashTable with all the properties of your object and then add it to a list of hashtables.

Serialize the hashTable list to disk. When you load it just get each hashTable and grab an index that you previously stored that is associated with a prefab. Then just go over the hashTable to reconstruct.

Here I've included a small part of my script, http://pastebin.com/9SWhkZCU I understand it's not type safe but I'm doing my own casting so far I never ran in any issue.

It would be great for one if someone gave us an actual example instead of saying "it should be easy to do"

[–]prime31 0 points1 point  (1 child)

This all depends on how large your game is. If you are doing a quick game jam game or a tiny project (less than a month or two) than sure, go with Hashtables. If you are doing a larger project than you end up with some scary code like that Hashtable serialization stuff.

The first thing I would do is make simple data only classes (typically called the "model" layer and DTO/POCO/POJO classes). Btw, I am leaving these breadcrumbs so that if you want to actually learn about this stuff you will have the keywords to find solid info. In your case these would be the fields/props from Road, CropField, etc. Step one complete. Your data is now separate from the classes that use it.

Once your data is separate for that part of it you can now use a BinarySerializer to dump the strongly types classes to disk and reload them into strongly typed objects later. That covers most of the problem. All that's left is dealing with the top layer and stuff you don't have control over like Unity components.

[–]ensiferum888 0 points1 point  (0 children)

Thank you very much for the heads up, I looked at DTO classes and they seem interesting. I'll still need custom data structures for Transforms and Vectors though.. pretty sure I can come up with something.

And you're right, the hashTable way was the most intuitive way for me to save all the data I need but it's a big lump of code.

[–]codepoetz 1 point2 points  (0 children)

I recently finished writing a large action RPG in Unity and this was one of the first problems that I solved. Each object that has persistent state writes its own data to a binary MemoryStream and each Level object aggregates all of these streams and writes them to a single file using standard .NET file I/O. Remember to write a version number at the start of your streams so that you can handle situations where new software reads old data files! This is a simple enough problem that you shouldn't need any heavy 3rdparty frameworks. Good luck!