JSON+Class/Struct/else?

Once again, I’ve got a noob question:

I’ve got hundreds of different characters with plenty of statistics. I used to store the variables in their CharacterScript(Monobehaviour). For the save/load features, I’m iterating through tons of GameObjects then grabbing their variables and storing in a DB, all good.

Except I’m starting to take a look at more… intermediate stuff. I realize it might be much simpler to build a Creature class or Struct and then try and save the Object itself.

At the same time, I’m taking a look at JSON and I’m able to encode/decode basic types: String/int/etc…

Is there any way to have a system where I could just initiate all my Class/Struct instance, encode them and save as blob and load them as-is with the help of JSON ?

EDIT: Serialize?

I know I’m asking a lot, but any hint is welcome :slight_smile:

.NET has System.Xml.Serialization which allows you to take any object and “turn it into” an xml file and vice versa.

Not all data can be serialized this way (for example, you cant serialize a reference to a GameObject in your scene and expect a new deserialized object to also reference that same GameObject . Though you could achieve this rather painlessly by storing the name of the referenced GameObject in a string). You can control which properties are serialized and how the process happens.

There MAY be something similar if you must use JSON but I doubt it would be as full-featured as .NET’s XML stuff, though it would probably be more lightweight

Here are a couple of links:
http://support.microsoft.com/kb/815813
http://msdn.microsoft.com/en-us/library/2baksw0z%28v=VS.100%29.aspx

Start with the first one to see the basic procedure. The second link will get you started on learning how to control the serialization. Also just googling “XML serialization C#” will yield a lot of good results and tutorials.

Awesome! Thanks alot. I’ve just got my XML serializer working. I had to tweak around a bit but I can now save my Character class faster and more easily. Thanks again for putting up with me and my noob questions every single time, its really appreciated.

I definitly couldn’t have learned Class,Struct,XML serialization and DLL basis in an afternoon without your help, hehe!

Any idea if I can count on XMLserialization to work on Mac?

EDIT:
Also, can I get XMLSerializer.Serialize() to output to a variable instead of having to send it to a Stream/Writer?

You can make it output to a MemoryStream etc from where you can get a string or where you can push a string in. Also keep in mind, that Memorystreams etc are also variables, like any other.
The XML Serialization script on the unifycommunity wiki shows how to do this if I remember it correctly

Your welcome :slight_smile: I’ve learned a ton just from asking noobish questions on the forum as well, it never hurts to ask. Keep it up, you are learning very quick!

I don’t see any reason why it wouldn’t work on Mac. Are you asking because I sent you to Microsoft.com? :wink:

Well, kinda. Perdon my ignorance once again, but all the “using/import” means I’m using DLLs right? And aren’t those platform-specific?
Or are those System.x; found on every ‘modern’ O/S?

And thanks Dreamora, I got a MemoryStream to catch the serialization and output to SQLite. Super-clean, super-fast, super-awesome! :smile:
I’ve now got a CharacterList class holding >100 StatisticSheet subclass. And I can just serialize the whole CharacterList and restore it as one, instead of the >3000 single statistics.

I can’t thank you enough guys. I’ve learned more in the last 24hrs than the last few weeks,hehe :slight_smile:

In general they are only found on Windows systems with .NET installed, or some other platforms with Mono installed.

But Unity Editor and Player comes with a custom version of Mono which contains most of them. So for your Unity app, you can depend on these being available on Win or Mac. iPhone Android, etc. have a slighly smaller subset. See here for details:
http://unity3d.com/support/documentation/ScriptReference/MonoCompatibility.html

I recommend Json.NET , a easy library for json and c#.

http://james.newtonking.com/projects/json-net.aspx

No, you’re not using anything external to Unity. “using/import” just applies to namespaces, they don’t “load” anything. If you were using actual separate external libraries, you’d know it, because you’d have to work at it (and you’d need Unity Pro).

–Eric

You don’t need Unity Pro to load external dlls if they only reference classes that are available in Mono though right? I seem to be dong this without Unity Pro.