Best practice - public fields or private with access methods?

I have absolutely no idea what Reflection is. :wink: Something else to look up.

I’m serializing my objects into XML files to record game object attributes (save level, basically). I have a moderately complex adaptive structure (short tree) that I want to save and load.

I agree. Not because of the overhead as I know nothing about the overhead, but because unless I have an explicit need to monitor my variables because I want to control what happens whenever it’s set or received, It’s just superfluously sitting there making my code look ugly.

No… My question why more like “Why XML and not the Unity internal save format?”. You can save your data as .asset and Unity does all the Serialization/Deserialization.

Ugly? Now that’s hardly debatable, right? Every coder got a different point of view about what is ugly or pretty. Personally I can’t stand people who don’t align their brackets. :stuck_out_tongue:

When I researched saving realtime objects, everything was pointing to XML. There’s very little documentation to the contrary. Also being able to edit the objects via textfile is quite useful.

Unity can also save its data as text file, if you use the Pro version.

Just saying, because it’s using Unity’s own serialization, it’s just a 1 line call to save a file from an existing class.

I hate it when people insert blank lines and spaces randomly :stuck_out_tongue:

That would only work in the editor or in the game running in editor game mode. I think Shifty wanted to do that in pure game where AssetDatabase is not available, no?

http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

:slight_smile:

Combining AssetDatabase and Resources is fairly powerful. Frankly, it works right out of the box, and I wonder why someone would mess around with other serialization. It also works with AssetBundles and every deployment platform (Ex.: iOS).

I’m using Free version at the moment. So it looks like I’m using public variables, but there’s no real downside as long as I keep control of my code.

Exactly that is the point of having restricted access to variables, to make keeping control of code easier for you and those who work or will work with you and your code! The key is that “I” in your sentence :wink: - you will have to keep the control of your code instead of letting the compiler to do that for you automatically. And that is the only downside, but it’s an important one! Instead of focusing your efforts on doing more useful things you will be always more and more busy keeping control of always growing code, until it grows to some unmanageable size. Humans are limited in that aspect, while compilers are not! This is the key point of your question and its answer.