0

so quick rundown, im currently building a MVP for a text based adventure game for ym dnd players who havent had tabletop in a while.

i have all the systems working so far but one of the biggies i havnt done is saving/loading the game (or more accurately the players character data).

i WAS trying binary serialization but have since given up in favor of XML but my experiments so far have revealed that i have NO IDEA what im doing.

can anyone run me through understanding what i need to do please? (assume i dont know anything about xml saving.

| V my player class (minus the methods and getters/setters) 1

EDIT:

i'm learnign with the basics of streamWriter and StreamReader and was making progress when i started getting "IOException: The process cannot access the file 'D:\test\Game1.txt' because it is being used by another process." for files that it's creating and i dont understand whats causing it as there is no apparent cause, it was literally fine and then BOOM cant access anymore.

EDIT-EDIT: figured out the problem and got this critical system for my MVP working as required, wanna thank J.Salas as the link actually did help me towards the right answer, everyone else wernt any help.

but for future referance for anyone having the same problem: dont create static writer/reader for your data saving, if they point to the same file they prevent each other from working, i simply create them inside the save/load methods and it works like a charm.

additionally the StreamWriter.WriteLine()/StreamReader.ReadLine() method operate 1 line at a time and will move onto the next line when called, the path can be a full filepath for a very specific place or just "game1.xml" if you wanna have the files be created in the games default folder.

(look how easy it was to explain the basics fo how this works? just do this instead of linking to much less helpful google searches anyone can find)

Dredin 47
  • 21
  • 3
  • i still dont know how to make the image show in the post. edit: nvm i got it – Dredin 47 Dec 22 '22 at 10:31
  • You can google C# File, C# File save and so on https://learn.microsoft.com/zh-tw/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file – MichaelMao Dec 22 '22 at 10:32
  • i tried following random google searches like that and its not working, none fo the data actually gets saved, hence why i came here – Dredin 47 Dec 22 '22 at 10:34
  • Post your saving data code and do you got any exception? – MichaelMao Dec 22 '22 at 10:35
  • [Serialize de Object](https://learn.microsoft.com/en-us/troubleshoot/developer/visualstudio/csharp/language-compilers/serialize-object-xml) to a streamwriter and save the result stream to a file. – J.Salas Dec 22 '22 at 12:08
  • okay so i managed to get something together that seems to work but i cant serialize the dictionary of player items, any ideas? (it throws an exception so i cant confirm if its actually creatign the save file) – Dredin 47 Dec 22 '22 at 13:10
  • nvm, didnt work, the xml file was completely blank – Dredin 47 Dec 22 '22 at 14:30
  • You have a problem with dictionary AND dynamic object, see [this post](https://stackoverflow.com/questions/47757808/how-to-serialize-dynamic-object-to-xml-c-sharp) for possible solutions – J.Salas Dec 22 '22 at 14:50

1 Answers1

0

At this time, you can use ReaderWriterLockSlim to protect the file, and treat "xmlDocument.Save" as a "FileStream file = new FileStream("1.txt", FileMode.Open, FileAccess.ReadWrite);".

static void SaveXml()
        {
            Console.WriteLine(DateTime.Now.ToString() + "    " + "SaveXml");
            lockSlim.EnterWriteLock();
            try
            {
                Console.WriteLine(DateTime.Now.ToString() + "    " + "xmlDocument begin save");
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(@"<note>
<to> George </to>
<from> John </from>
<heading> Reminder </heading>
<body> Don't forget the meeting!</body>
</note>");
                xmlDocument.Save("1.txt");
                Console.WriteLine(DateTime.Now.ToString() + "    " + "xmlDocument Save ok");
            }
            finally
            {
                lockSlim.ExitWriteLock();
            }
        }
wenbingeng-MSFT
  • 1,546
  • 1
  • 1
  • 8