Sign in to follow this  
irras88

XML

Recommended Posts

irras88    100
I've read a few things that state the use of XML files to either (dynamically?) generate classes or instances of classes. I wondering if anyone could provide an example or two of what this could be used for and how exactly it is done, or a link to somewhere that explains it well.

Also, what are the advantages of using XML instead of, say, a structured .txt file?

Share this post


Link to post
Share on other sites
laztrezort    1058
If you are using C#, serializing/deserializing class instances to/from XML is almost trivial. Otherwise, you can try loitering around Codeproject, I seem to remember many relevant articles over there. XML is a structured text file, just in a standardized format - the big advantage to that being that you can find code & libraries to manipulate XML for whatever language you are using without having to roll your own.

Edit: I should clarify, since I'm not sure what you are asking - Serialization is the process of storing data, typically to a file on the hard drive, for example to save game state. I assume this is what you asking about?

Share this post


Link to post
Share on other sites
irras88    100
Not quite with the game state thing. Not sure specifically what I'm wanting to do with it. The project I'm working on now is basically just a mass of code that I'm making into a game just for the sake of practice and learning, so when I think of something I've heard/read about I try to find a niche for it in this game.

The idea I had though, was creating my enemies in this way instead of explicitly creating a class for each type. I'm not sure if this is exactly what this does or not, but that's why I'm asking :)

Share this post


Link to post
Share on other sites
pulpfist    528
[quote name='irras' timestamp='1313968580' post='4852049']
Also, what are the advantages of using XML instead of, say, a structured .txt file?
[/quote]
One advantage is that you can add new elements to your xml nodes without braking existing code.
Another advantage is that xml is structured as a hierarchy, while a text file would typically be flat.
Libraries that extract data from xml files can take advantage of this structure.

Some languages like C#, Go and others has methods that can store and retrieve objects to/from xml files automatically by looking at the class definition, but I'm not sure that is what you are talking about here.


Share this post


Link to post
Share on other sites
CoffeeSlave    102
[quote name='irras' timestamp='1313968580' post='4852049']
I've read a few things that state the use of XML files to either (dynamically?) generate classes or instances of classes. I wondering if anyone could provide an example or two of what this could be used for and how exactly it is done, or a link to somewhere that explains it well.[/quote]

I'm a chronic newbie, so please don't take my advice uncritically.

I think you might want to look at dynamic memory. If you were making a game with tiles in a grid you could take a number out of an external file and use it in a loop to generate new instances of a class, and then add a reference of each address into a container. It means you wouldn't need to recompile just to add new columns and rows, and you don't need names as long as you can keep track of the object's address. You might need to delete them afterwards depending on the language and how you go about it.

Is this what you meant, or am I being horribly patronising? (If so, I don't mean it.)

Share this post


Link to post
Share on other sites
laztrezort    1058
[quote name='irras' timestamp='1313983160' post='4852104']
Not quite with the game state thing. Not sure specifically what I'm wanting to do with it. The project I'm working on now is basically just a mass of code that I'm making into a game just for the sake of practice and learning, so when I think of something I've heard/read about I try to find a niche for it in this game.

The idea I had though, was creating my enemies in this way instead of explicitly creating a class for each type. I'm not sure if this is exactly what this does or not, but that's why I'm asking :)
[/quote]

Trying out new things is a good learning experience - but be careful not to over complicate things :wink:

I don't think XML is what you are looking for here. There are many ways of dealing with game entities - class inheritance, composition, component-model, etc. Why do you think you would need a separate class for each type of enemy? Enemies should be similar enough to share a single class (or possible a small inheritance tree).

Unless, of course, you are talking about loading the enemy classes up with data, not defining the classes themselves, in which case XML could be a consideration.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this