Forum Thread
  Posts  
C# Visual Studio reading and writing files (Forums : Coding & Scripting : C# Visual Studio reading and writing files) Locked
Thread Options
Jul 3 2016 Anchor

I'm working on a text based game so that I can start applying and expanding my programming knowledge. Something I have never done before has come up and I'm hoping someone on here can help me out! I've read that reading and writing into external files is the best way to handle a lot of data like the different rooms of the game and the inventory system, and all the dialogue. I'm not really sure how to do that or what kind of files to use. I mean as far as reading and writing to a file, I can look that up pretty easily, but I don't know what to do with what I take from the files. I also don't know how to write the files exactly. I've started one in xml simply because it seems to be an easy choice since I'm using visual studio, but I'm not sure if this is the right choice or not. An example of what I am trying to do is this: Right a file with a Root Element called Rooms. Then each child element will be each individual room of the map. Beneath those would be the description the player receives about the room, as well as a list of items the player could potentially pick up. The root of my question here is how do I use this file to help me track which room the player is in, and what the player has interacted with in the room, etc. I really hope this makes sense and that someone can help me with this! Thank you!

Nightshade
Nightshade Senior Technical Artist
Jul 10 2016 Anchor

Chill - this is easy stuff :)

Being able to manipulate files on disk is an important basic skill of any programmer, and isn't that hard to get into.
If your data doesn't have to be protected from external manipulation (game progress, etc) you can go with a data-format such as JSON (JavaScript Object Notation) - which is unencrypted - and just leave the files out in the open in the game/installation directory. Many games save data like this! The great thing about JSON is that it's very human-readable and has a really simply and neat syntax making it easy for you to write a compiler for it (if you can't use a lib that is) if you have to! For data that has to be secured from manipulation you need to read up on encryption (not that difficult topic) but you can always save that problem for the future and go with unencrypted data during development.

Now for the actual reading/writing - use google. You can find plenty of tutorials on this and it's also covered by the C# documentation (which you need to learn how to use anyway). I don't how it works in C# precisely as I haven't worked with that language - but it's probably similiar to Python and JavaScript, where you first create a file object and then set a path to the actual file as a property of said object. Then you have various functions for processing data (like read line, read character, move position to byte X, defining the row delimiter, etc) and you either read, write, edit or append to that file. Last you use a function for closing the file object. You will get the hang of it quickly!

JSON syntax example:

Also: JSON is a universal file format great for storing data that needs to be accessible to different applications.

Edited by: Nightshade

--

Senior Technical Artist @ Massive - a Ubisoft studio
Portfolio | LinkedIn

Jul 10 2016 Anchor

Thank you for the help! I did read through the documentation and it helped quite a bit but I still had some more questions. I think I've got enough figured out now to where I can start doing it and kind of learn the details as I go. That's usually the best way to learn anyways! Thanks again!

Jul 22 2016 Anchor

Hi people,

I learn a lot about save and load data wathing this video.

Unity3d.com

Maybe it's good for you too.

Aug 3 2016 Anchor

I'll check this out too! Thanks!

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.