• Register
Forum Thread
  Posts  
xml files for a c# text based game (Forums : Coding & Scripting : xml files for a c# text based game) Locked
Thread Options
Aug 20 2016 Anchor

I'm working on a text based game (similar to Zork) and I am currently writing the xml file. So far I've only worked on the rooms themselves. My question is do I need to put the inventory system in an entirely different xml file, or just in a different section? I've never used external files like this before so I'm still learning. Is it ok to have separate xml files? Is it preferred, like when you start a new class in c#? Thanks for any help!

Nightshade
Nightshade Unemployed 3D artist
Aug 20 2016 Anchor

For clarity you should try and place things in different files imo - unless they belong.

Edited by: Nightshade

--

   - My portfolio
“There he goes. One of God's own prototypes. Some kind of high powered mutant never even considered for mass production. Too weird to live, and too rare to die.” Hunter S. Thompson

Aug 24 2016 Anchor

ok so I should have an xml file for rooms and an xml file for inventory. that's makes sense. how do I actually take the xml file and turn the elements into objects? I've read some stuff on doing it but I'm still a little confused though.

Aug 24 2016 Anchor

You have to open and read the xml; then you have to parse the data accordingly.

Say you have an integer "Item level" somewhere in your code and that integer is also written in your xml, you would do something like "int.TryParse(xml string, out int)";

Just make sure to not access that file all the time, it would be pretty slow, access it once and then parse the data when needed.


-edit-
To expand on what I have said, consider the xml like a "collection" of strings layed out the way you want, what you want to do with said strings is up to you.
For instance, if your item element is composed of:
string itemName;
int price;
float status;
Vector2 position;
bool canBeSold;

You would read those elements from your xml file, so the string as it is, int/float.TryParse(xmlstring, out int/float), the Vector could be parsed as int[] splitted by "," or any other symbol (depending on how you layed it out in your xml file) and the bool could simply be: your bool = String == "True" ?

Of course all that should be done in a loop if there are many items.

Edited by: Draakhex

--

Meadow of Dreams

Nightshade
Nightshade Unemployed 3D artist
Aug 26 2016 Anchor
ethanCodes wrote:

ok so I should have an xml file for rooms and an xml file for inventory. that's makes sense. how do I actually take the xml file and turn the elements into objects? I've read some stuff on doing it but I'm still a little confused though.

You use an XML-parser in the environment where you need to read the data. Some people even write their own but it can be too much work also. Then for saving, you need to read the objects and take the information there into data in the form of XML-tags. You do that step with an XML-composer. That step is easy and could be done by yourself without hassle in case you have no XML-composer in your library.

But, imo JSON is a better format for data passing/storage - easier to read, prettier, easy to work with (imo).

Edited by: Nightshade

--

   - My portfolio
“There he goes. One of God's own prototypes. Some kind of high powered mutant never even considered for mass production. Too weird to live, and too rare to die.” Hunter S. Thompson

Aug 26 2016 Anchor

Thanks for the info guys. I'm going to try some of this out, but I'm sure I'll be back with more questions!

so I've been playing with this a little bit. I'm running to a small problem with setting up the reader. it's not taking my xml file path. I'm getting a runtime error with it. Also, in my research I've found some info on Linq. would this be a better option?

Nightshade
Nightshade Unemployed 3D artist
Nov 6 2016 Anchor

Just bumping this to get back to you and hear how things have been going.
I've never used Linq myself but I assume it's some dot NET thing? I would still go for either XML or JSON but maybe that's because I am biased.

--

   - My portfolio
“There he goes. One of God's own prototypes. Some kind of high powered mutant never even considered for mass production. Too weird to live, and too rare to die.” Hunter S. Thompson

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.